Update: this function is now available at github.com/maxim/smart_resize_image.
Fork away!
Should be easy to find, right? All you want is a function that resizes an image to constraints (doesn’t care if it should scale up or down), with possibility to select if you want to keep it proportional, and possibility to use either width or height as the constraint. Also, you want it to preserve transparency damn it! Surprisingly, I was unable to find a good function that does all that, so I decided to attempt writing it. This should do it.
Features/Usage:
-
If you pass width as 0 (zero) — this function will disregard width, and use height as constraint. Same vice versa.
If you set “proportional” to false – the function will simply stretch (or shrink) the image to its full constraints.
If one of the dimensions is set to zero, and proportional set to “false” – then the image will be forced to stretch or shrink the other dimension, and disregard the zeroed dimension (leave it the same).
If proportional is set to true – the image will resize to constraints proportionally, once again, with possibility to have either width or height set to zero.
The function can use either linux “rm” command, or php @unlink. Most probably you don’t need to ever use that flag, but on some setups – @unlink won’t work due to user access restrictions.
The function will simply replace the file that you give it, with the resized file.
The function supports gif, png, and jpeg, and preserves the transparency of gif and png images.
Tested on GD version 2.0.28 only.
Essentially, everything happens just as logically expected. Please, if you see anything wrong, and you know ways to enhance, or optimize it, let me know.
Update: Thank you Joanne for finding a bug with proportional resize, and providing a great, more concise and clear solution to proportional resize. (see comments)
Update: The image transparency code was updated to a more thorough solution. See comments for info.
Update: There is a new parameter “output” which can be set to either
- “file” – overwrite the given file (default)
- “browser” – output image through http – with correct mime type
- “return” – return GD Library Image object
- or any filename of choice – save changed version to output path
Update: There is another new parameter “delete_original”. Speaks for itself.
Please, excuse a few code misalignments. Bug with coloring filter.
{
if ( $height <= 0 && $width <= 0 ) {
return false;
}
$info = getimagesize($file);
$image = '';
$final_width = 0;
$final_height = 0;
list($width_old, $height_old) = $info;
if ($proportional) {
if ($width == 0) $factor = $height/$height_old;
elseif ($height == 0) $factor = $width/$width_old;
else $factor = min ( $width / $width_old, $height / $height_old);
$final_width = round ($width_old * $factor);
$final_height = round ($height_old * $factor);
}
else {
$final_width = ( $width <= 0 ) ? $width_old : $width;
$final_height = ( $height <= 0 ) ? $height_old : $height;
}
switch ( $info[2] ) {
case IMAGETYPE_GIF:
$image = imagecreatefromgif($file);
break;
case IMAGETYPE_JPEG:
$image = imagecreatefromjpeg($file);
break;
case IMAGETYPE_PNG:
$image = imagecreatefrompng($file);
break;
default:
return false;
}
$image_resized = imagecreatetruecolor( $final_width, $final_height );
if ( ($info[2] == IMAGETYPE_GIF) || ($info[2] == IMAGETYPE_PNG) ) {
$trnprt_indx = imagecolortransparent($image);
// If we have a specific transparent color
if ($trnprt_indx >= 0) {
// Get the original image's transparent color's RGB values
$trnprt_color = imagecolorsforindex($image, $trnprt_indx);
// Allocate the same color in the new image resource
$trnprt_indx = imagecolorallocate($image_resized, $trnprt_color['red'], $trnprt_color['green'], $trnprt_color['blue']);
// Completely fill the background of the new image with allocated color.
imagefill($image_resized, 0, 0, $trnprt_indx);
// Set the background color for new image to transparent
imagecolortransparent($image_resized, $trnprt_indx);
}
// Always make a transparent background color for PNGs that don't have one allocated already
elseif ($info[2] == IMAGETYPE_PNG) {
// Turn off transparency blending (temporarily)
imagealphablending($image_resized, false);
// Create a new transparent color for image
$color = imagecolorallocatealpha($image_resized, 0, 0, 0, 127);
// Completely fill the background of the new image with allocated color.
imagefill($image_resized, 0, 0, $color);
// Restore transparency blending
imagesavealpha($image_resized, true);
}
}
imagecopyresampled($image_resized, $image, 0, 0, 0, 0, $final_width, $final_height, $width_old, $height_old);
if ( $delete_original ) {
if ( $use_linux_commands )
exec('rm '.$file);
else
@unlink($file);
}
switch ( strtolower($output) ) {
case 'browser':
$mime = image_type_to_mime_type($info[2]);
header("Content-type: $mime");
$output = NULL;
break;
case 'file':
$output = $file;
break;
case 'return':
return $image_resized;
break;
default:
break;
}
switch ( $info[2] ) {
case IMAGETYPE_GIF:
imagegif($image_resized, $output);
break;
case IMAGETYPE_JPEG:
imagejpeg($image_resized, $output);
break;
case IMAGETYPE_PNG:
imagepng($image_resized, $output);
break;
default:
return false;
}
return true;
}
Thank you for providing some great code… However, after trying to resize some transparent gifs for the past few days I finally noticed from php.net that imagecreatetruecolor() does not work with gifs.
so I added
$image_resized = imagecreate( $final_width, $final_height );
else
$image_resized = imagecreatetruecolor( $final_width, $final_height );
If someone is able to get imagecreatetruecolor() to work on gifs let me know, but I keep getting a filled in background.
a second option for dealing with GIFs is to add
imagetruecolortopalette($image_resized, true, imagecolorstotal($image) );
after
imagecolortransparent( $image_resized, $trnprt_indx );
this will convert the truecolor image to a palette with the same number of colors as the original image.
FYI: GIFs are images based on the 256 color palette, imagecreatetruecolor() creates an image based on 1.6 million colors (or so).
My own re-size function was not properly handling transparency. I was able to integrate your transparency handling section into my function in just a couple minutes and have tested both gif and png.
Works absolutely perfect!
Hey you, sorry, but i’ve one question. If I want to save the resized image into an thumb folder like /thumbs/, where do I have to define the output path of the saved image?
Marvin
The parameter named
$outputaccepts any arbitrary filesystem paths.I was using the thumbnail resize solution from Sitepoint’s PHP Anthology (2nd edition), and I was able to integrate your transparency handling code into it easily. Thanks!
Struggled with inconsistent transparency-results on a resized PNG for two hours… Solved my problem in 5min after i found your routine/example… THANKS!
Excellent work. I am using your function (along with Aaron’s fix) and it works great.
Awesome stuff! Thanks!
Now to add a watermark/branding parameter ;)
This function is good and all, but I have been looking for 3 days now on how to fill the transparent portion with a specific color. As you can probably guess, I am not all that familiar with GD.
Thanks! It works like a champ!
Ok, so I’m uploading PNG files and they are resizing correctly but coming out with a black background instead of transparency. Is that due to the way the original PNG was created or is there something I need to tweek with your code. Unfortunately I don’t know a lot about working with images like this. Any help is greatly appreciated.
Thanks!
For those who get a black background, this works perfectly with images saved as PNG 24 rather than 8. If you still get a black background and you are merging the image returned from this function on to another image then heres the problem. It took me days to figure this out and I hope it saves someone else lots of time. imagecopymerge() does NOT preserve the transparency, that is what makes it a black background, not this function, instead use imagecopy() and it will work perfectly.
There are also 2 typos in this function if you get the version from GitHub, $trnprt_indx should be $transparency and $trnprt_color should be $transparent_color
If i’d like to resize my image with 50% ? How I manage that? $width = $width/2 –> doesn’t work..
Grts
[...] [...]