首頁 >後端開發 >php教程 >PHP 新增浮水印 & 比例縮圖 & 固定高度 & 固定寬度 類別。

PHP 新增浮水印 & 比例縮圖 & 固定高度 & 固定寬度 類別。

WBOY
WBOY原創
2016-07-25 08:42:52904瀏覽
  1. //PHP 增加浮水印 & 比例縮圖 & 固定高度 & 固定寬度 類別。
  2. class Image_process{
  3. public $source; //原圖
  4. public $source_width; //原圖寬度
  5. public $source_height; //原圖高度
  6. public $source_type_id;
  7. public $orign_name;
  8. public $orign_dirname;
  9. //傳入原圖路徑
  10. public function __construct($source){
  11. $this->typeList = array(1=> 'gif',2=>'jpg',3=>'png');
  12. $ginfo = getimagesize($source);
  13. $this->source_width = $ginfo[0];
  14. $ this->source_height = $ginfo[1];
  15. $this->source_type_id = $ginfo[2];
  16. $this->orign_url = $source;
  17. $this->orign_name = basename($ source);
  18. $this->orign_dirname = dirname($source);
  19. }
  20. //判斷圖片的檔案的格式,傳回PHP可辨識的編碼
  21. public function judgeType( $type,$source){
  22. if($type == 1){
  23. return imagecreatefromgif($source); //gif
  24. }else if($type == 2){
  25. return imagecreatefromjpeg($source); //jpg
  26. }else if($type == 3){
  27. return imagecreatefrompng($source); //png
  28. }else{
  29. return false;
  30. }
  31. }
  32. //產生浮水印圖片
  33. public function waterMakeImage($logo){
  34. $linfo = getimagesize($logo);
  35. $logo_width = $logo_width = $linfo[0 ];
  36. $logo_height = $linfo[1];
  37. $logo_type_id = $linfo[2];
  38. $sourceHandle = $this->judgeType($this->source_type_id,$this->orign_url) ;
  39. $logoHandle = $this->judgeType($logo_type_id,$logo);
  40. if(!$sourceHandle || !$logoHandle){
  41. return false;
  42. }
  43. $x = ($this->source_width - $logo_width)/2;
  44. $y = ($this->source_height - $logo_height)/2;
  45. imagecopy($sourceHandle,$logoHandle,$x,$y, 0,0,$logo_width,$logo_height);
  46. $newPic = $this->orign_dirname.'water_'.time().'.'.$this->typeList[$this->source_type_id];
  47. if($this->saveImage($sourceHandle,$newPic)){
  48. imagedestroy($sourceHandle);
  49. imagedestroy($logoHandle);
  50. }
  51. }
  52. 🎜> / /固定高度寬度
  53. public function fixSizeImage($width,$height){
  54. if($width > $this->source_width) $this->source_width;
  55. if($height > $this-> source_height) $this->source_height;
  56. if($width === false){
  57. $width = floor($this->source_width / ($this->source_height / $height));
  58. }
  59. if($height === false){
  60. $height = floor($this->source_height / ($this->source_width / $width));
  61. }
  62. $this- >tinyImage($width,$height);
  63. }
  64. //等比例縮放圖片
  65. public function scaleImage($scale){
  66. $width = floor($this->source_widthth-> * $scale);
  67. $height = floor($this->source_height * $scale);
  68. $this->tinyImage($width, $height);
  69. }
  70. / /建立縮圖
  71. public function tinyImage($width,$height){
  72. $tinyImage = imagecreatetruecolor($width,$height);
  73. $handle = $this->judgeType($this->source_type_id ,$this->orign_url);
  74. if(function_exists('imagecopyresampled')){
  75. imagecopyresampled($tinyImage, $handle, 0, 0, 0, 0, $width, $height, $width, $height, $widththis- source_width, $this->source_height);
  76. }else{
  77. imagecopyresized($tinyImage, $handle, 0, 0, 0, 0, $width, $height, $this->source_width, $this-> source_height);
  78. }
  79. $newPic = $this->orign_dirname.'thumb_'.time().'_'.$width."_".$height.".".$this->typeList [$this->source_type_id];
  80. if($this->saveImage($tinyImage,$newPic)){
  81. imagedestroy($tinyImage);
  82. imagedestroy($handle);
  83. }
  84. }
  85. }
  86. //儲存圖片
  87. private function saveImage($image,$url){
  88. if(imagejpeg($image,$url)){
  89. return true;
  90. }
  91. }
  92. }
  93. $imgHandle = new Image_process('D:AppServwwwtestgetimg14061907445601.jpg');
  94. //$imgHandle->waterMakecmteststesthedMakec.
  95. //$imgHandle->fixSizeImage(200,150); //固定長度圖片
  96. $imgHandle->scaleImage(0.2); //等比例縮放
?>
複製程式碼
PHP、amp


陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn