PHP生成图片缩略图类示例

admin3年前软件教程76

篇文章介绍了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开发功能特点有:1、农庄类型分类。共享农庄APP整合了各地的农庄商品,人们可以根据农庄的功能模式或者是服务实施不同选择,如一些有钓鱼,果蔬综合性较强的,有的是仅是果蔬。2、农庄详情信息。...

python直接生成注释

Python 是一种极受欢迎的编程语言,它不仅易于学习也有广泛的用途。在 Python 中,注释是一种用于增强代码可读性和可理解性的重要元素。事实上,Python 提供了一种简单而有效的方法来生成注释...

拼多多保证金交多少合适

拼多多保证金如下:1、拼多多入驻的时候会有个保证金的费用,根据类目不同,保证金的价格就不同。2、拼多多代微信收取百分之零点六的交易手续费。拼多多是国内主流的手机购物APP,是一家专注于C2B拼团的第三...

python相关指导书

Python是一门非常流行的编程语言,拥有着丰富的标准库和第三方库,能够广泛应用于数据分析、爬虫、人工智能等方面。对于初学者来说,学习Python可能存在一定的难度,因此有许多Python指导书可以帮...

天猫会员和88vip会员有什么区别

天猫会员和88vip会员区别是:如果淘气值为1000以上,就只要88元就能开通天猫88VIP会员一年,开通后部分商品可以打95折,还有一年的优酷会员、虾米会员、饿了么会员、淘票票年卡等。而天猫会员店没...

oracle 1023

Oracle 10g 是Oracle Corporation为企业级环境开发的一款关系型数据库,具备稳定可靠、高效快速等特点。其中Oracle 10.2.0.3是常用版本之一,今天我们来探讨一下这个版...