PHP生成图片缩略图类示例

admin3年前软件教程118

篇文章介绍了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;

}

}

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

相关文章

soul一个手机可以注册几个号

目前soul这个软件,只能通过手机号进行注册,一个手机号只能注册一个账号。2019年6月28日,国家网信办通报称,针对网络音频乱象启动专项整治行动。根据群众举报线索,经核查取证,首批依法依规对吱呀、S...

钉钉中途换设备会影响观看时长吗

钉钉切换设备会影响观看时长,切换了设备,时长会从最后那个设备的观看时间算起。钉钉(DingTalk)是中国领先的智能移动办公平台,由阿里巴巴集团开发,于2015年1月份正式上线。该平台免费提供给所有中...

爱奇艺怎么修改清晰度

以爱奇艺v11.8.6为例,切换清晰度方法如下:1、手机上打开“爱奇艺”视频软件。2、点击要看的视频进入播放页面。3、点击视频右下角的全屏图标,使视频进入全屏模式。4、全屏状态下,点击视频右下方的清晰...

激萌能录三分钟视频吗

截止2020年8月24日,激萌版本最长只能拍摄1分钟的视频,不能拍摄3分钟的长视频,在激萌APP里长按拍摄键即可拍视频。 Faceu激萌是由今日头条旗下脸萌团队制作的一款拍照相机软件,于2015年9月...

网易云唱歌的在哪里

网易云音乐APP版本为7.1.80,截止到2020年9月,网易云音乐无k歌功能,所以不能唱歌,只能听音乐。想要唱歌的话,可以使用其它APP。网易云音乐是一款由网易开发的音乐产品,是网易杭州研究院的成果...

芒果tv会员最多能登录几个

同一个芒果会员账号最多可以在五个设备上使用,且同一时间内同一账号最多在两个设备上使用,同一账号24小时内最多可以在五个不同城市登录。超过上述范围,可能会导致账号被冻结。芒果TV是以视听互动为核心,融网...