|
本文实例讲述了php实现的支持imagemagick及gd库两种处理的缩略图生成类及其用法实例,非常具有实用价值。分享给大家供大家参考。具体如下:
一、功能:
1.按比例缩小/放大
2.填充背景色
3.按区域裁剪
4.添加水印,包括水印的位置,透明度等
使用imagemagick/GD库实现,imagemagick地址:www.imagemagick.org
需要安装imagemagick,安装方法如下:
二、实现方法:
PicThumb.class.php类文件如下:
class PicThumb{ // class start
private $_log = null; // log file
private $_handler = null; // 进行图片处理的程序,imagemagick/gd库
private $_type = 'fit'; // fit or crop
private $_source = null; // 原图路径
private $_dest = null; // 缩略图路径
private $_watermark = null; // 水印图片
private $_opacity = 75; // 水印圖片透明度,gd库不支持
private $_gravity = 'SouthEast'; // 水印摆放位置 NorthWest,North,NorthEast,West,Center,East,SouthWest,South,SouthEast
private $_geometry = '+10+10'; // 水印定位,gd库不支持
private $_croppos = 'TL'; // 截图的位置 TL TM TR ML MM MR BL BM BR
private $_bgcolor = null; // 填充的背景色
private $_quality = 90; // 生成的图片质量
private $_width = null; // 指定区域宽度
private $_height = null; // 指定区域高度
// 初始化
public function __construct($logfile=''){
if($logfile!=''){
$this->_log = $logfile;
}
}
// 设置参数
public function set_config($param=array()){
$this->_handler = $this->exists($param,'handler')? strtolower($param['handler']) : null;
$this->_type = $this->exists($param,'type')? strtolower($param['type']) : 'fit';
$this->_watermark = $this->exists($param,'watermark')? $param['watermark'] : null;
$this->_opacity = $this->exists($param,'opacity')? $param['opacity'] : 75;
$this->_gravity = $this->exists($param,'gravity')? $param['gravity'] : 'SouthEast';
$this->_geometry = $this->exists($param,'geometry')? $param['geometry'] : '+10+10';
$this->_croppos = $this->exists($param,'croppos')? $param['croppos'] : 'TL';
$this->_bgcolor = $this->exists($param,'bgcolor')? $param['bgcolor'] : null;
$this->_quality = $this->exists($param,'quality')? $param['quality'] : 90;
$this->_width = $this->exists($param,'width')? $param['width'] : null;
$this->_height = $this->exists($param,'height')? $param['height'] : null;
}
/** 创建缩略图
-
@param String $source 原图
-
@param String $dest 目标图
-
@return boolean
*/
public function create_thumb($source,$dest){
// 检查使用的handler是否已安装
if(!$this->check_handler()){
$this->to_log('handler not installed');
return false;
}
// 判断区域宽高是否正确
if(!is_numeric($this->_width) || !is_numeric($this->_height) || $this->_width<=0 || $this->_height<=0){
$this->to_log('width or height invalid');
return false;
}
// 判断源文件是否存在
if(!file_exists($source)){
$this->to_log($source.' not exists');
return false;
}
// 创建目标文件路径
if(!$this->create_dirs($dest)){
$this->to_log(dirname($dest).' create fail');
return false;
}
$this->_source = $source; // 源文件
$this->_dest = $dest; // 目标文件
// 处理图片
switch($this->_type){
case 'fit':
if($this->_handler=='imagemagick'){
return $this->fit();
}else{
return $this->gd_fit();
}
break;
case 'crop':
if($this->_handler=='imagemagick'){
return $this->crop();
}else{
return $this->gd_crop();
}
break;
default:
$this->to_log($this->_type.' not fit and crop');
return false;
}
}
/** 按比例压缩或拉伸图片
-
@return boolean
*/
private function fit(){
// 判断是否填充背景
$bgcolor = ($this->_bgcolor!=null)?
sprintf(" -background '%s' -gravity center -extent '%sx%s' ",$this->_bgcolor,$this->_width,$this->_height) : "";
// 判断是否要转为RGB
$source_info = getimagesize($this->_source);
$colorspace = (!isset($source_info['channels']) || $source_info['channels']!=3)? ' -colorspace RGB ' : '';
// 命令行
$cmd = sprintf("convert -resize '%sx%s' '%s' %s -quality %s %s '%s'",$this->_height,$this->_source,$bgcolor,$this->_quality,$colorspace,$this->_dest);
// 记录执行的命令
$this->to_log($cmd);
// 执行命令
exec($cmd);
// 添加水印
$this->add_watermark($this->_dest);
return is_file($this->_dest)? true : false;
}
/** 裁剪图片
-
@return boolean
*/
private function crop(){
// 获取生成的图片尺寸
list($pic_w,$pic_h) = $this->get_size();
// 获取截图的偏移量
list($offset_w,$offset_h) = $this->get_crop_offset($pic_w,$pic_h);
// 判断是否要转为RGB
$source_info = getimagesize($this->_source);
$colorspace = (!isset($source_info['channels']) || $source_info['channels']!=3)? ' -colorspace RGB ' : '';
// 命令行
$cmd = sprintf("convert -resize '%sx%s' '%s' -quality %s %s -crop %sx%s+%s+%s +repage '%s'",$pic_w,$pic_h,$offset_w,$offset_h,$this->_dest);
// 记录执行的命令
$this->to_log($cmd);
// 执行命令
exec($cmd);
// 添加水印
$this->add_watermark($this->_dest);
return is_file($this->_dest)? true : false;
}
/** GD库按比例压缩或拉伸图片
-
@return boolean
*/
private function gd_fit(){
// 获取生成的图片尺寸
list($pic_w,$pic_h) = $this->get_size();
list($owidth,$oheight,$otype) = getimagesize($this->_source);
switch($otype){
case 1: $source_img = imagecreatefromgif($this->_source); break;
case 2: $source_img = imagecreatefromjpeg($this->_source); break;
case 3: $source_img = imagecreatefrompng($this->_source); break;
default: return false;
}
// 按比例缩略/拉伸图片
$new_img = imagecreatetruecolor($pic_w,$pic_h);
imagecopyresampled($new_img,$source_img,$owidth,$oheight);
// 判断是否填充背景
if($this->_bgcolor!=null){
$bg_img = imagecreatetruecolor($this->_width,$this->_height);
$rgb = $this->hex2rgb($this->_bgcolor);
$bgcolor =imagecolorallocate($bg_img,$rgb['r'],$rgb['g'],$rgb['b']);
imagefill($bg_img,$bgcolor);
imagecopy($bg_img,$new_img,(int)(($this->_width-$pic_w)/2),(int)(($this->_height-$pic_h)/2),$pic_h);
$new_img = $bg_img;
}
// 获取目标图片的类型
$dest_ext = $this->get_file_ext($this->_dest);
// 生成图片
switch($dest_ext){
case 1: imagegif($new_img,$this->_dest,$this->_quality); break;
case 2: imagejpeg($new_img,$this->_quality); break;
case 3: imagepng($new_img,(int)(($this->_quality-1)/10)); break;
}
if(isset($source_img)){
imagedestroy($source_img);
}
if(isset($new_img)){
imagedestroy($new_img);
}
// 添加水印
$this->add_watermark($this->_dest);
return is_file($this->_dest)? true : false;
}
/** GD库裁剪图片
-
@return boolean
*/
private function gd_crop(){
// 获取生成的图片尺寸
list($pic_w,$pic_h);
list($owidth,$otype) = getimagesize($this->_source);
(编辑:安卓应用网)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|