首頁  >  文章  >  後端開發  >  php 圖片縮圖產生程式碼(支援png透明)

php 圖片縮圖產生程式碼(支援png透明)

WBOY
WBOY原創
2016-07-25 08:52:191392瀏覽
效果:
  1. /*

  2. * desc: Resize Image(png, jpg, gif)
  3. */
  4. class ResizeImage {
  5. //圖片類型
  6. private $type;
  7. //實際寬度
  8. private $width;
  9. //實際高度
  10. private $height;
  11. //改變後的寬度
  12. private $resize_width;
  13. //改變後的高度
  14. private $resize_height;
  15. //是否裁圖
  16. private $cut;
  17. //源源圖象
  18. private $srcimg;
  19. //目標圖象位址
  20. private $dstimg;
  21. //暫時建立的圖象
  22. private $im;
  23. function __construct($imgPath, $width, $height, $isCut, $savePath) {
  24. $this->srcimg = $imgPath;
  25. $this->resize_width = $width;resize_height = $height;
  26. $this->cut = $isCut;
  27. //圖片的類型
  28. $this->type = strtolower(substr(strrchr($ this->srcimg,"."),1));

  29. //初始化圖象

  30. $this->initi_img();
  31. //目標圖象位址
  32. $this -> dst_img($savePath);
  33. //--
  34. $this->width = imagesx($this->im);
  35. $this->height = imagesy($this ->im);
  36. //生成圖象
  37. $this->newimg();
  38. ImageDestroy ($this->im);
  39. }
  40. private function newimg() {

  41. //改變後的圖象的比例
  42. $resize_ratio = ($this->resize_width)/($this->resize_height);
  43. //實際圖象的比例
  44. $ratio = ($this->width)/($this->height);
  45. if($this->cut) {
  46. //裁圖
  47. $newimg = imagecreatetruecolor($ this->resize_width,$this->resize_height);
  48. if($this->type=="png") {
  49. imagefill($newimg, 0, 0, imagecolorallocatealpha($newimg, 0, 0, 0, 127));
  50. }
  51. if($ratio>=$resize_ratio) {
  52. //高度優先
  53. imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, $this->resize_width,$this->resize_height, (($this->height)*$resize_ratio), $this->height);
  54. } else {
  55. //寬度優先
  56. imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, $this->resize_width, $this->resize_height, $this->width, (($this->width)/$resize_ratio)) ;
  57. }
  58. } else {
  59. //不裁圖
  60. if($ratio>=$resize_ratio) {
  61. $newimg = imagecreatetruecolor($this->resize_width,($new-this >resize_width)/$ratio);
  62. if($this->type=="png") {
  63. imagefill($newimg, 0, 0, imagecolorallocatealpha($newimg, 0, 0, 0, 127) );
  64. }
  65. imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, $this->resize_width, ($this->resize_width)/$ratio, $this->width , $this->height);
  66. } else {
  67. $newimg = imagecreatetruecolor(($this->resize_height)*$ratio,$this->resize_height);
  68. if($this->type =="png") {
  69. imagefill($newimg, 0, 0, imagecolorallocatealpha($newimg, 0, 0, 0, 127));
  70. }
  71. imagecopyresampled($$newimg,this-1> im, 0, 0, 0, 0, ($this->resize_height)*$ratio, $this->resize_height, $this->width, $this->height);
  72. }
  73. }
  74. if($this->type=="png") {
  75. imagesavealpha($newimg, true);
  76. imagepng ($newimg,$this->dstimg);
  77. } else {
  78. imagejpeg ($newimg,$this->dstimg);
  79. }
  80. }
  81. //初始化圖象

  82. private function initi_img() {
  83. if($ this->type=="jpg") {
  84. $this->im = imagecreatefromjpeg($this->srcimg);
  85. }
  86. if($this->type=="gif") {
  87. $this->im = imagecreatefromgif($this->srcimg);
  88. }
  89. if($this->type=="png") {
  90. $this->im = imagecreatefrompng( $this->srcimg);
  91. }
  92. }
  93. //圖像目標位址

  94. private function dst_img($dstpath) {
  95. $full_length = strlen( $this->srcimg);
  96. $type_length = strlen($this->type);

  97. $name_length = $full_length-$type_length;
  98. $name = substr($this->srcimg,0,$name_length-1);
  99. $this->dstimg = $dstpath;
  100. }
  101. }
  102. ?> p>
複製程式碼

使用時直接呼叫類別的建構子即可,建構子: $resizeimage = new resizeimage($imgPath, $width, $height, $isCut, $savePath);

參數 $imgPath:原圖片地址 $width:縮圖寬 $height:縮圖高 $isCut:是否裁剪,bool值 $savePath:縮圖位址(可以跟原圖片地址相同)

例:

  1. include "ResizeImage.php";

  2. //jpgg

  3. $jpgResize = new ResizeImage("img/test_1920_1200.jpg", 320, 240, false, "img/test_320_240.jpg");
  4. //png = new ResizeImage("img/test_1024_746.png", 320, 240, false, "img/test_320_240.png");

  5. ?>

php 圖片縮圖產生程式碼(支援png透明) php 圖片縮圖產生程式碼(支援png透明)



陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn