PHP生成图片缩略图类示例

admin3年前软件教程119

篇文章介绍了PHP生成图片缩略图类示例,有兴趣的同学可以参考一下

本文实例讲述了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;

}

}

免责声明:本文内容来自用户上传并发布,站点仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。请核实广告和内容真实性,谨慎使用。

相关文章

qq音乐关注的人在哪里

qq音乐找到关注的人的操作方法如下:1、打开手机QQ音乐APP,进入页面点击我。 2、在我的页面点击自己的头像。  3、进入个人主页面,在头像下面就有“关注”,点击即可进入查看关注的人。QQ音乐是腾讯...

骑手差评多久能看到

以美团为例,一般情况下给骑手差评,骑手第二天就会知道,但是在三天后才能查看具体内容。美团网的全称为“北京三快在线科技有限公司”,是2010年3月4日成立的团购网站。美团网有着“吃喝玩乐全都有”和“美团...

天猫双11助力额度是什么意思

天猫双11助力额度的意思是为了满足客户对于额度的需求,由系统评估满足使用条件的用户,在天猫双11可以领取临时额度,而且领取额度后的使用时间都是在双11当天。天猫(英语:Tmall,亦称天猫商城),原名...

拼多多品牌店什么意思

拼多多品牌店的意思一般就是品牌旗舰店、品牌专营店。旗舰店是指某一品牌在某城市中最大且最为丰富齐全的营业店,或拥有最快上市速度等特点为一体的专门店或专营店。 品牌专营店是专门经营或授权经营某一主要品牌商...

拼多多上限怎么解除

以拼多多助力次数上限为例,其拼多多每个用户每天(00:00-24:00)有3次助力机会,帮同一用户仅可助力1次。助力次数上限后需要使用另外的账号进行助力。拼多多用户通过发起和朋友、家人、邻居等的拼团,...

微商相册企业版是什么

微商相册企业版是一款图片批量编辑、共享、转发软件,具有素材采集、图片抓取、图片收藏等功能,支持云存储,可以转发高清无损图片。微商相册企业版版是供微商管理相册的客户端应用,中具有图文共享、一键转发功能,...