PHP生成图片缩略图类示例

admin3年前软件教程66

篇文章介绍了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,因为其已经成为了人们日常生活中的必需品,人们无时无...

bilibili下载的视频存在手机哪一个文件下

大家好,我是80知识网,上述问题将由我为大家进行解答。以哔哩哔哩6.10.0为例,手机bilibili下载的视频在【download】文件夹下。bilibili的特色是悬浮于视频上方的实时评论功能,爱...

手机软件开发细节有哪些

手机软件开发让人们的生活变得多姿多彩。从移动软件的兴起到Uber等现象级app的崛起,手机软件的设计直到现在才渐入佳境。这与社会趋势的变化、新技术的成熟等等因素有关,这也是为什么移动应用到最近才火起来...

抖音动态发布看不到

以抖音12.7.0版本为例,动态发布看不到的原因:1、可能是动态违规被官方封禁,因此无法找到动态。2、可能是网络延迟导致用户无法第一时间看到动态,可以重启软件再查看。3、可能是软件缓存过多导致,可以清...

如何判断app开发公司的实力

判断app开发公司的实力的方法如下:1、团队还是个人。了解一家APP开发公司首先要看这家APP开发公司是团队还是个人,因为一家APP开发公司的规模在一定程度上代表着这家公司的技术水平和开发质量,如果没...

房屋租赁APP开发流程

房屋租赁APP开发流程:1、沟通需求阶段。沟通需求阶段主要是对于APP市场需求,以及是了解用户使用环境和操作流程,与商家对于APP的功能进一步沟通了解需求,确定最终的开发方案。2、产品UI设计。产品原...