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 相关文章推荐
如何将一个表单同时提交到两个地方处理
Oct 09 PHP
php中stream(流)的用法
Mar 25 PHP
PHP按行读取、处理较大CSV文件的代码实例
Apr 09 PHP
php中in_array函数用法分析
Nov 15 PHP
php实现字符串翻转的方法
Mar 27 PHP
PHP实现简单数字分页效果
Jul 26 PHP
CodeIgniter配置之autoload.php自动加载用法分析
Jan 20 PHP
CI分页类首页、尾页不显示的解决方法
Mar 28 PHP
centos+php+coreseek+sphinx+mysql之一coreseek安装篇
Oct 25 PHP
PHP简单装饰器模式实现与用法示例
Jun 22 PHP
php在windows环境下获得cpu内存实时使用率(推荐)
Feb 08 PHP
PHP实现数据库的增删查改功能及完整代码
Apr 18 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
删除及到期域名的查看(抢域名必备哦)
2008/05/14 PHP
分享下页面关键字抓取www.icbase.com站点代码(带asp.net参数的)
2014/01/30 PHP
php实现保存submit内容之后禁止刷新
2014/03/19 PHP
smarty模板引擎中变量及变量修饰器用法实例
2015/01/22 PHP
php强制下载文件函数
2016/08/24 PHP
php版微信公众平台接口参数调试实现判断用户行为的方法
2016/09/23 PHP
简述php环境搭建与配置
2016/12/05 PHP
PHP中的函数声明与使用详解
2017/05/27 PHP
Ajax+PHP实现的分类列表框功能示例
2019/02/11 PHP
PHP中的异常处理机制深入讲解
2020/11/10 PHP
看了就知道什么是JSON
2007/12/09 Javascript
jquery 操作表格实现代码(多种操作打包)
2011/03/20 Javascript
jquery用data方法获取某个元素上的事件
2014/06/23 Javascript
javascript函数式编程实例分析
2015/04/25 Javascript
JS触摸事件、手势事件详解
2017/05/04 Javascript
阿里大于短信验证码node koa2的实现代码(最新)
2017/09/07 Javascript
vue富文本框(插入文本、图片、视频)的使用及问题小结
2018/08/17 Javascript
vue+axios实现文件下载及vue中使用axios的实例
2018/09/21 Javascript
详解Vue的组件中data选项为什么必须是函数
2020/08/17 Javascript
[03:17]2014DOTA2 国际邀请赛中国区预选赛 四强专访
2014/05/23 DOTA
[02:26]2016国际邀请赛8月3日开战 中国军团出征西雅图
2016/08/02 DOTA
简单的编程0基础下Python入门指引
2015/04/01 Python
Python 实现取矩阵的部分列,保存为一个新的矩阵方法
2018/11/14 Python
python 自动轨迹绘制的实例代码
2019/07/05 Python
python FTP批量下载/删除/上传实例
2019/12/22 Python
python基于socket函数实现端口扫描
2020/05/28 Python
怎么写有吸引力的自荐信
2013/11/17 职场文书
人事主管岗位职责
2014/01/30 职场文书
促销活动方案模板
2014/02/24 职场文书
中式婚礼主持词
2014/03/13 职场文书
就业协议书怎么填
2014/09/15 职场文书
平面设计师岗位职责
2014/09/18 职场文书
2014幼儿园中班工作总结
2014/11/10 职场文书
pytorch 实现在测试的时候启用dropout
2021/05/27 Python
忆童年!用Python实现愤怒的小鸟游戏
2021/06/07 Python
VS2019连接MySQL数据库的过程及常见问题总结
2021/11/27 MySQL