Jump to content

Wierd Problem with trying to create thumbnails


spyke01

Recommended Posts

I need to create 2 different sized thumbnails automaticly from an image being uploaded, the databas insertion works fine, as well as the first image, however the second and third images look like this:

[img]http://www.maroonweekly.com/assets/galleries2/maroonweeklystreetteam/1570932f817abf417fa50d20370b650b_mid..jpg[/img]
[img]http://www.maroonweekly.com/assets/galleries2/maroonweeklystreetteam/1570932f817abf417fa50d20370b650b_thumb..jpg[/img]

Heres the code thats called when an image is uploaded
[code]
if(isset($_POST[submit])){
               
                $typeOfFile = $_FILES['large_pic']['type'];
                $fileName = $_FILES['large_pic']['name'];
                $fileExt = strtolower(substr($fileName,strrpos($fileName,".")));
                $fileName = str_replace("\\","",$fileName);
                $fileName = str_replace("'","",$fileName);
                $randName = md5(rand() * time()); // make a random filename
               
                $largefilename = "../assets/galleries2/maroonweeklystreetteam/" . $randName . $fileExt;   
                                           
                // Upload the full sixed image
                move_uploaded_file($_FILES['large_pic']['tmp_name'], "../assets/galleries2/maroonweeklystreetteam/" . $randName . $fileExt);
               
               
                // Get the image size so we can calculate the new size
                list($width, $height) = getimagesize("../assets/galleries2/maroonweeklystreetteam/" . $randName . $fileExt);
               
                if ($width < $height) {
                    // Get what the next ID will be - not pretty but i dont feel like fixing it               
                    $sql = "SELECT ID FROM galleryimages ORDER BY ID DESC LIMIT 1";
                    $result = mysql_query($sql) or die(mysql_error());
                   
                    while ($row = mysql_fetch_array($result)) {
                        $last_id = $row['ID'] + 1;
                    }
                   
                    // Insert the caption and image into the DB
                    $sql = "INSERT INTO `galleryimages` (ID, filename, memberof, caption, numinorder, height, width, owner) VALUES('$last_id', 'assets/galleries2/maroonweeklystreetteam/". $randName . $fileExt . "', '0', '$_POST[caption]', '$last_id', '0', '540', '0')";
                    $result = mysql_query($sql) or die(mysql_error());                   
                                       
                    // Our files
                    $mediumfilename = "assets/galleries2/maroonweeklystreetteam/" . $randName . "_mid." . $fileExt;
                    $smallfilename = "assets/galleries2/maroonweeklystreetteam/" . $randName . "_thumb." . $fileExt;
                               
                    // Create the Medium sized pic - Height is more important than width to us
                    $tosubtract = $height - 380; // This tells what to subtract the current hieght and width by to get 380
                    $newwidth = $width - $tosubtract;
                    $newheight = $height - $tosubtract;
                   
                    createthumb($largefilename, $mediumfilename, $newwidth, $newheight);
                   
                    // Create the Thumbnail - Height is more important than width to us
                    $tosubtract = $height - 159; // This tells what to subtract the current hieght and width by to get 159
                    $newwidth = $width - $tosubtract;
                    $newheight = $height - $tosubtract;
                   
                    createthumb($largefilename, $smallfilename, $newwidth, $newheight);               
                }
                else {   
                    echo "\n            <center>";
                    echo "\n                The height of your image should be larger than the width. Please crop the image and try again";
                    echo "\n            </center>";
                    echo "\n            <br />";
                    echo "\n            <br />";
                }
                unset($_POST[submit]);
            } 
[/code]

and heres the create thum function
[code]
//============================================
// This function is designed to create a
// thumbnail from the given image.
//
// ORIGINALLY FROM:
// http://icant.co.uk/articles/phpthumbnails/
// Modified for use here
//
// USAGE:
// createthumb($p, "tn_".$p, 150, 150);
//
// OPTIONS:
// $name        Original filename
// $filename    Filename of the resized image
// $new_w        width of resized image
// $new_h        height of resized image
//============================================
function createthumb($name, $filename, $new_w, $new_h) {
    $system=explode(".",$name);
   
    if (preg_match("/jpg|jpeg/", $system[1])){$src_img = imagecreatefromjpeg($name);}
    if (preg_match("/png/", $system[1])){$src_img = imagecreatefrompng($name);}
   
    list($old_x, $old_y) = getimagesize($name);
   
    if ($old_x > $old_y) {
        $thumb_w = $new_w;
        $thumb_h = $old_y * ($new_h/$old_x);
    }
   
    if ($old_x < $old_y) {
        $thumb_w = $old_x * ($new_w/$old_y);
        $thumb_h = $new_h;
    }
   
    if ($old_x == $old_y) {
        $thumb_w = $new_w;
        $thumb_h = $new_h;
    }
   
    $thumb_w = number_format($thumb_w, 0, '.', '');
    $thumb_h = number_format($thumb_h, 0, '.', '');
   
   
    $dst_img = ImageCreateTrueColor($thumb_w, $thumb_h);
   
    echo $dst_img . " " . $src_img . " " . 0 . " " . 0 . " " . 0 . " " . 0 . " " . $thumb_w . " " . $thumb_h . " " . $old_x . " " . $old_y;
   
    imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $thumb_w, $thumb_h, $old_x, $old_y);
   
    if (preg_match("/png/", $system[1])) {
        imagepng($dst_img, "../" . $filename);
    }
   
    else {
        imagejpeg($dst_img, "../" . $filename);
    }
   
    imagedestroy($dst_img);
    imagedestroy($src_img);

[/code]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.