PHP生成图片缩略图类示例

admin3年前软件教程46

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

}

}

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

相关文章

婚恋交友APP开发要注意什么

现在随着互联网的发展,社会上的社交类APP越来越多出现在我们的生活中。婚恋交友APP就是现在其中的一种。现在都市中的单身男女群体也是喜欢通过这些类型的社交软件在线交友聊天,在开发APP时候也是要注意其...

哔哩哔哩电视大会员与大会员的区别是什么

大家好,我是80知识网,上述问题将由我为大家进行解答。以哔哩哔哩6.9.1版本为例,电视大会员为哔哩哔哩TV客户端“云视听小电视”的专有会员特权,大会员无法在TV端享受会员特权,除此之外,两者在其他会...

促使app失败的致命点是什么

促使app失败的致命点是:1、强迫用户注册朋友给你推荐了一款非常优秀的应用,而你也非常有兴趣使用它。下载应用,打开,而弹出的第一个屏幕却要求你先注册,很肯定的一点,这不是你期望的第一体验。用户下载应用...

宠物APP开发覆盖哪些功能应用

宠物带来的市场经济逐渐攀升,当下宠物行业混乱攀高消费,收费杂乱,品质监管等等的问题,对于线下宠物从业人士来发展限制,宠物看似“钱景无限”背后如何去抓住市场机会,创造有价值的市场?不少企业对于宠物APP...

腾讯会议只能30人视频吗

PC端一个会议最多60人可开启摄像头,一个屏幕同时容纳9或25人。移动端一个会议最多30人可开启摄像头,一个屏幕同时容纳4人。腾讯会议是腾讯云旗下一款云会议产品,于2019年12月25日发布,具有灵活...

棋牌游戏开发的几大优势是什么

棋牌游戏行业是最近比较盛行的娱乐项目之一,大量的投资商都对该行业产生了浓厚的兴趣,想要从棋牌游戏行业中分一块肉,这个时候找一家好的棋牌游戏开发公司就是非常重要的事情了,国内棋牌游戏开发公司很多,做的好...