Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 13 of 13
  1. #1
    Regular Coder
    Join Date
    Sep 2007
    Posts
    809
    Thanks
    5
    Thanked 2 Times in 2 Posts

    Image upload nightmare!

    Hi all,

    I currently have an image upload/resize script but the thumbnails generated are terrible!

    I thought it might be easier to find an existing script that could do this... I've taken a look at this one:

    http://www.blazonry.com/scripting/upload-size.php

    But when I download it and make changes etc it always fails saying it is not JPEG (although it certainly is) and:

    Warning: unlink() [function.unlink]: No such file or directory in

    Although I would assume the latter is because it wont upload in the first place!

    Can anyone link me to any good image upload/resize scripts that generate good quality thumbnails that are limited to certain dimensions?

    Any help would be appreciated as this is driving me mad!

    Many thanks,

    Greens85

  • #2
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    Used as:

    Code:
    $newimage = image_resize($file, $width, $height);
    returns an array with the info for the newly created image.

    Code:
            function image_resize($file, $width, $height, $delete = true)
            {
                    $image = getimagesize($file);
                    $image['type'] = $image[2];
                    $image['width'] = $image[0];
                    $image['height'] = $image[1];
    
                    if (!$image)
                    {
                            return false;
                    }
    
                    /***********************************/
    
                    $ratio = ($image['width']/$image['height']);
                    $image['file'] = $file.time();
    
                    if ($image['width'] <= $width && $image['height'] <= $height)
                    {
                            copy($file, $image['file']);
    
                            if (is_file($image['file']))
                            {
                                    if ($delete && is_file($file))
                                    {
                                            unlink($file);
                                    }
                                    $image['name'] = basename($image['file']);
    
                                    return $image;
                            }
                            return false;
                    }
                    else
                    {
                            if (($width/$height) > $ratio)
                            {
                                    $width = ($height*$ratio);
                            }
                            else
                            {
                                    $height = ($width/$ratio);
                            }
    
                            /***********************************/
    
                            $dimg = imagecreatetruecolor($width, $height);
                            $background = imagecolorallocate($dimg, 255, 255, 255);
                            imagefill($dimg, 0, 0, $background);
    
                            /***********************************/
    
                            if ($image['type'] == 1)
                            {
                                    $simg = imagecreatefromgif($file);
                                    imagecopyresampled(
                                            $dimg, $simg, 0, 0, 0, 0, $width,
                                            $height, $image['width'], $image['height']
                                    );
                                    imagegif($dimg, $image['file']);
                            }
                            else if ($image['type'] == 2)
                            {
                                    $simg = imagecreatefromjpeg($file);
                                    imagecopyresampled(
                                            $dimg, $simg, 0, 0, 0, 0, $width,
                                            $height, $image['width'], $image['height']
                                    );
                                    imagejpeg($dimg, $image['file'], 85);
                            }
                            else if ($image['type'] == 3)
                            {
                                    $simg = imagecreatefrompng($file);
                                    imagealphablending($simg, true);
                                    imagesavealpha($simg, true);
                                    imagecopyresampled(
                                            $dimg, $simg, 0, 0, 0, 0, $width,
                                            $height, $image['width'], $image['height']
                                    );
                                    imagepng($dimg, $image['file'], 9);
                            }
                            imagedestroy($dimg);
                            imagedestroy($simg);
    
                            /***********************************/
    
                            if (is_file($image['file']))
                            {
                                    $image['name'] = basename($image['file']);
                                    list($image['width'], $image['height']) = getimagesize($image['file']);
    
                                    if ($delete && is_file($file))
                                    {
                                            unlink($file);
                                    }
                                    return $image;
                            }
                            return false;
                    }
            }
    Last edited by MattF; 12-03-2010 at 05:09 PM.

  • #3
    Regular Coder
    Join Date
    Sep 2007
    Posts
    809
    Thanks
    5
    Thanked 2 Times in 2 Posts
    Hi Matt,

    Thanks for this!

    How would this handle portrait and landscape images? I have found that mine and many of the others I have tried on the net dont display portrait images too bad but aren't too clever at handling landscape images.

    Is it possible to set specific dimensions with this?

    Many thanks.

  • #4
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    Width and height set the maximum dimensions allowed. If the original image is above those sizes, it will be proportionately scaled and reduced, hence yes, it should be fine with both portrait and landscape. Try it and then you'll know for definite. Example:

    Code:
    Portrait:
    
    $newimage = image_resize($file, 600, 800);
    
    Landscape:
    
    $newimage = image_resize($file, 800, 600);
    Last edited by MattF; 12-03-2010 at 03:42 PM.

  • #5
    Regular Coder
    Join Date
    Sep 2007
    Posts
    809
    Thanks
    5
    Thanked 2 Times in 2 Posts
    Quote Originally Posted by MattF View Post
    Width and height set the maximum dimensions allowed. If the original image is above those sizes, it will be proportionately scaled and reduced, hence yes, it should be fine with both portrait and landscape. Try it and then you'll know for definite. Example:

    Code:
    Portrait:
    
    $newimage = image_resize($file, 600, 800);
    
    Landscape:
    
    $newimage = image_resize($file, 800, 600);
    Thanks for this Matt,

    I will give it a shot and let you know...

    Much appreciated.

  • #6
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    Just made a slight alteration to the function above, (added lines 4, 5 and 6 and changed lines 1 and 3), so use the updated version. This is an example of it in use:

    http://gxcr.org/devtest/

    Click on the thumbnails and you'll see the original images they've been created from, via that function.

  • #7
    Regular Coder
    Join Date
    Sep 2007
    Posts
    809
    Thanks
    5
    Thanked 2 Times in 2 Posts
    Quote Originally Posted by MattF View Post
    Just made a slight alteration to the function above, (added lines 4, 5 and 6 and changed lines 1 and 3), so use the updated version. This is an example of it in use:

    http://gxcr.org/devtest/

    Click on the thumbnails and you'll see the original images they've been created from, via that function.
    Hi Matt,

    Sorry for the delayed response to this!

    I've just tried to test out your script but I got a 404 error... I assume you have since taken the script down?

    Cheers,

    Greens85

  • #8
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    Temporarily. It'll be back within the hour. It's my test site, so tends to disappear and reappear on occasion whilst I'm reinstalling.

  • #9
    Regular Coder
    Join Date
    Sep 2007
    Posts
    809
    Thanks
    5
    Thanked 2 Times in 2 Posts
    Quote Originally Posted by MattF View Post
    Temporarily. It'll be back within the hour. It's my test site, so tends to disappear and reappear on occasion whilst I'm reinstalling.
    Ah I see... I have started implementing the script but I am having a few problems... probably more so to do with my lack of knowledge than your script!

    I am using this to upload and move my "big image":

    PHP Code:
    <?php
        
    include("../functions/resize_image.php");
        
        
    $file_ext strrchr($_FILES['imagefile']['name'], '.');   // Get The File Extention In The Format Of , For Instance, .jpg, .gif or .php
        
        
    $image_name=time().$file_ext;
        
    $newname="images/".$image_name;
        
        
    $copy copy($_FILES['imagefile']['tmp_name'], $newname);   // Move Image From TempLocation To Permanent Location    
        
        
    $newimage image_resize($newname250250);
    ?>
    However what I find is that the image is getting renamed twice and the final result is something like:

    100_0497.JPG1291637659

    I can see why this is happening, basically I am renaming and so are you in the function. However if I try and remove the naming part on mine it breaks the upload and copy().

    Any chance you could advise me here?

    many thanks.

  • #10
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    Code:
    <?php
        include("../functions/resize_image.php");
        
        $file_ext = strrchr($_FILES['imagefile']['name'], '.');   // Get The File Extention In The Format Of , For Instance, .jpg, .gif or .php
        
        $image_name=time().$file_ext;
        $newname="images/".$image_name;
        
        if (is_uploaded_file($_FILES['imagefile']['tmp_name']))   
        {
            $newimage = image_resize($_FILES['imagefile']['tmp_name'], 250, 250);
            rename($newimage['file'], $newname);
        }
    ?>

  • #11
    Regular Coder
    Join Date
    Sep 2007
    Posts
    809
    Thanks
    5
    Thanked 2 Times in 2 Posts
    Hi matt,

    Many thanks for that.. works a treat.

    I assume that the image now sitting in images/ is the resized image?

    As I need a full size image to be saved aswell, I guess thats just a case of using move_uploaded_file() before the resizing takes place?

    Then redirect the resized image to the thumbs folder instead of the images/ folder?

    As for portrait and landscape, I have tried both and both look 100% better, so many many thanks for your great function!

    many thanks,

    greens85

  • #12
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    Code:
    <?php
        include("../functions/resize_image.php");
        
        $file_ext = strrchr($_FILES['imagefile']['name'], '.');   // Get The File Extention In The Format Of , For Instance, .jpg, .gif or .php
        
        $image_name=time().$file_ext;
        $image="images/".$image_name;
        $thumb="thumbs/".$image_name;
        
        if (move_uploaded_file($_FILES['imagefile']['tmp_name'], $image))   
        {
            $newimage = image_resize($image, 250, 250);
            rename($newimage['file'], $thumb);
        }
    ?>

  • #13
    Regular Coder
    Join Date
    Sep 2007
    Posts
    809
    Thanks
    5
    Thanked 2 Times in 2 Posts
    Hi Matt,

    I actually forgot I posted here and managed to solve it in the meantime... however I doubt that my version is as efficient as yours so I will take a look at your solution!

    Once again many thanks for the great function! I have been tearing my hair out trying to find a way of sorting this with the best possible image quality and your solution has been the best by a long way...

    many many thanks,

    greens85


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •