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 相关文章推荐
备份mysql数据库的php代码(一个表一个文件)
May 28 PHP
QQ登录 PHP OAuth示例代码
Jul 20 PHP
linux iconv方法的使用
Oct 01 PHP
php的array_multisort()使用方法介绍
May 16 PHP
Linux下安装PHP MSSQL扩展教程
Oct 24 PHP
Laravel 4 初级教程之视图、命名空间、路由
Oct 30 PHP
php表单敏感字符过滤类
Dec 08 PHP
PHP中使用CURL获取页面title例子
Jan 07 PHP
php获取指定数量随机字符串的方法
Feb 06 PHP
PHP实现深度优先搜索算法(DFS,Depth First Search)详解
Sep 16 PHP
解决Laravel无法使用COOKIE和SESSION的问题
Oct 16 PHP
宝塔面板在NGINX环境中TP5.1如何运行?
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实现手机号码中间四位用星号(*)隐藏的自定义函数分享
2014/09/27 PHP
实例讲解php实现多线程
2019/01/27 PHP
jqPlot Option配置对象详解
2009/07/25 Javascript
Mootools 1.2教程 Tooltips
2009/09/15 Javascript
页面元素绑定jquery toggle后元素隐藏的解决方法
2014/03/27 Javascript
jQuery选择器源码解读(一):Sizzle方法
2015/03/31 Javascript
JavaScript优化专题之Loading and Execution加载和运行
2016/01/20 Javascript
浅谈JS中json数据的处理
2016/06/30 Javascript
Angularjs 设置全局变量的方法总结
2016/10/20 Javascript
微信小程序 五星评分的实现实例
2017/08/04 Javascript
vue+vuex+axios实现登录、注册页权限拦截
2018/03/09 Javascript
jQuery实现判断上传图片类型和大小的方法示例
2018/04/11 jQuery
详解React中setState回调函数
2018/06/14 Javascript
vue2.0的虚拟DOM渲染思路分析
2018/08/09 Javascript
使用jquery Ajax实现上传附件功能
2018/10/23 jQuery
在layui中layer弹出层点击事件无效的解决方法
2019/09/05 Javascript
JS倒计时两种实现方式代码实例
2020/07/27 Javascript
echarts实现晶体球面投影的实例教程
2020/10/10 Javascript
Python 执行字符串表达式函数(eval exec execfile)
2014/08/11 Python
python通过wxPython打开一个音频文件并播放的方法
2015/03/25 Python
Python外星人入侵游戏编程完整版
2020/03/30 Python
python实现将列表中各个值快速赋值给多个变量
2020/04/02 Python
keras训练曲线,混淆矩阵,CNN层输出可视化实例
2020/06/15 Python
Python读取xlsx数据生成图标代码实例
2020/08/12 Python
PyCharm设置注释字体颜色以及是否倾斜的操作
2020/09/16 Python
Camper鞋西班牙官方网上商店:西班牙马略卡岛的鞋类品牌
2019/03/14 全球购物
美国名牌手表折扣网站:Jomashop
2020/05/22 全球购物
护理自荐信
2013/10/22 职场文书
大学毕业生求职自荐书
2014/06/05 职场文书
暖通工程师岗位职责
2014/06/12 职场文书
保安2014年终工作总结
2014/12/06 职场文书
出生证明格式
2015/06/15 职场文书
2016年圣诞节义工活动总结
2016/04/01 职场文书
导游词之镜泊湖
2019/12/09 职场文书
快速学习Oracle触发器和游标
2021/06/30 Oracle
Java实现简单小画板
2022/06/10 Java/Android