基于GD2图形库的PHP生成图片缩略图类代码分享

这篇文章主要介绍了基于GD2图形库的PHP生成图片缩略图类代码分享,本文直接给出实现代码和使用方法,需要的朋友可以参考下

要使用PHP生成图片缩略图,要保证你的PHP服务器安装了GD2图形库 使用一个类生成图片的缩略图

1.使用方法

$resizeimage = new resizeimage("图片源文件地址", "200", "100", "0","缩略图地址");

就只用上面的一句话,就能生成缩略图,其中,源文件和缩略图地址可以相同,200,100分别代表宽和高

2. 缩略图类代码

  1. //使用如下类就可以生成图片缩略图,
  2. <?php
  3. class resizeimage
  4. {
  5. //图片类型
  6. var $type;
  7. //实际宽度
  8. var $width;
  9. //实际高度
  10. var $height;
  11. //改变后的宽度
  12. var $resize_width;
  13. //改变后的高度
  14. var $resize_height;
  15. //是否裁图
  16. var $cut;
  17. //源图象
  18. var $srcimg;
  19. //目标图象地址
  20. var $dstimg;
  21. //临时创建的图象
  22. var $im;
  23. function resizeimage($img, $wid, $hei,$c,$dstpath)
  24. {
  25. $this->srcimg = $img;
  26. $this->resize_width = $wid;
  27. $this->resize_height = $hei;
  28. $this->cut = $c;
  29. //图片的类型
  30. $this->type = strtolower(substr(strrchr($this->srcimg,"."),1));
  31. //初始化图象
  32. $this->initi_img();
  33. //目标图象地址
  34. $this -> dst_img($dstpath);
  35. //--
  36. $this->width = imagesx($this->im);
  37. $this->height = imagesy($this->im);
  38. //生成图象
  39. $this->newimg();
  40. ImageDestroy ($this->im);
  41. }
  42. function newimg()
  43. {
  44. //改变后的图象的比例
  45. $resize_ratio = ($this->resize_width)/($this->resize_height);
  46. //实际图象的比例
  47. $ratio = ($this->width)/($this->height);
  48. if(($this->cut)=="1")
  49. //裁图
  50. {
  51. if($ratio>=$resize_ratio)
  52. //高度优先
  53. {
  54. $newimg = imagecreatetruecolor($this->resize_width,$this->resize_height);
  55. imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, $this->resize_width,$this->resize_height, (($this->height)*$resize_ratio), $this->height);
  56. ImageJpeg ($newimg,$this->dstimg);
  57. }
  58. if($ratio<$resize_ratio)
  59. //宽度优先
  60. {
  61. $newimg = imagecreatetruecolor($this->resize_width,$this->resize_height);
  62. imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, $this->resize_width, $this->resize_height, $this->width, (($this->width)/$resize_ratio));
  63. ImageJpeg ($newimg,$this->dstimg);
  64. }
  65. }
  66. else
  67. //不裁图
  68. {
  69. if($ratio>=$resize_ratio)
  70. {
  71. $newimg = imagecreatetruecolor($this->resize_width,($this->resize_width)/$ratio);
  72. imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, $this->resize_width, ($this->resize_width)/$ratio, $this->width, $this->height);
  73. ImageJpeg ($newimg,$this->dstimg);
  74. }
  75. if($ratio<$resize_ratio)
  76. {
  77. $newimg = imagecreatetruecolor(($this->resize_height)*$ratio,$this->resize_height);
  78. imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, ($this->resize_height)*$ratio, $this->resize_height, $this->width, $this->height);
  79. ImageJpeg ($newimg,$this->dstimg);
  80. }
  81. }
  82. }
  83. //初始化图象
  84. function initi_img()
  85. {
  86. if($this->type=="jpg")
  87. {
  88. $this->im = imagecreatefromjpeg($this->srcimg);
  89. }
  90. if($this->type=="gif")
  91. {
  92. $this->im = imagecreatefromgif($this->srcimg);
  93. }
  94. if($this->type=="png")
  95. {
  96. $this->im = imagecreatefrompng($this->srcimg);
  97. }
  98. }
  99. //图象目标地址
  100. function dst_img($dstpath)
  101. {
  102. $full_length = strlen($this->srcimg);
  103. $type_length = strlen($this->type);
  104. $name_length = $full_length-$type_length;
  105. $name = substr($this->srcimg,0,$name_length-1);
  106. $this->dstimg = $dstpath;
  107. //echo $this->dstimg;
  108. }
  109. }
  110. ?>