本文实例讲述了PHP生成图片缩略图类。分享给大家供大家参考,具体如下:
| 代码如下 | 复制代码 |
classApp_image_helper { protected$imgFileName; protected$imgWidth; protected$imgHeight; protected$imgMime; protected$imgResource; static $imgMineList =array( 'jpeg'=>'image/jpeg', 'gif'=>'image/gif', 'png'=>'image/png', 'wbmp'=>'image/wbmp', ); /** * 根据文件名,初始化图片, * 计算出给定图片的宽、高、图片类型,并获取图片的资源保存到内存,便于下次使用 * App_image_helper constructor. * * @param $fileName */ publicfunction__construct($fileName) { $this->imgFileName =$fileName; list($this->imgWidth,$this->imgHeight,$this->imgMime) =$this->getImageInfo($this->imgFileName); $this->imgResource =$this->getImageResource($this->imgFileName); } /** * 根据图片路径获取相关宽、高、MIME类型信息 * * @param $fileName * * @return array|null */ protectedfunctiongetImageInfo($fileName) { $result= null; if(is_file($fileName) ) { $tmpImageInfo=getimagesize($fileName); if($tmpImageInfo) { $result=array($tmpImageInfo[0],$tmpImageInfo[1],$tmpImageInfo['mime']); } } return$result; } /** * 将图片文件转为资源类类型 * * @param $fileName * * @return null|resource */ protectedfunctiongetImageResource($fileName) { $image= null; if(is_file($fileName) ) { switch($this->imgMime) { caseself::$imgMineList['jpeg']: $image= imagecreatefromjpeg($fileName); break; caseself::$imgMineList['gif']: $image= imagecreatefromgif($fileName); break; caseself::$imgMineList['png']: $image= imagecreatefrompng($fileName); break; caseself::$imgMineList['wbmp']: $image= imagecreatefromwbmp($fileName); break; default: break; } } return$image; } /** * 可根据固定宽,等比缩放图片;或根据百分比,等比缩放图片 * * @param int $width * @param int $percent * * @return array|null */ protectedfunctiongetSizeByScale($width= 360,$percent= 1) { $result= null; if($this->imgWidth &&$this->imgHeight ) { if($width) { $result=array($width,intval($width*$this->imgHeight /$this->imgWidth)); }elseif($percent) { $result=array(intval($this->imgWidth *$percent),intval($this->imgHeight *$percent)); } } return$result; } /** * 外调 * * @param int $percentOrWidth int整数表示图片缩放为固定宽度,0.0~0.99999表示缩放百分比 * @param null $fileName * @param int $quality * @param bool $reSample 重新采样图片,默认是 * * @return bool */ publicfunctioncreateImage($percentOrWidth= 1,$fileName= null,$quality= 75,$reSample= true) { $result= false; $fileName? header('Content-Type: '.$this->imgMime) : false; $size=$this->getSizeByScale(($percentOrWidth<= 1) ? null :$percentOrWidth,$percentOrWidth); if($size) { $thumb= imagecreatetruecolor($size[0],$size[1]); if($reSample) { imagecopyresampled($thumb,$this->imgResource, 0, 0, 0, 0,$size[0],$size[1],$this->imgWidth,$this->imgHeight); }else{ imagecopyresized($thumb,$this->imgResource, 0, 0, 0, 0,$size[0],$size[1],$this->imgWidth,$this->imgHeight); } $result= imagejpeg($thumb,$fileName,$quality); } return$result; } } | |
g沙盒仇恨官方英文版(gorebox)
G沙盒仇恨英文原版是一款最近非常火热的沙盒模拟类游戏,在这里
迷你世界测试服最新版2024
迷你世界测试服2021最新版,即迷你世界的先遣服版本,用户能
闪耀暖暖最新版2024
闪耀暖暖手游这是非常好玩的换装手游,游戏内容丰富有趣,游戏环
杜拉拉升职记官方正版
杜拉拉升职记手游完美还原了经典同名电视剧中的故事剧情、以及各
银河战舰手机版
银河战舰满你所想宇宙战舰世界,茫茫宇宙资源是一切,你必须利用