PHP scales png images to support transparent background
PHP scales png images to support transparent background
Function smart_resize_image ($ file, $ width = 0, $ height = 0, $ proportional = false, $ output = 'file', $ delete_original = true, $ use_linux_commands = false)
{
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;
}