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 5 of 5
  1. #1
    ubh
    ubh is offline
    Regular Coder ubh's Avatar
    Join Date
    Apr 2008
    Location
    Portland, Oregon U.S.A.
    Posts
    443
    Thanks
    108
    Thanked 15 Times in 14 Posts

    PHP GD Resize Image

    I already have an image uploading script that checks for lots of things like dupes of folders the images are placed in or even the image names them self's.

    Hell I even check the images width and height to run a condition if either width and height exceed 500px.

    In this condition.. all I want to do is if the image in either width or height exceeds 500px to resize either width or height while keeping the images ratio.

    You think I am asking for the freaking moon here folks, everything out on the net is trying to show me how to weave a mess of uploading and resizing with thumbnails when all I need is to resize my original image and overwrite it. I don't need stupid thumbnails and I CERTAINLY don't need to be shown how to upload images OVER AND OVER AND OVER AND OVER again.

    Please, anyone, a simple resize demo is all I need that will overwrite it original source image...

  • #2
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,538
    Thanks
    8
    Thanked 1,093 Times in 1,084 Posts
    Are all the images the same type, and what is the type .jpg, .gif?

  • #3
    ubh
    ubh is offline
    Regular Coder ubh's Avatar
    Join Date
    Apr 2008
    Location
    Portland, Oregon U.S.A.
    Posts
    443
    Thanks
    108
    Thanked 15 Times in 14 Posts
    allowed image types are 'jpg','gif' and 'png'

  • #4
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    No idea if this is helpful for you, but just incase. The vars you supply to the function should be fairly self explanatory. Easy enough to strip down if you want to minimise the code too.

    Code:
    function image_resize($input, $width, $height, $delete = false)
    {
            list($currwidth, $currheight, $type) = @getimagesize($input);
    
            if (empty($currwidth) || empty($currheight) || $type > '3')
            {
                    return false;
            }
    
            $ratio = $currwidth/$currheight;
    
            if ($currwidth < $width && $currheight < $height)
            {
                    if (is_file($input))
                    {
                            @copy($input, $input.'_new');
                            return $input.'_new';
                    }
                    else
                    {
                            return false;
                    }
            }
            else
            {
                    $output = $input.time();
    
                    if ($width/$height > $ratio)
                    {
                            $width = $height*$ratio;
                    }
                    else
                    {
                            $height = $width/$ratio;
                    }
    
                    $zwidth = $width;
                    $zheight = $height;
    
                    $dimg = imagecreatetruecolor($zwidth, $zheight);
                    $background = imagecolorallocate($dimg, 255, 255, 255);
                    imagefill($dimg, 0, 0, $background);
    
                    if ($type == '1')
                    {
                            $simg = imagecreatefromgif($input);
                            imagecopyresampled($dimg, $simg, 0, 0, 0, 0, $zwidth, $zheight, $currwidth, $currheight);
                            imagegif($dimg, $output);
                    }
                    else if ($type == '2')
                    {
                            $simg = imagecreatefromjpeg($input);
                            imagecopyresampled($dimg, $simg, 0, 0, 0, 0, $zwidth, $zheight, $currwidth, $currheight);
                            imagejpeg($dimg, $output, 85);
                    }
                    else if ($type == '3')
                    {
                            $simg = imagecreatefrompng($input);
                            imagecopyresampled($dimg, $simg, 0, 0, 0, 0, $zwidth, $zheight, $currwidth, $currheight);
                            imagepng($dimg, $output);
                    }
    
                    @imagedestroy($dimg);
                    @imagedestroy($simg);
    
                    if (is_file($output))
                    {
                            if ($delete)
                            {
                                    unlink($input);
                            }
                            return $output;
                    }
                    else
                    {
                            return false;
                    }
            }
    }
    Last edited by MattF; 09-22-2009 at 08:29 PM.

  • #5
    New Coder
    Join Date
    Sep 2009
    Location
    Pakistan
    Posts
    14
    Thanks
    0
    Thanked 2 Times in 2 Posts
    If you can bother altering a few lines of code, try the following comprehensive code example:

    Resize Images Using PHP and GD Library

    The code sample contains a function called "generate_image_thumbnail( )" which accepts source path of an existing image and destination path for the resized image. You can use the above function and specify destination image path = source image path; that should overwrite the original image.


  •  

    Posting Permissions

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