本文实例讲述了php生成图片缩略图类。分享给大家供大家参考,具体如下:
class app_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
*/
public function __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
*/
protected function getimageinfo($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
*/
protected function getimageresource($filename) {
$image = null;
if ( is_file($filename) ) {
switch ($this->imgmime) {
case self::$imgminelist['jpeg']:
$image = imagecreatefromjpeg($filename);
break;
case self::$imgminelist['gif']:
$image = imagecreatefromgif($filename);
break;
case self::$imgminelist['png']:
$image = imagecreatefrompng($filename);
break;
case self::$imgminelist['wbmp']:
$image = imagecreatefromwbmp($filename);
break;
default:
break;
}
}
return $image;
}
/**
* 可根据固定宽,等比缩放图片;或根据百分比,等比缩放图片
*
* @param int $width
* @param int $percent
*
* @return array|null
*/
protected function getsizebyscale($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
*/
public function createimage($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;
}
}
ps:这里再为大家推荐几款比较实用的图片处理工具供大家参考使用:
在线图片转换base64工具:
http://tools.jb51.net/transcoding/img2base64
ico图标在线生成工具:
http://tools.jb51.net/aideddesign/ico_img
在线email邮箱图标制作工具:
http://tools.jb51.net/email/emaillogo
在线图片格式转换(jpg/bmp/gif/png)工具:
http://tools.jb51.net/aideddesign/picext
更多关于php相关内容感兴趣的读者可查看本站专题:《php图形与图片操作技巧汇总》、《php基本语法入门教程》、《php运算与运算符用法总结》、《php面向对象程序设计入门教程》、《php网络编程技巧总结》、《php数组(array)操作技巧大全》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》
希望本文所述对大家php程序设计有所帮助。
【说明】:本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:)!