Heim  >  Artikel  >  Backend-Entwicklung  >  PHP 添加水印 & 比例缩略图 & 固定高度 & 固定宽度 类

PHP 添加水印 & 比例缩略图 & 固定高度 & 固定宽度 类

WBOY
WBOYOriginal
2016-07-25 08:47:29805Durchsuche
PHP 添加水印 & 比例缩略图 & 固定高度 & 固定宽度 类。
使用foreach 循环处理的时候,需要 sleep 设定一个时间 或者 按照处理后的返回值 ,否则处理不完。

下载: http://pan.baidu.com/s/1ntKAfFF
  1. //文件名:image_process.class.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 watermarkImage($logo){
  34. $linfo = getimagesize($logo);
  35. $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;
  44. $y = $this->source_height- $logo_height;
  45. ImageCopy($sourceHandle,$logoHandle,$x,$y,0,0,$logo_width,$logo_width) or die("fail to combine");
  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. // fix 宽度
  53. // height = true 固顶高度
  54. // width = true 固顶宽度
  55. public function fixSizeImage($width,$height){
  56. if( $width > $this->source_width) $this->source_width;
  57. if( $height > $this->source_height ) $this->source_height;
  58. if( $width === false){
  59. $width = floor($this->source_width / ($this->source_height / $height));
  60. }
  61. if( $height === false){
  62. $height = floor($this->source_height / ($this->source_width / $width));
  63. }
  64. $this->tinyImage($width,$height);
  65. }
  66. //比例缩放
  67. // $scale 缩放比例
  68. public function scaleImage($scale){
  69. $width = floor($this->source_width * $scale);
  70. $height = floor($this->source_height * $scale);
  71. $this->tinyImage($width,$height);
  72. }
  73. //创建略缩图
  74. private function tinyImage($width,$height){
  75. $tinyImage = imagecreatetruecolor($width, $height );
  76. $handle = $this->judgeType($this->source_type_id,$this->orign_url);
  77. if(function_exists('imagecopyresampled')){
  78. imagecopyresampled($tinyImage,$handle,0,0,0,0,$width,$height,$this->source_width,$this->source_height);
  79. }else{
  80. imagecopyresized($tinyImage,$handle,0,0,0,0,$width,$height,$this->source_width,$this->source_height);
  81. }
  82. $newPic = time().'_'.$width.'_'.$height.'.'. $this->typeList[$this->source_type_id];
  83. $newPic = $this->orign_dirname .'\thumb_'. $newPic;
  84. if( $this->saveImage($tinyImage,$newPic)){
  85. imagedestroy($tinyImage);
  86. imagedestroy($handle);
  87. }
  88. }
  89. //保存图片
  90. private function saveImage($image,$url){
  91. if(ImageJpeg($image,$url)){
  92. return true;
  93. }
  94. }
  95. }
复制代码
  1. //使用
  2. include('image_process.class.php');
  3. $m = array(
  4. 'D:\myspace\test\image_process\1.jpg',
  5. 'D:\myspace\test\image_process\2.jpg',
  6. 'D:\myspace\test\image_process\3.jpg',
  7. 'D:\myspace\test\image_process\4.jpg'
  8. );
  9. $img = 'D:\myspace\test\image_process\1.jpg';
  10. $logo = 'D:\myspace\test\image_process\logo.png';
  11. foreach( $m as $item){
  12. $s = new Image_process( $item );
  13. $s->watermarkImage($logo);
  14. $s->scaleImage(0.8);
  15. $s->fixSizeImage(200,false);
  16. sleep(1);
  17. }
复制代码


Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Vorheriger Artikel:将数组保存为文件 Nächster Artikel:派生类数据库 单利模式