PHP 处理图片的类实现代码

 更新时间:2009年10月23日 21:35:58   作者:  
PHP 处理图片的类实现代码,需要的朋友可以参考下。
复制代码 代码如下:

<?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多重接口的实现方法

    php多重接口的实现方法

    这篇文章主要介绍了php多重接口的实现方法,实例分析了php多重接口的定义与使用技巧,需要的朋友可以参考下
    2015-06-06
  • PHP数组基本用法与知识点总结

    PHP数组基本用法与知识点总结

    这篇文章主要介绍了PHP数组基本用法与知识点,总结整理了PHP数组基本概念、用法、定义、访问、删除等相关操作技巧,需要的朋友可以参考下
    2020-06-06
  • 20个PHP常用类库小结

    20个PHP常用类库小结

    下面是一些非常有用的PHP类库,相信一定可以为你的WEB开发提供更好和更为快速的方法。
    2011-09-09
  • 详细分析PHP 命名空间(namespace)

    详细分析PHP 命名空间(namespace)

    这篇文章主要介绍了PHP 命名空间(namespace)的的相关资料,文中讲解非常详细,实例代码帮助大家更好的理解和学习,感兴趣的朋友可以了解下
    2020-06-06
  • 服务器变量 $_SERVER 的深入解析

    服务器变量 $_SERVER 的深入解析

    本篇文章是对服务器变量$_SERVER进行了详细的分析介绍,需要的朋友参考下
    2013-07-07
  • Windows上PHP安装redis扩展和igbinary扩展

    Windows上PHP安装redis扩展和igbinary扩展

    php扩展就是php核心并不支持的功能,通过安装扩展增加PHP的功能,在Windows上有两种加载PHP扩展的方式:把扩展编译进 PHP,或者加载 DLL,加载预编译的扩展是更简单更被推荐的方式,要加载某扩展,需要在系统中有其相对应的“.dll”文件
    2023-10-10
  • PHP记录页面停留时间的方法

    PHP记录页面停留时间的方法

    这篇文章主要介绍了PHP记录页面停留时间的方法,涉及PHP结合js针对文件与时间的相关操作技巧,需要的朋友可以参考下
    2016-03-03
  • phpinfo()中Loaded Configuration File(none)的解决方法

    phpinfo()中Loaded Configuration File(none)的解决方法

    这篇文章主要给大家介绍了phpinfo()中Loaded Configuration File(none)问题的解决方法,需要的朋友可以参考借鉴,下面来一起看看吧。
    2017-01-01
  • PHP设计模式 注册表模式

    PHP设计模式 注册表模式

    注册表模式其实是一个单例模式,注册表类提供静态方法(或单例对象的实例化方法)来让其它对象访问其中的数据(通常是对象)。整个系统中的每个对象都可以访问这些数据对象
    2012-02-02
  • PHP中创建和验证哈希的简单方法实探

    PHP中创建和验证哈希的简单方法实探

    这篇文章主要介绍了PHP中创建和验证哈希的简单方法,即为Password Hashing API的使用介绍,需要的朋友可以参考下
    2015-07-07

最新评论