免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1521 | 回复: 1
打印 上一主题 下一主题

PHP 添加水印 & 比例缩略图 & 固定高度 & 固定宽度 类 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2015-07-02 09:52 |只看该作者 |倒序浏览
PHP 添加水印 & 比例缩略图 & 固定高度 & 固定宽度 类。
使用foreach 循环处理的时候,需要 sleep 设定一个时间 或者 按照处理后的返回值 ,否则处理不完。

[PHP]代码
  1. //文件名:image_process.class.php
  2. <?php
  3. class Image_process{
  4.     public $source;//原图
  5.     public $source_width;//宽
  6.     public $source_height;//高
  7.     public $source_type_id;
  8.     public $orign_name;
  9.     public $orign_dirname;
  10.     //传入图片路径
  11.     public function __construct($source){
  12.         $this->typeList      = array(1=>'gif',2=>'jpg',3=>'png');
  13.         $ginfo               = getimagesize($source);
  14.         $this->source_width  = $ginfo[0];
  15.         $this->source_height = $ginfo[1];
  16.         $this->source_type_id= $ginfo[2];
  17.         $this->orign_url     = $source;
  18.         $this->orign_name    = basename($source);
  19.         $this->orign_dirname = dirname($source);
  20.     }

  21.     //判断并处理,返回PHP可识别编码
  22.     public function judgeType($type,$source){
  23.         if($type==1){
  24.             return ImageCreateFromGIF($source);//gif
  25.         }else if($type==2){
  26.             return ImageCreateFromJPEG($source);//jpg
  27.         }else if($type==3){
  28.             return ImageCreateFromPNG($source);//png
  29.         }else{
  30.             return false;
  31.         }
  32.     }

  33.     //生成水印图
  34.     public function watermarkImage($logo){
  35.         $linfo        = getimagesize($logo);
  36.         $logo_width   = $linfo[0];
  37.         $logo_height  = $linfo[1];
  38.         $logo_type_id = $linfo[2];
  39.         $sourceHandle = $this->judgeType($this->source_type_id,$this->orign_url);
  40.         $logoHandle   = $this->judgeType($logo_type_id,$logo);

  41.         if( !$sourceHandle || ! $logoHandle ){
  42.             return false;
  43.         }
  44.         $x = $this->source_width - $logo_width;
  45.         $y = $this->source_height- $logo_height;

  46.         ImageCopy($sourceHandle,$logoHandle,$x,$y,0,0,$logo_width,$logo_width) or die("fail to combine");
  47.         $newPic = $this->orign_dirname .'\water_'. time().'.'. $this->typeList[$this->source_type_id];

  48.         if( $this->saveImage($sourceHandle,$newPic)){
  49.             imagedestroy($sourceHandle);
  50.             imagedestroy($logoHandle);
  51.         }
  52.     }

  53.     // fix 宽度
  54.     // height = true 固顶高度
  55.     // width  = true 固顶宽度
  56.     public function fixSizeImage($width,$height){
  57.         if( $width > $this->source_width) $this->source_width;
  58.         if( $height > $this->source_height ) $this->source_height;
  59.         if( $width === false){
  60.             $width = floor($this->source_width / ($this->source_height / $height));
  61.         }
  62.         if( $height === false){
  63.             $height = floor($this->source_height / ($this->source_width / $width));
  64.         }
  65.         $this->tinyImage($width,$height);
  66.     }

  67.     //比例缩放
  68.     // $scale 缩放比例
  69.     public function scaleImage($scale){
  70.         $width  = floor($this->source_width * $scale);
  71.         $height = floor($this->source_height * $scale);
  72.         $this->tinyImage($width,$height);
  73.     }

  74.     //创建略缩图
  75.     private function tinyImage($width,$height){
  76.         $tinyImage = imagecreatetruecolor($width, $height );
  77.         $handle    = $this->judgeType($this->source_type_id,$this->orign_url);
  78.         if(function_exists('imagecopyresampled')){
  79.             imagecopyresampled($tinyImage,$handle,0,0,0,0,$width,$height,$this->source_width,$this->source_height);
  80.         }else{
  81.             imagecopyresized($tinyImage,$handle,0,0,0,0,$width,$height,$this->source_width,$this->source_height);
  82.         }

  83.         $newPic = time().'_'.$width.'_'.$height.'.'. $this->typeList[$this->source_type_id];
  84.         $newPic = $this->orign_dirname .'\thumb_'. $newPic;
  85.         if( $this->saveImage($tinyImage,$newPic)){
  86.             imagedestroy($tinyImage);
  87.             imagedestroy($handle);
  88.         }
  89.     }

  90.     //保存图片
  91.     private function saveImage($image,$url){
  92.         if(ImageJpeg($image,$url)){
  93.             return true;
  94.         }
  95.     }
  96. }
复制代码
[PHP]代码
  1. //使用
  2. <?php

  3. include('image_process.class.php');
  4. $m = array(
  5.     'D:\myspace\test\image_process\1.jpg',
  6.     'D:\myspace\test\image_process\2.jpg',
  7.     'D:\myspace\test\image_process\3.jpg',
  8.     'D:\myspace\test\image_process\4.jpg'
  9. );

  10. $img  = 'D:\myspace\test\image_process\1.jpg';
  11. $logo = 'D:\myspace\test\image_process\logo.png';
  12. foreach( $m as $item){
  13.     $s = new Image_process( $item );
  14.     $s->watermarkImage($logo);
  15.     $s->scaleImage(0.8);
  16.     $s->fixSizeImage(200,false);
  17.     sleep(1);
  18. }
复制代码

论坛徽章:
59
2015七夕节徽章
日期:2015-08-24 11:17:25ChinaUnix专家徽章
日期:2015-07-20 09:19:30每周论坛发贴之星
日期:2015-07-20 09:19:42ChinaUnix元老
日期:2015-07-20 11:04:38荣誉版主
日期:2015-07-20 11:05:19巳蛇
日期:2015-07-20 11:05:26CU十二周年纪念徽章
日期:2015-07-20 11:05:27IT运维版块每日发帖之星
日期:2015-07-20 11:05:34操作系统版块每日发帖之星
日期:2015-07-20 11:05:36程序设计版块每日发帖之星
日期:2015-07-20 11:05:40数据库技术版块每日发帖之星
日期:2015-07-20 11:05:432015年辞旧岁徽章
日期:2015-07-20 11:05:44
2 [报告]
发表于 2015-07-10 10:11 |只看该作者
这个功能确实不错。在图片上传的时候很有用。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP