note 77878 deleted from function.imagecopyresampled by nlopess

From: Date: Sat, 24 Nov 2007 11:31:16 +0000
Subject: note 77878 deleted from function.imagecopyresampled by nlopess
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-132534@lists.php.net to get a copy of this message
Note Submitter: madfancier at NOSPAM dot gmail dot com ---- Hello, If you're looking for a function that can actually do all kinds of image resizing, support png, gif, jpeg, be agnostic to resize directions (upscaling and downscaling), support proportionality and absence of it, support one-dimension constraints (width or height), and preserve transparency, don't look any further. : ) Warning - this function replaces the passed-in file with the resized file with the same name and format. Modify as necessary. You can find more information about this function's usage and features in this article on my blog: http://www.mediumexposure.com/node/19 <?php /* * Written by Maxim Chernyak (mx_) * * Please visit http://www.mediumexposure.com for more PHP stuff. * If you have any criticism, suggestions, improvements, or you simply liked the function, I would appreciate if you dropped me a note on my blog (mentioned above). * * Feel free to use anywhere, anyhow. : ) * */ function smart_resize_image( $file, $width = 0, $height = 0, $proportional = false, $use_linux_command = 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) { $proportion = $width_old / $height_old; if ( $width > $height && $height != 0) { $final_height = $height; $final_width = $final_height * $proportion; } elseif ( $width < $height && $width != 0) { $final_width = $width; $final_height = $final_width / $proportion; } elseif ( $width == 0 ) { $final_height = $height; $final_width = $final_height * $proportion; } elseif ( $height == 0) { $final_width = $width; $final_height = $final_width / $proportion; } else { $final_width = $width; $final_height = $height; } } 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 ); imagecolortransparent($image_resized, imagecolorallocate($image_resized, 0, 0, 0) ); imagealphablending($image_resized, false); imagesavealpha($image_resized, true); imagecopyresampled($image_resized, $image, 0, 0, 0, 0, $final_width, $final_height, $width_old, $height_old); if ( $use_linux_command ) exec('rm '.$file); else @unlink($file); switch ( $info[2] ) { case IMAGETYPE_GIF: imagegif($image_resized, $file); break; case IMAGETYPE_JPEG: imagejpeg($image_resized, $file); break; case IMAGETYPE_PNG: imagepng($image_resized, $file); break; default: return false; } return true; } ?>

« previous php.notes (#132534) next »