PHP生成图片缩略图类示例

admin3年前软件教程77

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

}

}

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

相关文章

拼多多电力有几个阶段

拼多多电力分三个阶段,需要在拼多多下单大概六个就可以了,不拼东西只靠签到免费领电力是不可能得到东西的,前面两个阶段简单,后面第三个阶段很难。拼多多是上海寻梦信息技术有限公司于2015年9月上线的一家专...

soul注销后别人还会看到你吗

soul注销后别人就看不到你了。账号注销后,注销用户的所有瞬间与聊天记录都会清空,也会从对方关注列表里自动消失,并且不能在与其发消息等。Soul是一款基于心灵的社交App,隶属于上海任意门科技有限公司...

违反钉钉公约禁言几天

钉钉封号封3至15天。一次封禁需要3至15天就可以自动解封了。如果感觉没有违反规定,建议到投诉中心认真按规则申请,如果符合解封条件,会在三天内作出解封处理的。钉钉(DingTalk)是阿里巴巴集团专为...

驾校App制作方案分析

驾校App制作面对的市场需求是驾照考证人员的日益增多和驾校服务的不平衡之间的现象。由于驾照考试的人员的需求在不断的上升,很多用户都想快速的拿到驾照,而驾照似乎也成为了人们生活中的基本的生活技能之一。不...

拼多多的拼小圈如何设置成不让别人看

大家好,我是80知识网,上述问题将由我为大家进行解答。拼多多APP版本5.29.0,拼多多的拼小圈不让别人看的方法如下:1、在手机上打开拼多多。2、进入拼多多后,进入个人中心。3、接下来在打开的页面中...

oracle 10015事件

Oracle是世界上最流行的关系型数据库管理系统之一,它的广泛应用使得它的错误代码也层出不穷。其中,10015事件是常见的一个错误。10015事件通常是由于表空间不足、磁盘 I/O 瓶颈等导致的数据库...