php 图片缩略图生成代码(支持png透明)

WBOY
Lepaskan: 2016-07-25 08:52:19
asal
1383 orang telah melayarinya
  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;
  26. $this->resize_height = $height;
  27. $this->cut = $isCut;
  28. //图片的类型
  29. $this->type = strtolower(substr(strrchr($this->srcimg,"."),1));

  30. //初始化图象

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

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

  83. private function initi_img() {
  84. if($this->type=="jpg") {
  85. $this->im = imagecreatefromjpeg($this->srcimg);
  86. }
  87. if($this->type=="gif") {
  88. $this->im = imagecreatefromgif($this->srcimg);
  89. }
  90. if($this->type=="png") {
  91. $this->im = imagecreatefrompng($this->srcimg);
  92. }
  93. }
  94. //图象目标地址

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

  98. $name_length = $full_length-$type_length;
  99. $name = substr($this->srcimg,0,$name_length-1);
  100. $this->dstimg = $dstpath;
  101. }
  102. }
  103. ?>
复制代码

使用时直接调用类的构造函数即可,构造函数: $resizeimage = new resizeimage($imgPath, $width, $height, $isCut, $savePath);

参数 $imgPath:原图片地址 $width:缩略图宽 $height:缩略图高 $isCut:是否裁剪,bool值 $savePath:缩略图地址(可以跟原图片地址相同)

例子:

  1. include "ResizeImage.php";

  2. //jpg

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

  5. $pngResize = new ResizeImage("img/test_1024_746.png", 320, 240, false, "img/test_320_240.png");
  6. ?>
复制代码

效果: php生成缩略图1 php生成缩略图2



sumber:php.cn
Kenyataan Laman Web ini
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
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan