123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356 |
- <?php
- /*
- * ResizeImage.php
- * 缩略图
- * 更新: Jacky.Chen 2013/10/21 21:26
- *
- */
- namespace app\common\components;
- use Yii;
- class ResizeImage
- {
- //图片类型
- private $type;
- //实际宽度
- public $width;
- //实际高度
- public $height;
- //改变后的宽度
- private $resizeWidth;
- //改变后的高度
- private $resizeHeight;
- //是否裁图
- public $cut;//0:等比缩放 1:从左上角裁切 2:裁切中间核心内容 3: 按照目标大小,填白缩放
- //源图象
- public $srcimg;
- //目标图象地址
- public $dstimg;
- //临时创建的图象
- var $im;
- //是否使用缓存
- public $usecache;
- //文件名
- private $filename;
- //文件路径hash
- private $hash;
- function __construct($srcimg, $resizewidth, $resizeheight, $hash, $cut, $usecache, $dstrootdir)
- {
- dir_create($dstrootdir);
- if ($srcimg == '') {return false;}
- $this->srcimg = $srcimg;
- $this->resizeWidth = $resizewidth;
- $this->resizeHeight = $resizeheight;
- $this->cut = $cut;
- $this->usecache = $usecache;
- $this->hash = $hash;
- //图片的类型
- $this->type = 'jpg';//所有的缩略图都为jpg后缀
- //$this->type = strtolower(substr(strrchr($this->srcimg,"."),1));
- //目标图象地址
- $this -> dstImg($dstrootdir);
- if ($this->usecache && file_exists($this->dstimg)){
- return true;
- }
- else
- {
- //初始化图象
- $this->initImg();
- //尺寸
- $this->width = imagesx($this->im);
- $this->height = imagesy($this->im);
- //生成图象
- $this->newImg();
- ImageDestroy($this->im);
- }
- }
- //返回缩略图相关信息
- function getDstImgInfo()
- {
- $info = getimagesize($this->dstimg);
- $array = array('width'=>$info[0],'height'=>$info[1],'type'=>$this->type,'dstimg'=>$this->dstimg);
- return $array;
- }
- private function newImg()
- {
- //改变后的图象的比例
- $resize_ratio = ($this->resizeWidth)/($this->resizeHeight);
- //实际图象的比例
- $ratio = ($this->width)/($this->height);
- if(($this->cut)=="1")
- //裁图
- {
- if($ratio>=$resize_ratio)
- //高度优先
- {
- $newimg = imagecreatetruecolor($this->resizeWidth,$this->resizeHeight);
- $color=imagecolorallocate($newimg,255,255,255);
- imagecolortransparent($newimg,$color);
- imagefill($newimg,0,0,$color);
- imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, $this->resizeWidth,$this->resizeHeight, (($this->height)*$resize_ratio), $this->height);
- $this->createImg($newimg);
- }
- if($ratio<$resize_ratio)
- //宽度优先
- {
- $newimg = imagecreatetruecolor($this->resizeWidth,$this->resizeHeight);
- $color=imagecolorallocate($newimg,255,255,255);
- imagecolortransparent($newimg,$color);
- imagefill($newimg,0,0,$color);
- imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, $this->resizeWidth, $this->resizeHeight, $this->width, (($this->width)/$resize_ratio));
- $this->createImg($newimg);
- }
- }
- else if($this->cut==2)//取最核心的中间内容
- {
-
- if($this->width>=$this->resizeWidth)//如果原图宽度大于目标宽度
- {
- //目标偏移量
- $src_y = 0;
- $src_x = 0;
- //以原图比例为基础,宽度优先计算出的缩略目标图尺寸
- $resize_width = $this->resizeWidth;
- $resize_height = ceil($this->resizeWidth/$ratio);
- //原图以宽度优先,按照缩略图宽高比计算出的高度
- $cal_height = ceil($this->width/$resize_ratio);
- if($this->height>$cal_height)//缩略时进行位置偏移
- {
- $src_y = ceil(($this->height-$cal_height)/2);
- $final_height = $cal_height;
- $final_width= $this->width;
- }
- else//原图高度不足
- {
- $resize_height = $this->height;
- $resize_width = ceil($resize_height*$resize_ratio);
- //原图以高度优先,按照缩略图宽高比计算出的宽度
- $cal_width = ceil($this->height*$resize_ratio);
- //位置偏移
- $src_x = ceil(($this->width-$cal_width)/2);
- $final_width = $cal_width;
- $final_height = $this->height;
- }
- $newimg = imagecreatetruecolor($this->resizeWidth,$this->resizeHeight); imagefill($newimg, 0, 0, 0xFFFFFF);
- $color=imagecolorallocate($newimg,255,255,255);
- imagecolortransparent($newimg,$color);
- imagefill($newimg,0,0,$color);
- $dst_x = $dst_y = 0;
- if ($resize_width < $this->resizeWidth){$dst_x = ($this->resizeWidth-$resize_width)/2;}
- if ($resize_height < $this->resizeHeight){ $dst_y = ($this->resizeHeight - $resize_height)/2;}
- imagecopyresampled($newimg, $this->im, $dst_x, $dst_y, $src_x, $src_y, $this->resizeWidth, $this->resizeHeight,$final_width,$final_height);
- $this->createImg($newimg);
- }
- else
- {
- //目标偏移量
- $src_y = 0;
- $src_x = 0;
- if($this->height>=$this->resizeHeight)//如果原图高度大于目标高度
- {
- //以原图比例为基础,高度优先计算出的缩略目标图宽度
- $resize_width = ceil($this->resizeHeight*$ratio);
- $resize_height = $this->resizeHeight;
-
- $newimg = imagecreatetruecolor($this->resizeWidth,$this->resizeHeight);imagefill($newimg, 0, 0, 0xFFFFFF);
- $dst_x = $dst_y = 0;
- if ($resize_width < $this->resizeWidth){$dst_x = ($this->resizeWidth-$resize_width)/2;}
- if ($resize_height < $this->resizeHeight){ $dst_y = ($this->resizeHeight - $resize_height)/2;}
- imagecopyresampled($newimg, $this->im, $dst_x, $dst_y, $src_x, $src_y, $resize_width, $resize_height,$this->width,$this->height);
- $this->createImg($newimg);
- }
- else
- {
- $src_y = 0;
- $src_x = 0;
- $newimg = imagecreatetruecolor($this->resizeWidth,$this->resizeHeight);imagefill($newimg, 0, 0, 0xFFFFFF);
- $dst_x = $dst_y = 0;
- $resize_width = $this->width;
- $resize_height = $this->height;
- if ($resize_width < $this->resizeWidth){$dst_x = ($this->resizeWidth-$resize_width)/2;}
- if ($resize_height < $this->resizeHeight){ $dst_y = ($this->resizeHeight - $resize_height)/2;}
- imagecopyresampled($newimg, $this->im, $dst_x, $dst_y, $src_x, $src_y, $resize_width, $resize_height,$this->width,$this->height);
- $this->createImg($newimg);
- }
- }
- }
- else if($this->cut==3)
- {
- if($ratio>=$resize_ratio)
- {
- $newimg = imagecreatetruecolor($this->resizeWidth,($this->resizeWidth)/$ratio);
- $color=imagecolorallocate($newimg,255,255,255);
- imagecolortransparent($newimg,$color);
- imagefill($newimg,0,0,$color);
- imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, $this->resizeWidth, ($this->resizeWidth)/$ratio, $this->width, $this->height);
- $this->createImg($newimg);
- $this->srcimg = $this->dstimg;
- $this->initImg();
- $this->width = imagesx($this->im);
- $this->height = imagesy($this->im);
- $newimg = imagecreatetruecolor($this->resizeWidth,$this->resizeHeight);
- $whites = imagecolorallocate($newimg,255,255,255);
- imagefill($newimg,0,0,$whites);
- $dst_x = 0;
- $dst_y = ($this->resizeHeight-$this->height)/2;
- imagecopy($newimg, $this->im, $dst_x, $dst_y, 0, 0, $this->width, $this->height);
- $this->createImg($newimg);
-
- }
- if($ratio<$resize_ratio)
- {
- $newimg = imagecreatetruecolor(($this->resizeHeight)*$ratio,$this->resizeHeight);
- $color=imagecolorallocate($newimg,255,255,255);
- imagecolortransparent($newimg,$color);
- imagefill($newimg,0,0,$color);
- imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, ($this->resizeHeight)*$ratio, $this->resizeHeight, $this->width, $this->height);
- $this->createImg($newimg);
- $this->srcimg = $this->dstimg;
- $this->initImg();
- $this->width = imagesx($this->im);
- $this->height = imagesy($this->im);
- $newimg = imagecreatetruecolor($this->resizeWidth,$this->resizeHeight);
- $whites = imagecolorallocate($newimg,255,255,255);
- imagefill($newimg,0,0,$whites);
- $dst_x = ($this->resizeWidth-$this->width)/2;
- $dst_y = 0;
- imagecopy($newimg, $this->im, $dst_x, $dst_y, 0, 0, $this->width, $this->height);
- $this->createImg($newimg);
- }
- }
- else
- {
- if($ratio>=$resize_ratio)
- {
- $newimg = imagecreatetruecolor($this->resizeWidth,($this->resizeWidth)/$ratio);
- $color=imagecolorallocate($newimg,255,255,255);
- imagecolortransparent($newimg,$color);
- imagefill($newimg,0,0,$color);
- imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, $this->resizeWidth, ($this->resizeWidth)/$ratio, $this->width, $this->height);
- $this->createImg($newimg);
- }
- if($ratio<$resize_ratio)
- {
- $newimg = imagecreatetruecolor(($this->resizeHeight)*$ratio,$this->resizeHeight);
- $color=imagecolorallocate($newimg,255,255,255);
- imagecolortransparent($newimg,$color);
- imagefill($newimg,0,0,$color);
- imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, ($this->resizeHeight)*$ratio, $this->resizeHeight, $this->width, $this->height);
- $this->createImg($newimg);
- }
- }
- }
- //初始化图象
- private function initImg()
- {
- $srcInfo = getimagesize($this->srcimg);
- switch ($srcInfo[2])
- {
- case 1:
- $this->im = imagecreatefromgif($this->srcimg); break;
- case 2:
- $this->im = imagecreatefromjpeg($this->srcimg); break;
- case 3:
- $this->im = imagecreatefrompng($this->srcimg); break;
- case 6:
- $this->im = imagecreatefrombmp($this->srcimg); break;
- default:
- die('Unsupport the file type'); exit();
- }
- }
- //生成目标文件
- private function createImg(&$newimg)
- {
-
- $srcInfo = @getimagesize($this->srcimg);
- switch ($srcInfo[2])
- {
- case 1:
- imagegif($newimg,$this->dstimg); break;
- case 2:
- imagejpeg($newimg,$this->dstimg); break;
- case 3:
- imagepng($newimg,$this->dstimg); break;
- case 6:
- file_put_contents($this->dstimg,file_get_contents($this->srcimg)); break;
- default:
- die('unsupport file type'); exit();
- }
- imagedestroy($newimg);
- }
-
-
- //图象目标地址
- private function dstImg($dstrootdir)
- {
- /*
- *以源文件名基础,获取缩略图文件名
- $basename = basename($this->srcimg);
- $this->filename = substr($basename,strrpos($basename,DIRECTORY_SEPARATOR)+1,0-(strlen($basename)-strpos($basename,'.')));
- if (strlen($this->filename)<4){return false;}
- */
- $this->filename = $this->hash;
- if ($this->resizeWidth<=0 || $this->resizeHeight<=0){return false;}
- if(!empty($dstrootdir))
- {
- $this->dstimg = rtrim($dstrootdir,DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR.$this->resizeWidth.'_'.$this->resizeHeight.'_'.$this->filename.'.'.$this->type;
- }
- else
- {
- $this->dstimg = $this->resizeWidth.'_'.$this->resizeHeight.'_'.$this->filename.'.'.$this->type;
- }
- }
- }
- function imagecreatefrombmp($p_sFile)
- {
- $file = fopen($p_sFile,"rb");
- $read = fread($file,10);
- while(!feof($file)&&($read<>""))
- $read .= fread($file,1024);
- $temp = unpack("H*",$read);
- $hex = $temp[1];
- $header = substr($hex,0,108);
- if (substr($header,0,4)=="424d")
- {
- $header_parts = str_split($header,2);
- $width = hexdec($header_parts[19].$header_parts[18]);
- $height = hexdec($header_parts[23].$header_parts[22]);
- unset($header_parts);
- }
- $x = 0;
- $y = 1;
- $image = imagecreatetruecolor($width,$height);
- $body = substr($hex,108);
- $body_size = (strlen($body)/2);
- $header_size = ($width*$height);
- $usePadding = ($body_size>($header_size*3)+4);
- for ($i=0;$i<$body_size;$i+=3)
- {
- if ($x>=$width)
- {
- if ($usePadding)
- $i += $width%4;
- $x = 0;
- $y++;
- if ($y>$height)
- break;
- }
- $i_pos = $i*2;
- $r = hexdec($body[$i_pos+4].$body[$i_pos+5]);
- $g = hexdec($body[$i_pos+2].$body[$i_pos+3]);
- $b = hexdec($body[$i_pos].$body[$i_pos+1]);
- $color = imagecolorallocate($image,$r,$g,$b);
- imagesetpixel($image,$x,$height-$y,$color);
- $x++;
- }
- unset($body);
- return $image;
- }
- ?>
|