首页 > 开发 > PHP > 正文

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

2024-05-04 23:31:02
字体:大 中 小
来源:转载
供稿:网友

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

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

1.使用方法

 

 
  1. $resizeimage = new resizeimage("图片源文件地址", "200", "100", "0","缩略图地址"); 
  2. //就只用上面的一句话,就能生成缩略图,其中,源文件和缩略图地址可以相同,200,100分别代表宽和高 

2. 缩略图类代码

 

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

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表