PHP生成图片缩略图类示例

admin3年前软件教程121

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

}

}

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

相关文章

钉钉回放老师能看见吗

只要用户使用钉钉,有没有看直播,什么时候看回放,看了多长时间,管理员都是可以看到的,所以钉钉直播回放是有记录的。钉钉(DingTalk)是阿里巴巴集团专为中国企业打造的免费沟通和协同的多端平台,提供P...

macos 12自带的投屏

最近,随着macOS 12的推出,许多用户发现新版本的操作系统自带了一项令人兴奋的功能 - 投屏。通过这项功能,用户可以轻松地将MacBook的屏幕投射到其他设备上,例如电视、投影仪或其他具备HDMI...

爱奇艺微博登录在哪里

以爱奇艺11.9.0为例,爱奇艺微博登录的方法如下:1、打开手机爱奇艺,点击我的,点击登录。 2、然后选择微博登录即可。 北京爱奇艺科技有限公司(iQIYI)是一家独立视频服务公司,由龚宇于2010年...

秘乐短视频是干什么的

秘乐短视频是一款短视频软件,软件中拥有着八卦、热点、影视、搞笑、奇谈趣闻、动漫等资源。秘乐短视频立志于帮助大众发现更有趣的世界、连接有趣的人和世界的短视频分享创造平台,可以记录美好生活、可以发现更多、...

牙医APP开发具备哪些特色服务

我国5岁组乳牙、12岁组恒牙、35岁至44岁组、65岁至74岁组的龋齿患病率分别为66%、28.9%、88.1%和98.4%。 在看来,高患病率形成鲜明对比的是,目前国内口腔疾病患者的就诊率非常低。牙...

商城购物app如何开发

商城购物app开发可通过以下两点开发:1、商家后台管理。2、用户端。软件开发是根据用户要求建造出软件系统或者系统中的软件部分的过程。软件开发是一项包括需求捕捉、需求分析、设计、实现和测试的系统工程。软...