国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > PHP > 正文

PHP實現對png圖像進行縮放的方法(支持透明背景)

2024-05-04 23:37:49
字體:
來源:轉載
供稿:網友

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


希望本文所述對大家的php程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 大冶市| 弋阳县| 陆良县| 桃江县| 宁夏| 伊宁县| 麻栗坡县| 集安市| 眉山市| 肇庆市| 长岭县| 祥云县| 玉林市| 松江区| 吴旗县| 子洲县| 涟水县| 罗甸县| 资溪县| 拉孜县| 库伦旗| 鲁甸县| 铜山县| 文登市| 加查县| 邻水| 赫章县| 岗巴县| 永顺县| 龙州县| 庆云县| 鄂托克前旗| 中山市| 香格里拉县| 罗甸县| 通榆县| 吴堡县| 江陵县| 共和县| 云霄县| 铁岭市|