国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > 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. ?> 

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 芦山县| 外汇| 若羌县| 教育| 色达县| 沅江市| 惠东县| 拜泉县| 水城县| 泾川县| 新蔡县| 古蔺县| 阳山县| 若尔盖县| 九龙县| 平和县| 涿州市| 博乐市| 汉川市| 乐都县| 潢川县| 濮阳县| 同心县| 永福县| 阳朔县| 云阳县| 海口市| 都江堰市| 榕江县| 长沙市| 阳春市| 金堂县| 福清市| 远安县| 山西省| 丰城市| 安阳市| 建阳市| 东海县| 海宁市| 闽清县|