搜索
查看: 2051|回复: 0

php实现常见图片格式的水印和缩略图制作(面向对象)

[复制链接]

5378

主题

5381

帖子

1万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
17651
发表于 2018-2-11 12:09:53 | 显示全部楼层 |阅读模式

            本文实例为大家分享了php水印和缩略图制作代码,使用面向对象的方法来实现常见图片格式jpg,png,gif的水印和缩略图的制作,供大家参考,具体内容如下
check($img) || !$this->thumb_on) return FALSE;
   
  //定义缩略图的初始值
  $t_type = $t_type ? $t_type : $this->thumb_type;
  $t_w = $t_w ? $t_w : $this->thumb_width;
  $t_h = $t_h ? $t_h : $this->thumb_height;
   
  //获取到原图的信息
  $($img);
  $$[0];
  $$[1];
  //取得图像类型的文件后缀
  $($[2]);
  //获取到相关尺寸
  $thumb_size = $this->thumb_size($,$,$t_w,$t_h,$t_type);
  //确定原始图像类型
  //利用自定义函数来实现图片类型的确定
  $func = "imagecreatefrom".substr($, 1);
  $res_$func($img);
   
  //缩略图资源   编辑图片资源moon
  if( $'.gif' || $'.png' ){
   $res_thumb = imagecreate($thumb_size[0], $thumb_size[1]);
   $color = imagecolorallocate($res_thumb, 255, 0, 0);
  }else{
   $res_thumb = imagecreatetruecolor($thumb_size[0], $thumb_size[1]);
  }
   
  //制作缩略图
  if(function_exists( "imagecopyresampled" ) ){
   imagecopyresampled($res_thumb, $res_img, 0, 0, 0, 0, $thumb_size[0],$thumb_size[1],$thumb_size[2],$thumb_size[3]);
  }else{
   imagecopyresized($res_thumb, $res_img, 0, 0, 0, 0, $thumb_size[0],$thumb_size[1],$thumb_size[2],$thumb_size[3]);
  }
  //处理透明色
  if( $'.gif' || $'.png' ){
   imagecolortransparent($res_thumb,$color);
  }
   
  //配置输出文件名
  $outfile = $outfile ? $outfile : $outfile.substr($img,0,strripos($img,'.')).$this->thumb_fix.$;
   
  //文件的保存输出
  $func = "image".substr($, 1);
  $func($res_thumb,$outfile);
  if(isset($res_thumb)) imagedestroy ($res_thumb);
  if(isset($res_img)) imagedestroy ($res_img);
  return $outfile;
}

public function watermark( $img,$pos='',$out_'',$water_'',$text='' ){
  if(!$this->check($img) || !$this->watermark_on) return false;
   
  $water_$water_$water_$this->water_img;
  //水印的开启状态
  $water$this->check($water_img) ? 1 : 0;
  //判断是否在原图上操作
  $out_$out_$out_$img;
  //判断水印的位置
  $pos = $pos ? $pos : $this->pos;
  //水印文字
  $text = $text ? $text : $this->text;
   
   
  $($img);
  $$[0];
  $$[1];
  //判断水印图片的类型
   
   
  if( $water){
   $w_info = getimagesize($water_img);
   $w_w = $w_info[0];
   $w_h = $w_info[1];
   if ( $)!=7 ) return FALSE;
   $text_info = imagettfbbox($this->size, 0, $this->font, $text);
   $w_w = $text_info[2] - $text_info[6];
   $w_h = $text_info[3] - $text_info[7];
  }
   
  //建立原图资源
   
  switch ( $[2] ){
   case 1:
    $res_($img);
    break;
   case 2:
    $res_($img);
    break;
   case 3:
    $res_($img);
    break;
  }
  //确定水印的位置
  switch ( $pos ){
   case 1:
    $x = $y =25;
    break;
   case 2:
    $x = ($- $w_w)/2;
    $y = 25;
    break;
   case 3:
    $x = $- $w_w;
    $y = 25;
    break;
   case 4:
    $x = 25;
    $y = ($- $w_h)/2;
    break;
   case 5:
    $x = ($- $w_w)/2;
    $y = ($- $w_h)/2;
    break;
   case 6:
    $x = $- $w_w;
    $y = ($- $w_h)/2;
    break;
   case 7:
    $x = 25;
    $y = $- $w_h;
    break;
   case 8:
    $x = ($- $w_w)/2;
    $y = $- $w_h;
    break;
   case 9:
    $x = $- $w_w;
    $y = $- $w_h;
    break;
   default :
    $x = mt_rand(25, $- $w_w);
    $y = mt_rand(25, $- $w_h);
  }
   
  //写入图片资源
  if( $water){
   imagecopymerge($res_img, $w_img, $x, $y, 0, 0, $w_w, $w_h, $this->pct);
}else{
  $r = hexdec(substr($this->color, 1,2));
  $g = hexdec(substr($this->color, 3,2));
  $b = hexdec(substr($this->color, 5,2));
  $color = imagecolorallocate($res_img, $r, $g, $b);
  imagettftext($res_img, $this->size, 0, $x, $y, $color, $this->font, $text);
}
  
//生成图片类型
switch ( $[2] ){
  case 1:
   imagecreatefromgif($res_img,$out_img);
   break;
  case 2:
   //imagecreatefromjpeg($res_img,$out_img);
   imagejpeg($res_img,$out_img);
   break;
  case 3:
   imagepng($res_img,$out_img);
   break;
}
if(isset($res_img)) imagedestroy ($res_img);
if(isset($w_img)) imagedestroy($w_img);
return TRUE;
}
//验证图片是否存在
  private function check($img){
   $type = array('.jpg','.jpeg','.png','.gif');
   $(strrchr($img, '.'));
   return extension_loaded('gd') && file_exists($img) && in_array($, $type);
  }
   
  //获取缩略图的相关比例
  //获取到图片的处理类型
  private function thumb_size( $,$,$t_w,$t_h,$t_type){
   //定义缩略图尺寸
   $w = $t_w;
   $h = $t_h;
   
   //定义图片的原始尺寸
   $cut_w = $;
   $cut_h = $;
   
   //当要目标图像小于缩略图的尺寸时;
   if( $){
     switch ( $t_type ){
      //当宽度固定时
      case 1:
       $h = $t_w/$*$;
       break;
      //高度固定时
      case 2:
       $w = $t_h/$*$;
       break;
      //宽度固定,高度裁切
      case 3:
       $cut_h = $$t_w*$t_h;
       break;
      //高度固定,宽度裁切
      case 4:
       $cut_w = $$t_h*$t_w;
       break;
      //等比例缩放
      default :
       if( ($$t_w) > ($$t_h) ){
        $h = $t_w/$*$t_h;
       }elseif( ($$t_w)
长春网站建设 网站设计 www.4435.cn 电话:136 2446 7185(于先生)
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

长春门户网站

长春门户网是网民了解长春的网络窗口,同是提供长春地区百姓生活分类供求信息的门户网站,同时提供长春网站建设、长春网站设计,我们将逐步的完善网站分类信息资源;

长春门户网二维码

联系我们

  • 工作时间:早上8:00 - 晚上5:30
  • 投稿联系:13624467185(微信同号)
  • 反馈邮箱:5053050@QQ.com
  • 公司地址:吉林省长春市亚泰大街与自由大路交汇五环国际大厦1408室

QQ|小黑屋|手机版|Archiver|cc! ( 吉ICP备2021009740号-8 )

Powered by Discuz! X3.4 © 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表