Comments for Resizing an Image Using PHP and the GD2 Library
13 comments for this article.
Subscribe to the comments for this article
Posted: 2007-12-05 07:53:28 by lobo235 - Netlobo Staff Member
To change the quality of the image look for this line toward the bottom of the function:
if( !imagejpeg( $img_dst, $destfile, 90 ) )
The number 90 indicates the quality of the JPEG image that will be saved. The possible values that you could replace it with are 0-100 with 100 being the highest quality.
You could also add a $quality parameter to the function and pass in the quality of the image to be saved. In this case you would just replace the number 90 with the variable $quality. If you want to implement a default value if the $quality is not passed then the function declaration would look like this:
function resampimagejpg( $forcedwidth, $forcedheight, $sourcefile, $destfile, $quality = 90 )
I hope this helps!
if( !imagejpeg( $img_dst, $destfile, 90 ) )
The number 90 indicates the quality of the JPEG image that will be saved. The possible values that you could replace it with are 0-100 with 100 being the highest quality.
You could also add a $quality parameter to the function and pass in the quality of the image to be saved. In this case you would just replace the number 90 with the variable $quality. If you want to implement a default value if the $quality is not passed then the function declaration would look like this:
function resampimagejpg( $forcedwidth, $forcedheight, $sourcefile, $destfile, $quality = 90 )
I hope this helps!
Posted: 2007-12-05 10:20:47 by Dahool
yeah I've figured it out a few minutes later after I've made thee first post!!
Thank you a lot!!
=)
Thank you a lot!!
=)
Posted: 2007-12-10 12:15:25 by ahhoi
sounds very promising! can't get ir trunning though as I don't know how to "must pass in the forced width, forced height, source image, and destination image" in the right format (including path?). sorry, php newbie here ... thx fo any more comments!
Posted: 2007-12-10 12:18:11 by lobo235 - Netlobo Staff Member
The forced width and forced height are measured in pixels. So if you want the resized image to fit in a 640x640 box you would pass the number 640 for both parameters. The source image and destination image should be passed in with either the full path or the path relative to the php script.
Are you getting a specific error when you try to use the function?
Are you getting a specific error when you try to use the function?
Posted: 2008-09-06 14:59:15 by Vlad
Thank you very much, it works just fine!
Posted: 2008-09-16 09:16:04 by Robert
Hello,
Thanks for all of the info. Very helpful as I am new to PHP.
would like to use the resize code above with the GD library but I want to Replace the image with the new resized one.
How might I alter your function above?
Thanks in advance
Robert
Thanks for all of the info. Very helpful as I am new to PHP.
would like to use the resize code above with the GD library but I want to Replace the image with the new resized one.
How might I alter your function above?
Thanks in advance
Robert
Posted: 2008-11-06 14:25:44 by Mneil
Perfect! Added a little functionality to my soon to come blog. Thanks for the script!
Posted: 2009-11-18 22:34:42 by frann
This is perfect - but what if the image to be resized is a gif or a png?
Posted: 2009-11-18 22:35:54 by lobo235 - Netlobo Staff Member
It would be similar to the function to resize jpgs but you would need to use different functions to read the png/gif image in before resizing it.
Posted: 2010-03-11 07:50:42 by Hugo
The same source with some refactoring :
My 2 cents ;)
function imageResize($source, $dest, $maxWidth, $maxHeight)
{
$is = getimagesize($source);
$fw = ($is[0] >= $is[1]) ? $maxWidth : $maxHeight;
$fh = ($is[0] >= $is[1]) ? $maxHeight : $maxWidth;
$orientation = ($is[0] >= $is[1]) ? 0 : 1;
$iw = $is[0];
$ih = $is[1];
$t = 2;
if ($is[0] > $fw || $is[1] > $fh)
{
if ($is[0] - $fw >= $is[1] - $fh)
{
$iw = $fw;
$ih = ($fw / $is[0]) * $is[1];
}
else
{
$ih = $fh;
$iw = ($ih / $is[1]) * $is[0];
}
$t = 1;
}
switch ($t)
{
case 1:
$src = imagecreatefromjpeg($source);
$dst = imagecreatetruecolor( $iw, $ih );
imagecopyresampled($dst, $src, 0, 0, 0, 0, $iw, $ih, $is[0], $is[1]);
return imagejpeg($dst, $dest, 90);
break;
case 2:
return copy($source, $dest);
break;
default:
return false;
break;
}
}
My 2 cents ;)
Posted: 2010-04-24 07:44:23 by lcmichal
thank you so much, great work :D very useful.
Posted: 2010-06-04 01:57:17 by Steven
I can't get the function to work. Its not saving as jpeg. Eg. when I set test.jpg as the destination it gets saved as testjpg (without the dot). How do I resolve this? Many thanks in advance
Subscribe to the comments for this article
Post your comment for the Resizing an Image Using PHP and the GD2 Library article:

Excelent resize Function!
Can you manage the quality of the image? like 66% when resized to 800 x 600?