...

View Full Version : PHP GD Resize Image



ubh
09-22-2009, 05:31 PM
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...

mlseim
09-22-2009, 06:13 PM
Are all the images the same type, and what is the type .jpg, .gif?

ubh
09-22-2009, 06:22 PM
allowed image types are 'jpg','gif' and 'png'

MattF
09-22-2009, 07:26 PM
No idea if this is helpful for you, but just incase. :) The vars you supply to the function should be fairly self explanatory. :D Easy enough to strip down if you want to minimise the code too.



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;
}
}
}

schwarzenneger
09-29-2009, 07:39 AM
If you can bother altering a few lines of code, try the following comprehensive code example:

Resize Images Using PHP and GD Library (http://911-need-code-help.blogspot.com/2008/10/resize-images-using-phpgd-library.html)

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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum