首页 > 其他 > 详细

时间:2014年4月10日09:52:21缩略图和水印类

时间:2014-04-11 16:15:37      阅读:432      评论:0      收藏:0      [点我收藏+]


如何获取图片的大小和类型?

在缩略图中,不知道大小无法确定比例

不知道类型,无法确定要调用的函数,如imagecreatefrompng()等。。

要处理的图片可能是各种大小,各种类型都有,如何获取大小和类型

使用函数getimagesize()

getimagesize 取得图像大小

array getimagesize ( string $filename [,array &$imageinfo ] )

string image_type_to_mime_type ( int$imagetype )

判断一个 IMAGETYPE常量的 MIME 类型。

<?php

$arr = getimagesize("./2.jpg");

print_r($arr);

echo image_type_to_mime_type($arr[2]);

/*

Array

(

    [0] => 720 //

    [1] => 1280 //

    [2] => 2 // 图片类型

    [3] => width="720" height="1280"

    [bits] => 8

    [channels] => 3

    [mime] => image/jpeg

)

类型标号:

1 = GIF2 = JPG3 = PNG4 = SWF

5 = PSD6 = BMP7 = TIFF(intel byte order)

8 = TIFF(motorola byte order)9 = JPC10 = JP2

11 = JPX12 = JB213 = SWC14 = IFF15 = WBMP16 = XBM

*/

?>


缩略图类:

<?php

/*

想操作图片,先要获得图片的大小,类型信息

水印就是把指定的水印复制到目标上,并加透明效果

缩略图就是把大图复制到小尺寸的图片上*/

class ImageTool{

     //imageInfo()  分析图片的信息

     //return  array()

     public  static function imageInfo($image){

          if(!file_exists($image)){

                return  false;

          }

          $info  = getimagesize($image);

          if  ($info == false) {

                return  false;

          }

          //分析info数组

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

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

          /*int  strpos ( string 被查找的字符串 , mixed 查找的内容 [, int 开始位置 = 0 ] )*/

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

          return  $img;

     }

     /*加水印功能*

     param  string $dst 待操作图片

     param  string $water 水印小图

     param  string $save ,不填默认替换原始图

     param  int $alpha 透明度

     param  int $position 水印位置左上开始顺时针 0123,默认2

     */

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


          //首先需要保证两个图片都得存在

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

                return  false;


          }


          //需要保证水印不能比待操作图片还要大

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

          $winfo  = self::imageInfo($water);

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

                return  false;

          }

          //把两张图都读到画布上,如何来读图片

          //动态加载函数名

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

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

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

                return  false;

           }

          //动态加载函数,创建画布

          $dim  = $dfunc($dst);//创建待操作的画布

          $wim  = $wfunc($water);//创建水印画布

          //根据水印的位置,计算粘贴的坐标

          switch  ($position) {

                case  0: $pos[x]=0;

                           $pos[y]=0;  break; // 左上角

                case  1: $pos[x]=$dinfo[‘width‘]-$winfo[‘width‘];

                           $pos[y]=0;  break; //右上角

                case  2: $pos[x]=$dinfo[‘width‘]-$winfo[‘width‘];

                           $pos[y]=$dinfo[‘height‘]-$winfo[‘height‘];  break; // 右下角

                case  3: $pos[x]=0;

                           $pos[y]=$dinfo[‘height‘]-$winfo[‘height‘];  break; // 左下角

          }

          //加水印


          imagecopymerge($dim,$wim,  $pos[x], $pos[y], 0, 0, $winfo[‘width‘], $winfo[‘height‘], $alpha);

          //保存

          if  (!$save) {

                $save  = $dst;

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

          }

          $createfunc  = ‘image‘.$dinfo[‘ext‘];

          $createfunc($dim,$save);

          imagedestroy($dim);

          imagedestroy($wim);

          return  true;

     }

     /*生成缩略图

等比例缩放,两边留白

     */

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

          //判断待处理的图片是否存在

          if(!file_exists($dst)){

                return  false;

          }

          //获取图片信息

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

          if  ($dinfo == false) {

                return  fasle;

          }

          //计算缩放比例

          //按宽高各计算缩放比

          //取小的值

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

          echo  $calc;

          /*创建原始图画布*/

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

          $dim  = $dfunc($dst);

          /*创建缩略画布*/

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

          /*创建白色填充缩略画布*/

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

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

          /*复制并缩略*/

          ///imagecopyresampled(dst_image,  src_image, dst_x, dst_y, src_x, src_y, dst_w, dst_h, src_w, src_h)

          $dwidth  = (int)($dinfo[‘width‘]*$calc);

          $dheight  = (int)($dinfo[‘height‘]*$calc);

          $padingx  = (int)(($width-$dwidth)/2);

          $padingy  = (int)(($height-$dheight)/2);

          echo  $dwidth,$dheight,$padingx,$padingy;

          imagecopyresampled($tim,  $dim, $padingx, $padingy, 0, 0, $dwidth,$dheight,  $dinfo[‘width‘], $dinfo[‘height‘]);

          /*生成缩略图*/

          if  (!$save) {

                $save  = $dst;

                unlink($dst);

          }

          $createfunc  = ‘image‘.$dinfo[‘ext‘];

          $createfunc($tim,$save);

          /*销毁图片*/

          imagedestroy($dim);

          imagedestroy($tim);

          return  true;

     }

}

//print_r(ImageTool::imageInfo(‘./2.jpg‘));

/*Array ( [width] => 720 [height]  => 1280 [ext] => jpeg ) */


/*echo  ImageTool::water(‘2.jpg‘,‘1.png‘,‘new0.jpg‘,0)?ok:fail;

echo  ImageTool::water(‘2.jpg‘,‘1.png‘,‘new1.jpg‘,1)?ok:fail;

echo  ImageTool::water(‘2.jpg‘,‘1.png‘,‘new2.jpg‘,2)?ok:fail;

echo  ImageTool::water(‘2.jpg‘,‘1.png‘,‘new3.jpg‘,3)?ok:fail;*/

/*echo ImageTool::thumb(‘1.png‘,  ‘thumb1.jpg‘)?ok:fail;

echo ImageTool::thumb(‘1.png‘,  ‘thumb2.jpg‘,100,100)?ok:fail;

echo ImageTool::thumb(‘2.jpg‘,  ‘thumb3.jpg‘)?ok:fail;

echo ImageTool::thumb(‘2.jpg‘,  ‘thumb4.jpg‘,100,100)?ok:fail;*/

?>


本文出自 “杜国栋个人PHP学习博文” 博客,请务必保留此出处http://duguodong.blog.51cto.com/7667978/1393700

时间:2014年4月10日09:52:21缩略图和水印类,布布扣,bubuko.com

时间:2014年4月10日09:52:21缩略图和水印类

原文:http://duguodong.blog.51cto.com/7667978/1393700

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!