首页 > 开发 > 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. 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.  
  24. function resizeimage($img$wid$hei,$c,$dstpath
  25. $this->srcimg = $img
  26. $this->resize_width = $wid
  27. $this->resize_height = $hei
  28. $this->cut = $c
  29. //图片的类型 
  30.  
  31. $this->type = strtolower(substr(strrchr($this->srcimg,"."),1)); 
  32.  
  33. //初始化图象 
  34. $this->initi_img(); 
  35. //目标图象地址 
  36. $this -> dst_img($dstpath); 
  37. //-- 
  38. $this->width = imagesx($this->im); 
  39. $this->height = imagesy($this->im); 
  40. //生成图象 
  41. $this->newimg(); 
  42. ImageDestroy ($this->im); 
  43. function newimg() 
  44. //改变后的图象的比例 
  45. $resize_ratio = ($this->resize_width)/($this->resize_height); 
  46. //实际图象的比例 
  47. $ratio = ($this->width)/($this->height); 
  48. if(($this->cut)=="1"
  49. //裁图 
  50. if($ratio>=$resize_ratio
  51. //高度优先 
  52. $newimg = imagecreatetruecolor($this->resize_width,$this->resize_height); 
  53. imagecopyresampled($newimg$this->im, 0, 0, 0, 0, $this->resize_width,$this->resize_height, (($this->height)*$resize_ratio), $this->height); 
  54. ImageJpeg ($newimg,$this->dstimg); 
  55. if($ratio<$resize_ratio
  56. //宽度优先 
  57. $newimg = imagecreatetruecolor($this->resize_width,$this->resize_height); 
  58. imagecopyresampled($newimg$this->im, 0, 0, 0, 0, $this->resize_width, $this->resize_height, $this->width, (($this->width)/$resize_ratio)); 
  59. ImageJpeg ($newimg,$this->dstimg); 
  60. else 
  61. //不裁图 
  62. if($ratio>=$resize_ratio
  63. $newimg = imagecreatetruecolor($this->resize_width,($this->resize_width)/$ratio); 
  64. imagecopyresampled($newimg$this->im, 0, 0, 0, 0, $this->resize_width, ($this->resize_width)/$ratio$this->width, $this->height); 
  65. ImageJpeg ($newimg,$this->dstimg); 
  66. if($ratio<$resize_ratio
  67. $newimg = imagecreatetruecolor(($this->resize_height)*$ratio,$this->resize_height); 
  68. imagecopyresampled($newimg$this->im, 0, 0, 0, 0, ($this->resize_height)*$ratio$this->resize_height, $this->width, $this->height); 
  69. ImageJpeg ($newimg,$this->dstimg); 
  70. //初始化图象 
  71. function initi_img() 
  72. if($this->type=="jpg"
  73. $this->im = imagecreatefromjpeg($this->srcimg); 
  74. if($this->type=="gif"
  75. $this->im = imagecreatefromgif($this->srcimg); 
  76. if($this->type=="png"
  77. $this->im = imagecreatefrompng($this->srcimg); 
  78. //图象目标地址 
  79. function dst_img($dstpath
  80. $full_length = strlen($this->srcimg); 
  81.  
  82. $type_length = strlen($this->type); 
  83. $name_length = $full_length-$type_length
  84.  
  85.  
  86. $name = substr($this->srcimg,0,$name_length-1); 
  87. $this->dstimg = $dstpath
  88.  
  89.  
  90. //echo $this->dstimg; 
  91. ?> 

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