PHP生成图片缩略图类示例

admin3年前软件教程50

篇文章介绍了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,是一家专注于C2B拼团...

乐播投屏可以看直播电视吗

乐播投屏可以看直播电视,乐播投屏就是把手机屏幕投送到电视上,手机播放什么就投屏什么,其本质是多屏互动,所以可以实现直播。乐播投屏,是一款连接移动设备(手机、平板等)与大屏终端(电视、盒子、投影、VR等...

探探必须用真照片吗

探探必须用真照片,不然系统会屏蔽掉,探探简单的动作设置是喜欢往右滑,不喜欢往左滑,使得用户体验极好。探探是由探探科技(北京)有限公司于2014年5月发布的一款社交应用。它根据用户的个人资料、位置、兴趣...

为什么语玩登录不了

语玩登录不了的原因可能是网络遭受攻击。耐心等待官方恢复;也可能是语玩系统崩溃了。语玩-真实的同城陌生交友软件。作为一款多人实时的交友软件,语玩主打用声音社交,为用户搭建一个同城陌生语音交友平台。在此理...

钉钉打卡能定位行踪吗

可以的。因为只要那个人签到,系统会自动定位它签到的地理位置。钉钉签到可以随时了解外勤人员分布。签到记录时间和位置,并可拍照打卡。签到时可选择拜访的客户,记录客户拜访过程。查看签到足迹。查看团队签到地图...

UC第三方视频是否免流

UC第三方视频不免流,但是有定向流量,可以激活UC免流服务后,使用UC浏览所有网页及观看视频、刷资讯、下载应用及文件等服务。UC浏览器(UC Browser)是UC Mobile Limited在20...