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
PHP4实际应用经验篇(6)
Oct 09 PHP
PHP第一季视频教程(李炎恢+php100 不断更新)
May 29 PHP
php流量统计功能的实现代码
Sep 29 PHP
ubuntu10.04配置 nginx+php-fpm模式的详解
Jun 03 PHP
php压缩和解压缩字符串的方法
Mar 14 PHP
php创建桌面快捷方式实现方法
Dec 31 PHP
PHP实践教程之过滤、验证、转义与密码详解
Jul 24 PHP
php readfile()修改文件上传大小设置
Aug 11 PHP
php服务器的系统详解
Oct 12 PHP
php判断数组是否为空的实例方法
May 10 PHP
win10下 php安装seaslog扩展的详细步骤
Dec 04 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
phpmyadmin 常用选项设置详解版
2010/03/07 PHP
腾讯QQ php程序员面试题目整理
2010/06/08 PHP
探讨Smarty中如何获取数组的长度以及smarty调用php函数的详解
2013/06/20 PHP
老生常谈PHP数组函数array_merge(必看篇)
2017/05/25 PHP
YII2框架使用控制台命令的方法分析
2020/03/18 PHP
tp5.1 框架join方法用法实例分析
2020/05/26 PHP
jquery 获取dom固定元素 添加样式的简单实例
2014/02/04 Javascript
div失去焦点事件实现思路
2014/04/22 Javascript
深入探讨前端框架react
2015/12/09 Javascript
JavaScript中的原型prototype完全解析
2016/05/10 Javascript
JS正则表达式判断有效数实例代码
2017/03/13 Javascript
ajax实现加载页面、删除、查看详细信息 bootstrap美化页面!
2017/03/14 Javascript
Ionic3 UI组件之autocomplete详解
2017/06/08 Javascript
jQuery插件实现非常实用的tab栏切换功能【案例】
2019/02/18 jQuery
利用不到200行代码写一款属于你自己的js类库
2019/07/08 Javascript
在Python中使用M2Crypto模块实现AES加密的教程
2015/04/08 Python
用pandas按列合并两个文件的实例
2018/04/12 Python
Python OOP类中的几种函数或方法总结
2019/02/22 Python
Python I/O与进程的详细讲解
2019/03/08 Python
Python 网络编程之UDP发送接收数据功能示例【基于socket套接字】
2019/10/11 Python
PyQt5 如何让界面和逻辑分离的方法
2020/03/24 Python
python except异常处理之后不退出,解决异常继续执行的实现
2020/04/25 Python
解决运行django程序出错问题 'str'object has no attribute'_meta'
2020/07/15 Python
一文带你了解Python 四种常见基础爬虫方法介绍
2020/12/04 Python
MoviePy常用剪辑类及Python视频剪辑自动化
2020/12/18 Python
用gpu训练好的神经网络,用tensorflow-cpu跑出错的原因及解决方案
2021/03/03 Python
香港化妆品经销商:我的公主
2016/08/05 全球购物
美国大尺码女装零售商:TORRID
2016/10/01 全球购物
澳大利亚头发和美容产品购物网站:OZ Hair & Beauty
2020/03/27 全球购物
思想品德自我鉴定
2013/10/12 职场文书
自荐信怎么写呢?
2013/12/09 职场文书
小学教师评语大全
2014/04/23 职场文书
2016学习依法治国心得体会
2016/01/15 职场文书
Nginx stream 配置代理(Nginx TCP/UDP 负载均衡)
2021/11/17 Servers
Python实现为PDF去除水印的示例代码
2022/04/03 Python
Python实现将多张图片合成MP4视频并加入背景音乐
2022/04/28 Python