燕十八------------缩略图与水印类

水印:就是将水印写在目标图片上

缩略图:就是将一张大的图片放在一个小的区域

封装类时,首先要考虑我们需要将用到的图片的大小以及类型搞清楚,那么我们就需要用到一个函数:getimagesize();

它的返回值是一个数组:0)宽度 1)高度 2)图像类型的标记 3)是文本字符串,内容为“height="yyy" width="xxx"”,对于 JPG 图像,还会多返回两个索引:channelsbitschannels 对于 RGB 图像其值为 3,对于 CMYK 图像其值为 4。bits 是每种颜色的位数。最后返回一个mime类型

图片类型的标记:1 = GIF,2 = JPG,3 = PNG,4 = SWF,5 = PSD,6 = BMP,7 = TIFF(intel byte order),8 = TIFF(motorola byte order),9 = JPC,10 = JP2,11 = JPX,12 = JB2,13 = SWC,14 = IFF,15 = WBMP,16 = XBM。

此时可以用到的一个函数为Example #1 image_type_to_mime_type (file)

image_type_to_mime_type ($info['2'])— 取得 getimagesize,exif_read_data,exif_thumbnail,exif_imagetype 所返回的图像类型的 MIME 类型

当然也可以不用,直接将数组中mime的键值(image/jpg)截取下来就好了。

 

 

 

 

代码:

class ImageTool{

//得到图片的大小及类型

  public static function imageInfo($image){

    if(!file_exists){return false;}

    $info=getimagesize($image);

    if(info==false){return false;}

    //此时返回一个数组

    $img['width']=$info[0];

    $img['height']=$info[1];

    //返回类型

    1)$in=explode('/',$info['mime']);

    $img['ext']=strtolower(end($in));

    2)

    $img['ext']=substr($info['mime'],strpos($info['mime'],'/')+1);

    return $img;

 

    }

//得到水印 

1)拷贝图像的一部分:bool imagecopy ( resource $dst_im , resource $src_im , int $dst_x , int $dst_y , int $src_x , int $src_y , int $src_w , int $src_h )

2)拷贝并合并图像的一部分:bool imagecopymerge ( resource $dst_im , resource $src_im , int $dst_x , int $dst_y , int $src_x , int $src_y , int $src_w , int $src_h , int $pct )

    public static function water($dst,$src,$save=null,$pos=2,$alpha=50){

 

      if(!file_exists($dst)||!file_exists($src)){

        return false;//文件不存在,则返回false

      }

      $dinfo=self::imageInfo($dst);
      $winfo=self::imageInfo($src);

      //水印的大小必须要小于目标图片的大小,否则额返回错误

      if($winfo['width']>$dinfo['width']||$winfo['height']>$dinfo['height']){

        return false;

      }

      //转为资源类型。创建目标文件画布

      $dfunc='imagecreatefrom'.$dinfo['ext'];

      //创建水印画布

      $wfunc='imagecreatefrom'.$winfo['ext'];

      判断功能是否存在

      if(!function_exists($dfunc)||!function_exists($wfunc)){

        return false;

      }

        //,调用动态加载

      $dim=$dfunc($dst);

      $wim=$wfunc($src);

      //水印的位置

      switch($pos){

      case(0)://在左上角

      $dst_x=0;

      $dst_y=0;

      break;

      case(1)://在右上角

      $dst_x=$dinfo['width']-$winfo['width'];

      $dst_y=0;

      break;

      case(3)://左下角

      $dst_x=0;

      $dst_y=$dinfo['height']-$winfo['height'];

      break;

      default:

      $dst_x=$dinfo['width']-$winfo['width'];

      $dst_y=$dinfo['height']-$winfo['height'];

      }

      //加水印

      imagecopymerge($dim,$wim,$dst_x,$dst_y,0,0,$winfo['width'],$winfo['height'],$alpha);

      //保存路径

      if(!$save){

        $save=$dst;

        unlink($dst);//删除原图

      }

      //保存

      $func='image'.$dinfo['ext'];

      $func($dim,$save);

      //销毁资源

      imagedestroy($dim);

      imagedestroy($wim);

      return true;

    }

    //缩略图:成比例的缩放

    imagecopyresampled:重采样拷贝部分图像并调整大小

    bool imagecopyresampled ( resource $dst_image , resource $src_image , int $dst_x , int $dst_y , int $src_x , int $src_y , int $dst_w , int $dst_h , int $src_w , int $src_h )

    public static function thumb($dst,$save=null,$width=200;$height=200){

      //判断dst文件是否存在

 

      $dinfo=ImageTool::imageInfo($dst);

       if(dinfo==false){

        return false;

      }

      //缩放比例

      $calc=min($width/$dinfo['width'],$height/$dinfo['height']);

      //截取大小

      $dst_w=(int)($calc*$dinfo['width']);

      $dst_h=(int)($calc*$dinfo['height']);

      //位置计算

      $paddingx=(int)[($width-$dst_w)/2];

      $paddingy=(int)[($width-$dst_h)/2];

      //创建画布

      $dfunc='imagecreatefrom'.$dinfo['ext'];

      if(!function_exists($func)){

         return false;

      }  

      $dim=$dfunc($dst);

      //创建缩略图画布

      $sim=imagecreatetruecolor($width,$height);

      $white=$imagecolorallocate($sim,255,255,255);

      imagefill($sim,0,0,$white);

      imagecopyresampled($sim,$dim,$paddingx,$paddingy,0,0,$dst_w,$dst_h,$dinfo['width'],$dinfo['height']);

      //保存的位置

      if(!$save){

        $save=$dst;

        unlink($dst);

      }

      //保存

      $zfunc='image'.$dinfo['ext'];

      $zfunc($sim,$save);

      imagedestroy($sim);

      imagedestroy($dim);

      return true;

    }

}

调用imageInfo():print_r(ImageTool::imageInfo('./pic1.jpg'));

调用water():echo ImageTool::water('./pic1.jpg','./01.png','./home1.jpg',1,30)?'ok':'no';

调用thumb():

echo ImageTool::thumb('./home3.jpg','./this1.jpg')?'ok':'fail';
echo ImageTool::thumb('./pic1.jpg','./this2.jpg')?'ok':'fail';

 

posted on 2016-01-15 16:40  yanran  阅读(298)  评论(0编辑  收藏  举报