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语法学习笔记1
Dec 17 PHP
php 无限极分类
Mar 27 PHP
将数组写入txt文件 var_export
Apr 21 PHP
php 深入理解strtotime函数的使用详解
May 23 PHP
探讨:如何编写PHP扩展
Jun 13 PHP
如何在Ubuntu下启动Apache的Rewrite功能
Jul 05 PHP
利用php获得flv视频长度的实例代码
Oct 26 PHP
PHP实现支持CURL字符串证书传输的方法
Mar 23 PHP
从ThinkPHP3.2.3过渡到ThinkPHP5.0学习笔记图文详解
Apr 03 PHP
php session_decode函数用法讲解
May 26 PHP
PHP中SESSION过期设置
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 SPL标准库的用法(遍历目录,查找固定条件的文件)
2013/06/18 PHP
PHP代码优化的53个细节
2014/03/03 PHP
php可生成缩略图的文件上传类实例
2014/12/17 PHP
PHP基于GD库实现的生成图片缩略图函数示例
2017/07/05 PHP
php中关于换行的实例写法
2019/09/26 PHP
thinkphp 框架数据库切换实现方法分析
2020/05/18 PHP
php中yar框架实例用法讲解
2020/12/27 PHP
div移动 输入框不能输入的问题
2009/11/19 Javascript
jquery easyui中treegrid用法的简单实例
2014/02/18 Javascript
基于jquery实现左右按钮点击的图片切换效果
2021/01/27 Javascript
vue 将页面公用的头部组件化的方法
2017/12/18 Javascript
微信小程序 scroll-view 水平滚动实现过程解析
2019/10/12 Javascript
原生JS实现九宫格抽奖
2020/09/13 Javascript
Vue+element-ui添加自定义右键菜单的方法示例
2020/12/08 Vue.js
[01:20]DOTA2 齐天大圣至宝动态展示
2016/12/13 DOTA
Python使用scrapy抓取网站sitemap信息的方法
2015/04/08 Python
Python 中开发pattern的string模板(template) 实例详解
2017/04/01 Python
python数字图像处理实现直方图与均衡化
2018/05/04 Python
解决pycharm运行时interpreter为空的问题
2018/10/29 Python
pip安装python库的方法总结
2019/08/02 Python
python查看数据类型的方法
2019/10/12 Python
Python 如何创建一个线程池
2020/07/28 Python
css3实现波纹特效、H5实现动态波浪效果
2018/01/31 HTML / CSS
AmazeUI 按钮交互的实现示例
2020/08/24 HTML / CSS
医学专业毕业生个人的求职信
2013/12/04 职场文书
党校学习自我鉴定
2014/02/24 职场文书
《石榴》教学反思
2014/03/02 职场文书
党员一句话承诺大全
2014/03/28 职场文书
创先争优承诺书范文
2014/03/31 职场文书
我的中国梦演讲稿800字
2014/08/19 职场文书
导游词300字
2015/02/13 职场文书
工作态度恶劣检讨书
2015/05/06 职场文书
2016年教代会开幕词
2016/03/04 职场文书
职场新人刚入职工作总结该怎么写?
2019/05/15 职场文书
Python echarts实现数据可视化实例详解
2022/03/03 Python
Nginx隐藏式跳转(浏览器URL跳转后保持不变)
2022/04/07 Servers