Rumah  >  Artikel  >  pembangunan bahagian belakang  >  PHP实现对png图像进行缩放的方法(支持透明背景)

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

WBOY
WBOYasal
2016-07-25 08:44:411064semak imbas

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

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

PHP, png


Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn