PHP 处理图片的类实现代码


Posted in PHP onOctober 23, 2009
<?php 
/** 
* author:yagas 
* email:yagas60@21cn.com 
*/ 
class Image 
{ 
/** 类保护变量 */ 
protected $th_width = 100; 
protected $th_height = 50; 
protected $quality = 85; //图片质量 
protected $transparent = 50; //水印透明度 
protected $background = "255,255,255"; //背景颜色 
/** 
* 生成缩略图文件 
* @param $src 原图文件 
* @param $dst 目标文件 
*/ 
public function thumb($src, $dst=null, $output=true) 
{ 
$thumb = array($this->th_width, $this->th_height); 
$this->scale($src, $thumb, $dst, $output); 
} 
/** 
* 对图片按百分比进行缩放处理 
* @param string $src 原图文件 
* @param string $dst 输入的目标文件 
* @param float/array $zoom 缩放比例,浮点类型时按百分比绽放,数组类型时按指定大小时行缩放 
* @param boolean $output 是否生成文件输出 
*/ 
public function scale($src, $dst=null, $zoom=1, $output=true) 
{ 
if(!file_exists($src)) die('File not exists.'); 
if(!$zoom) die('the zoom undefine.'); 
$src_im = $this->IM($src); 
$old_width = imagesx($src_im); 
if(is_float($zoom)) { 
//按百分比进行缩放 
$new_width = $old_width * $zoom; 
} 
elseif(is_array($zoom)) { 
//明确的缩放尺寸 
$new_width = $zoom[0]; 
} 
//是否定义的缩放的高度 
if(!isset($zoom[1])) { 
//等比例缩放 
$resize_im = $this->imageresize($src_im, $new_width); 
} 
else { 
//非等比例缩放 
$resize_im = $this->imageresize($src_im, $new_width, $zoom[1]); 
} 
if(!$output) { 
header("Content-type: image/jpeg"); 
imagejpeg($resize_im, null, $this->quality); 
} 
else { 
$new_file = empty($dst)? $src:$dst; 
imagejpeg($resize_im, $new_file, $this->quality); 
} 
imagedestroy($im); 
imagedestroy($nIm); 
} 
/** 
* 对图片进行裁切 
* @param $src 原始文件 
* @param $dst 目标文件 
* @param $output 是否生成目标文件 
*/ 
public function capture($src, $dst=null, $output=true) { 
if(!file_exists($src)) die('File not exists.'); 
$width = $this->th_width; 
$height = $this->th_height; 
$src_im = $this->IM($src); 
$old_width = imagesx($src_im); 
$old_height = imagesy($src_im); 
$capture = imagecreatetruecolor($width, $height); 
$rgb = explode(",", $this->background); 
$white = imagecolorallocate($capture, $rgb[0], $rgb[1], $rgb[2]); 
imagefill($capture, 0, 0, $white); 
//当图片大于缩略图时进行缩放 
if($old_width > $width && $old_height>$height) { 
$resize_im = $this->imageresize($src_im, $width); 
//图片比例不合规范时,重新计算比例进行裁切 
if(imagesy($resize_im) < $height) { 
$proportion = $old_height/$this->th_height; 
$resize_im = $this->imageresize($src_im, $old_width/$proportion); 
} 
$posx = 0; 
$posy = 0; 
} 
else { 
//图片小于缩略图时将图片居中显示 
$posx = ($width-$old_width)/2; 
$posy = ($height-$old_height)/2; 
$resize_im = $src_im; 
} 
imagecopy($capture, $resize_im, $posx, $posy, 0, 0, imagesx($resize_im), imagesy($resize_im)); 
if(!$output) { 
header("Content-type: image/jpeg"); 
imagejpeg($capture, null, $this->quality); 
} 
else { 
$new_file = empty($dst)? $src:$dst; 
imagejpeg($capture, $new_file, $this->quality); 
} 
imagedestroy($src_im); 
@imagedestroy($resize_im); 
imagedestroy($capture); 
} 
/** 
* 写入水印图片 
* @param $src 需要写入水印的图片 
* @param $mark 水印图片 
* @param $transparent 水印透明度 
*/ 
public function mark($src, $mark, $dst='', $output=true) 
{ 
$mark_info = getimagesize($mark); 
$src_info = getimagesize($src); 
list($mw,$mh) = $mark_info; 
list($sw,$sh) = $src_info; 
$px = $sw - $mw; 
$py = $sh - $mh; 
$im = $this->IM($src); 
$mim = $this->IM($mark); 
imagecopymerge($im, $mim, $px, $py, 0, 0, $mw, $mh, $this->transparent); 
if($output){ 
$new_file = empty($dst)? $src:$dst; 
imagejpeg($im, $new_file, $this->quality); 
} 
else 
{ 
header('Content-type: image/jpeg'); 
imagejpeg($im); 
} 
imagedestroy($im); 
imagedestroy($mim); 
} 
/** 
* 通过文件,获取不同的GD对象 
*/ 
protected function IM($file) 
{ 
if(!file_exists($file)) die('File not exists.'); 
$info = getimagesize($file); 
switch($info['mime']) 
{ 
case 'image/gif': 
$mim = imagecreatefromgif($file); 
break; 
case 'image/png': 
$mim = imagecreatefrompng($file); 
imagealphablending($mim, false); 
imagesavealpha($mim, true); 
break; 
case 'image/jpeg': 
$mim = imagecreatefromjpeg($file); 
break; 
default: 
die('File format errors.'); 
} 
return $mim; 
} 
/** 
* 对图片进行缩放的处理 
* @param resource $src_im 图像GD对象 
* @param integer $width 图片的宽度 
* @param integer $height 图片的高度,如果不设置高度,将对图片进行等比例缩放 
* @return resuorce $im 返回一个GD对象 
*/ 
protected function imageresize($src_im, $width, $height=null) { 
$old_width = imagesx($src_im); 
$old_height = imagesy($src_im); 
$proportion = $old_width/$old_height; 
$new_width = $width; 
$new_height = is_null($height)? round($new_width / $proportion):$height; 
//创建新的图象并填充默认的背景色 
$im = imagecreatetruecolor($new_width, $new_height); 
$rgb = explode(",", $this->background); 
$white = imagecolorallocate($im, $rgb[0], $rgb[1], $rgb[2]); 
imagefill($im, 0, 0, $white); 
//对图片进行缩放 
imagecopyresized($im, $src_im, 0, 0, 0, 0, $new_width, $new_height, $old_width, $old_height); 
return $im; 
} 
/** 
* 类变量赋值 
*/ 
public function __set($key, $value) 
{ 
$this->$key = $value; 
} 
/** 
* 获取类变量值 
*/ 
public function __get($key) 
{ 
return $this->$key; 
} 
} 
?>
PHP 相关文章推荐
模仿OSO的论坛(一)
Oct 09 PHP
使用php重新实现PHP脚本引擎内置函数
Mar 06 PHP
Zend Studio 无法启动的问题解决方法
Dec 04 PHP
php eval函数用法 PHP中eval()函数小技巧
Oct 31 PHP
php实现天干地支计算器示例
Mar 14 PHP
PHP调用Linux命令权限不足问题解决方法
Feb 07 PHP
php实现的简易扫雷游戏实例
Jul 09 PHP
PHP mysql事务问题实例分析
Jan 18 PHP
php利用imagemagick实现复古老照片效果实例
Feb 16 PHP
Laravel 5.4重新登录实现跳转到登录前页面的原理和方法
Jul 13 PHP
CMSPRESS 10行代码搞定 PHP无限级分类2
Mar 30 PHP
PHP7移除的扩展和SAPI
Mar 09 PHP
PHP教程 变量定义
Oct 23 #PHP
PHP教程 基本语法
Oct 23 #PHP
php self,$this,const,static,-&amp;gt;的使用
Oct 22 #PHP
PHP 长文章分页函数 带使用方法,不会分割段落,翻页在底部
Oct 22 #PHP
Wordpress php 分页代码
Oct 21 #PHP
PHP字符串 ==比较运算符的副作用
Oct 21 #PHP
php 3行代码的分页算法(求起始页和结束页)
Oct 21 #PHP
You might like
PHP下编码转换函数mb_convert_encoding与iconv的使用说明
2009/12/16 PHP
搭建Vim为自定义的PHP开发工具的一些技巧
2015/12/11 PHP
PHP连接MYSQL数据库实例代码
2016/01/20 PHP
PHP PDOStatement::closeCursor讲解
2019/01/30 PHP
Laravel框架Blade模板简介及模板继承用法分析
2019/12/03 PHP
HR vs CL BO3 第一场 2.13
2021/03/10 DOTA
jquery 批量上传图片实现代码
2010/01/28 Javascript
safari,opera嵌入iframe页面cookie读取问题解决方法
2010/06/23 Javascript
js精度溢出解决方案
2012/12/02 Javascript
用js写了一个类似php的print_r输出换行功能
2013/02/18 Javascript
jQuery隔行变色与普通JS写法的对比
2013/04/21 Javascript
jQuery快速上手:写jQuery与直接写JS的区别详细解析
2013/08/26 Javascript
javascript日期格式化示例分享
2014/03/05 Javascript
基于jQuery Bar Indicator 插件实现进度条展示效果
2015/09/30 Javascript
不用一句js代码初始化组件
2016/01/27 Javascript
基于JavaScript代码实现自动生成表格
2016/06/15 Javascript
在js里怎么实现Xcode里的callFuncN方法(详解)
2016/11/05 Javascript
简单实现js浮动框
2016/12/13 Javascript
js仿拉勾网首页穿墙广告效果
2017/03/08 Javascript
微信小程序时间标签和时间范围的联动效果
2019/02/15 Javascript
Python编程之属性和方法实例详解
2015/05/19 Python
numpy.delete删除一列或多列的方法
2018/04/03 Python
Python中的heapq模块源码详析
2019/01/08 Python
python scipy卷积运算的实现方法
2019/09/16 Python
Pandas —— resample()重采样和asfreq()频度转换方式
2020/02/26 Python
基于python实现数组格式参数加密计算
2020/04/21 Python
python 爬虫基本使用——统计杭电oj题目正确率并排序
2020/10/26 Python
html5读取本地文件示例代码
2014/04/22 HTML / CSS
canvas压缩图片以及卡片制作的方法示例
2018/12/04 HTML / CSS
跑步爱好者一站式服务网站:Jack Rabbit
2016/09/01 全球购物
佳能法国商店:Canon法国
2019/02/14 全球购物
会计人员岗位职责
2014/03/19 职场文书
医院领导班子四风对照检查材料
2014/09/27 职场文书
群众路线四风问题整改措施
2014/09/27 职场文书
解决python绘图使用subplots出现标题重叠的问题
2021/04/30 Python
Js类的构建与继承案例详解
2021/09/15 Javascript