当前位置:首页 > PHP教程 > php图像处理 > 列表

PHP实现对png图像进行缩放的方法(支持透明背景)

发布:smiling 来源: PHP粉丝网  添加日期:2021-06-12 19:47:28 浏览: 评论:0 

这篇文章主要介绍了PHP实现对png图像进行缩放的方法(支持透明背景),可实现php针对png图像的缩放功能,且支持透明背景,具有一定参考借鉴价值,需要的朋友可以参考下。

本文实例讲述了PHP实现对png图像进行缩放的方法,分享给大家供大家参考,具体实现方法如下:

  1. function smart_resize_image( $file, $width = 0, $height = 0, $proportional = false, $output = 'file', $delete_original = true, $use_linux_commands = false ) 
  2. { 
  3.     if ( $height <= 0 && $width <= 0 ) { 
  4.       return false; 
  5.     } 
  6.     $info = getimagesize($file); 
  7.     $image = ''; 
  8.     $final_width = 0; 
  9.     $final_height = 0; 
  10.     list($width_old, $height_old) = $info; 
  11.     if ($proportional) { 
  12.       if ($width == 0) $factor = $height/$height_old; 
  13.       elseif ($height == 0) $factor = $width/$width_old; 
  14.       else $factor = min ( $width / $width_old, $height / $height_old);  
  15.       $final_width = round ($width_old * $factor); 
  16.       $final_height = round ($height_old * $factor); 
  17.     } 
  18.     else {     
  19.       $final_width = ( $width <= 0 ) ? $width_old : $width; 
  20.       $final_height = ( $height <= 0 ) ? $height_old : $height; 
  21.     } 
  22.     switch ($info[2] ) { 
  23.       case IMAGETYPE_GIF: 
  24.         $image = imagecreatefromgif($file); 
  25.       break; 
  26.       case IMAGETYPE_JPEG: 
  27.         $image = imagecreatefromjpeg($file); 
  28.       break; 
  29.       case IMAGETYPE_PNG: 
  30.         $image = imagecreatefrompng($file); 
  31.       break; 
  32.       default: 
  33.         return false; 
  34.     } 
  35.     $image_resized = imagecreatetruecolor( $final_width, $final_height ); 
  36.     if ( ($info[2] == IMAGETYPE_GIF) || ($info[2] == IMAGETYPE_PNG) ) { 
  37.       $trnprt_indx = imagecolortransparent($image); 
  38.       // If we have a specific transparent color 
  39.       if ($trnprt_indx >= 0) { 
  40.         // Get the original image's transparent color's RGB values 
  41.         $trnprt_color  = imagecolorsforindex($image, $trnprt_indx); 
  42.         // Allocate the same color in the new image resource 
  43.         $trnprt_indx  = imagecolorallocate($image_resized, $trnprt_color['red'], $trnprt_color['green'], $trnprt_color['blue']); 
  44.         // Completely fill the background of the new image with allocated color. 
  45.         imagefill($image_resized, 0, 0, $trnprt_indx); 
  46.         // Set the background color for new image to transparent 
  47.         imagecolortransparent($image_resized, $trnprt_indx); 
  48.       } 
  49.       // Always make a transparent background color for PNGs that don't have one allocated already 
  50.       elseif ($info[2] == IMAGETYPE_PNG) { 
  51.         // Turn off transparency blending (temporarily) 
  52.         imagealphablending($image_resized, false); 
  53.         // Create a new transparent color for image 
  54.         $color = imagecolorallocatealpha($image_resized, 0, 0, 0, 127); 
  55.         // Completely fill the background of the new image with allocated color. 
  56.         imagefill($image_resized, 0, 0, $color); 
  57.         // Restore transparency blending 
  58.         imagesavealpha($image_resized, true); 
  59.       } 
  60.     } 
  61.     imagecopyresampled($image_resized, $image, 0, 0, 0, 0, $final_width, $final_height, $width_old, $height_old); 
  62.     if ( $delete_original ) { 
  63.       if ( $use_linux_commands ) 
  64.         exec('rm '.$file); 
  65.       else 
  66.         @unlink($file); 
  67.     } 
  68.     switch ( strtolower($output) ) { 
  69.       case 'browser': 
  70.         $mime = image_type_to_mime_type($info[2]); 
  71.         header("Content-type: $mime"); 
  72.         $output = NULL; 
  73.       break; 
  74.       case 'file': 
  75.         $output = $file; 
  76.       break; 
  77.       case 'return': 
  78.         return $image_resized; 
  79.       break; 
  80.       default: 
  81.       break; 
  82.     } 
  83.     switch ($info[2] ) { 
  84.       case IMAGETYPE_GIF: 
  85.         imagegif($image_resized, $output); 
  86.       break; 
  87.       case IMAGETYPE_JPEG: 
  88.         imagejpeg($image_resized, $output); 
  89.       break; 
  90.       case IMAGETYPE_PNG: 
  91.         imagepng($image_resized, $output); 
  92.       break; 
  93.       default: 
  94.         return false; 
  95.     } 
  96.     return true; 
  97. } 

希望本文所述对大家的php程序设计有所帮助。

Tags: png图像缩放

分享到: