php生成缩略图类,支持自定义高和宽,还支持按高和宽截图

php生成缩略图类,支持自定义高和宽,还支持按高和宽截图,实例代码如下:

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