This article mainly introduced PHP implementation of the PNG image scaling method (support transparent background), PHP for PNG image to achieve the zoom function, and support transparent background, with a certain reference value. We hope to help you.
The implementation method is as follows:
function Smart_resize_image ($file, $width = 0, $height = 0, $proportional = false, $output = ' file ', $delete _original = t Rue, $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 _col or[' 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 has one allocated already elseif ($info [2] = = IMA Getype_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;
Related recommendations:
PHP image operation class, support to generate thumbnails, add watermarks, upload thumbnails
Very strong PHP image processing class
How to implement PHP thumbnail image