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

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

WBOY
發布: 2016-07-25 08:47:29
原創
821 人瀏覽過
PHP 新增浮水印 & 比例縮圖 & 固定高度 & 固定寬度 類別。
使用foreach 迴圈處理的時候,需要 sleep 設定一個時間 或 依照處理後的回傳值 ,否則處理不完。

下載: http://pan.baidu.com/s/1ntKAfFF
  1. //檔名:image_process.class.php
  2. class Image_process{
  3. public $source;//原圖
  4. public $> public $ source_width;//寬
  5. public $source_height;//高
  6. public $source_type_id;
  7. public $orign_name;
  8. public $orign_dirname;
  9. //傳入圖片路徑
  10. public $orign_dirname;
  11. //傳入圖片路徑
  12. public $orign_dirname;
  13. //傳入圖片路徑 $this->typeList = array(1=>'gif',2=>'jpg',3=>'png');
  14. $ginfo = getimagesize($source) ;
  15. $this->source_width = $ginfo[0];
  16. $this->source_height = $ginfo[1];
  17. $this->source_type_id= $ginfo[2];
  18. $ this->orign_url = $source;
  19. $this->orign_name = basename($source);
  20. $this->orign_dirname = dirname($source);
  21. }
  22. //判斷並處理,回傳PHP可辨識編碼
  23. public function judgeType($type,$source){
  24. if($type==1){
  25. return ImageCreateFromGIF($source);//gif
  26. }else if($type==2){
  27. return ImageCreateFromJPEG($source);//jpg
  28. }else if($type==3){
  29. return ImageCreateFromPNG($source);/ /png
  30. }else{
  31. return false;
  32. }
  33. }
  34. //產生水印圖
  35. public function watermarkImage($logo){ $dinfo getimagesize($logo);
  36. $logo_width = $linfo[0];
  37. $logo_height = $linfo[1];
  38. $logo_type_id = $linfo[2];
  39. $sourceHandle = $thissource_id = $linfo[2];
  40. $sourceHandle = $thissourceHandle ->judgeType($this->source_type_id,$this->orign_url);
  41. $logoHandle = $this->judgeType($logo_type_id,$logo);
  42. if( !$sourceHandle || ! $logoHandle ){
  43. return false;
  44. }
  45. $x = $this->source_width - $logo_width;
  46. $y = $this->source_height- $logo_height-
  47. ImageCopy($sourceHandle,$logoHandle,$x,$y,0,0,$logo_width,$logo_width) or die("fail to combine");
  48. $newPic = $this->orign_dirname .'water_'. time().'.'. $this->typeList[$this->source_type_id];
  49. if( $this->saveImage($sourceHandle,$newPic)){
  50. imagedestroy($sourceHandle );
  51. imagedestroy($logoHandle);
  52. }
  53. }
  54. // fix 寬度
  55. // height = true 固頂高度
  56. // width = true 固頂寬度
  57. public function fixSizeImage($width,$height){
  58. if( $width > $this->source_width) $this->source_width;
  59. if( $height > $this->source_height ) $source_height ) $source_height ) $source_height ) $source_height ) $source this->source_height;
  60. if( $width === false){
  61. $width = floor($this->source_width / ($this->source_height / $height));
  62. }
  63. if( $height === false){
  64. $height = floor($this->source_height / ($this->source_width / $width));
  65. }
  66. $this->tinyImage( $width,$height);
  67. }
  68. //比例縮放
  69. // $scale 縮放比例
  70. public function scaleImage($scale){
  71. $width = floor($ this->source_width * $scale);
  72. $height = floor($this->source_height * $scale);
  73. $this->tinyImage($width,$height);
  74. }
  75. //建立略縮圖
  76. private function tinyImage($width,$height){
  77. $tinyImage = imagecreatetruecolor($width, $height );
  78. $handle = $this->jType( $this->source_type_id,$this->orign_url);
  79. if(function_exists('imagecopyresampled')){
  80. imagecopyresampled($tinyImage,$handle,0,0,0,0,$he,$height ,$this->source_width,$this->source_height);
  81. }else{
  82. imagecopyresized($tinyImage,$handle,0,0,0,0,$width,$height,$this->source_width ,$this->source_height);
  83. }
  84. $newPic = time().'_'.$width.'_'.$height.'.'. $this->typeList[$ this->source_type_id];
  85. $newPic = $this->orign_dirname .'thumb_'. $newPic;
  86. if( $this->saveImage($tinyImage,$newPic)){
  87. imagedeystro($ tinyImage);
  88. imagedestroy($handle);
  89. }
  90. }
  91. //儲存圖片
  92. private function saveImage($image,$url){
  93. private function saveImage($image,$url){
if; ($image,$url)){
return true; } }
}複製程式碼
  1. //使用
  2. include('image_process.class.php');
  3. $m = array(
  4. ' D:myspacetestimage_process1.jpg',
  5. 'D:myspacetestimage_process2.jpg',
  6. 'D:myspacetestimage_process2.jpg',
  7. 'D:myspacetestimage_process3.jpg',
  8. 'D:myspacetestimage_process4.m'$logo = 'D:myspacetestimage_processlogo.png';
  9. foreach( $m as $item){
  10. $s = new Image_process( $item ) ;
  11. $s->watermarkImage($logo);
  12. $s->scaleImage(0.8);
  13. $s->fixSizeImage(200,false);
  14. sleep(1) ;
  15. ;
  16. }
複製程式碼

來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板