欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

php图像处理类实例

程序员文章站 2023-12-03 17:13:10
本文实例讲述了php图像处理类。分享给大家供大家参考。具体如下:

本文实例讲述了php图像处理类。分享给大家供大家参考。具体如下:

<?php
/**
 * image 类
 */
class image {
 /**
 * @var string $filename 文件名
 * @access private
 */
 private $filename = '';
 /**
 * @var gd resource $imageresource 原图像
 * @access private
 */
 private $imageresource = null;
 /**
 * @var int $imagewidth 原图像宽
 * @access private
 */
 private $imagewidth = null;
 /**
 * @var int $imageheight 原图像高
 * @access private
 */
 private $imageheight = null;
 /**
 * @var int $imagetype 原图像类型
 * @access private
 */
 private $imagetype = null;
 /**
 * @var int $imagewidth 原图像宽
 * @access private
 */
 public $width = null;
 /**
 * @var int $imageheight 原图像高
 * @access private
 */
 public $height = null;
 /**
 * @var int $imagetype 原图像类型
 * @access private
 */
 public $type = null;
 /**
 * @var int $newresource 新图像
 * @access private
 */
 private $newresource = null;
 /**
 * @var int $newrestype 新图像类型
 * @access private
 */
 private $newrestype = null;
 /**
 * 构造函数
 * @param string $filename 文件名
   */
 public function __construct($filename = null) {
 $this->filename = $filename;
 if ($this->filename) {
 $this->getsrcimageinfo();
 }
 }
 /**
 * 取源图像信息
 * @access private
 * @return void
 */
 private function getsrcimageinfo() {
 $info = $this->getimageinfo();
 $this->imagewidth = $info[0];
 $this->imageheight = $info[1];
 $this->imagetype = $info[2];
 $this->width = $info[0];
 $this->height = $info[1];
 $this->type = $info[2];
 }
 /**
 * 取图像信息
 * @param string $filename 文件名
 * @access private
 * @return array
 */
 private function getimageinfo($filename = null) {
 if ($filename==null) {
 $filename = $this->filename;
 }
 $info = getimagesize($filename);
 return $info;
 }
 /**
 * 创建源图像gd 资源
 * @access private
 * @return void
 */
 private function createsrcimage () {
 $this->imageresource = $this->createimagefromfile();
 }
 /**
 * 跟据文件创建图像gd 资源
 * @param string $filename 文件名
 * @return gd resource
 */
 public function createimagefromfile($filename = null)
 {
 if (!$filename) {
 $filename = $this->filename;
 $imgtype = $this->imagetype;
 }
 if (!is_readable($filename) || !file_exists($filename)) {
  throw new exception('unable to open file "' . $filename . '"');
 }
 if (!$imgtype) {
 $imageinfo = $this->getimageinfo($filename);
 $imgtype = $imageinfo[2];
 }
 switch ($imgtype) {
 case imagetype_gif:
 $tempresource = imagecreatefromgif($filename);
 break;
 case imagetype_jpeg:
 $tempresource = imagecreatefromjpeg($filename);
 break;
 case imagetype_png:
 $tempresource = imagecreatefrompng($filename);
 break;
 case imagetype_wbmp:
 $tempresource = imagecreatefromwbmp($filename);
 break;
 case imagetype_xbm:
 $tempresource = imagecreatefromxbm($filename);
 break;
 default:
 throw new exception('unsupport image type');
 }
 return $tempresource;
 }
 /**
 * 改变图像大小
 * @param int $width 宽
 * @param int $height 高
 * @param string $flag 一般而言,允许截图则用4,不允许截图则用1; 假设要求一个为4:3比例的图像,则:4=如果太长则自动刪除一部分 0=长宽转换成参数指定的 1=按比例缩放,自动判断太长还是太宽,长宽约束在参数指定内 2=以宽为约束缩放 3=以高为约束缩放
 * @param string $bgcolor 如果不为null,则用这个参数指定的颜色作为背景色,并且图像扩充到指定高宽,该参数应该是一个数组;
 * @return string
 */
 public function resizeimage($width, $height, $flag=1, $bgcolor=null) {
 $widthratio = $width/$this->imagewidth;
 $heightratio = $height/$this->imageheight;
 switch ($flag) {
 case 1:
 if ($this->imageheight < $height && $this->imagewidth < $width) {
 $endwidth = $this->imagewidth;
 $endheight = $this->imageheight;
 //return;
 } elseif (($this->imageheight * $widthratio)>$height) {
 $endwidth = ceil($this->imagewidth * $heightratio);
 $endheight = $height;
 } else {
 $endwidth = $width;
 $endheight = ceil($this->imageheight * $widthratio);
 }
 break;
 case 2:
 $endwidth = $width;
 $endheight = ceil($this->imageheight * $widthratio);
 break;
 case 3:
 $endwidth = ceil($this->imagewidth * $heightratio);
 $endheight = $height;
 break;
 case 4:
 $endwidth2 = $width;
 $endheight2 = $height;
 if ($this->imageheight < $height && $this->imagewidth < $width) {
 $endwidth = $this->imagewidth;
 $endheight = $this->imageheight;
 //return;
 } elseif (($this->imageheight * $widthratio)<$height) {
 $endwidth = ceil($this->imagewidth * $heightratio);
 $endheight = $height;
 } else {
 $endwidth = $width;
 $endheight = ceil($this->imageheight * $widthratio);
 }
 break;
 default:
 $endwidth = $width;
 $endheight = $height;
 break;
 }
 if ($this->imageresource==null) {
 $this->createsrcimage();
 }
 if($bgcolor){
 $this->newresource = imagecreatetruecolor($width,$height);
 $bg=imagecolorallocate($this->newresource,$bgcolor[0],$bgcolor[1],$bgcolor[2]);
 imagefilledrectangle($this->newresource,0,0,$width,$height,$bg);
 $tox=ceil(($width-$endwidth)/2);
 $toy=ceil(($height-$endheight)/2);
 if($tox<0) $tox=0;
 if($toy<0) $toy=0;
 }else if ($flag==4) {
 $this->newresource = imagecreatetruecolor($endwidth2,$endheight2);
 }else {
 $this->newresource = imagecreatetruecolor($endwidth,$endheight);
 }
 $this->newrestype = $this->imagetype;
 imagecopyresampled($this->newresource, $this->imageresource, $tox, $toy, 0, 0, $endwidth, $endheight,$this->imagewidth,$this->imageheight);
 }
 /**
 * 给图像加水印
 * @param string $watercontent 水印内容可以是图像文件名,也可以是文字
 * @param int $pos 位置0-9可以是数组
 * @param int $textfont 字体大字,当水印内容是文字时有效
 * @param string $textcolor 文字颜色,当水印内容是文字时有效
 * @return string
 */
 public function watermark($watercontent, $pos = 0, $textfont=5, $textcolor="#ffffff") {
 $iswaterimage = file_exists($watercontent);
 if ($iswaterimage) {
 $waterimgres = $this->createimagefromfile($watercontent);
 $waterimginfo = $this->getimageinfo($watercontent);
 $waterwidth = $waterimginfo[0];
 $waterheight = $waterimginfo[1];
 } else {
 $watertext = $watercontent;
 //$temp = @imagettfbbox(ceil($textfont*2.5),0,"./cour.ttf",$watercontent);
 if ($temp) {
 $waterwidth = $temp[2]-$temp[6];
 $waterheight = $temp[3]-$temp[7];
 } else {
 $waterwidth = 100;
 $waterheight = 12;
 }
 }
 if ($this->imageresource==null) {
 $this->createsrcimage();
 }
 switch($pos) 
 { 
 case 0://随机 
 $posx = rand(0,($this->imagewidth - $waterwidth)); 
 $posy = rand(0,($this->imageheight - $waterheight)); 
 break; 
 case 1://1为顶端居左 
 $posx = 0; 
 $posy = 0; 
 break; 
 case 2://2为顶端居中 
 $posx = ($this->imagewidth - $waterwidth) / 2; 
 $posy = 0; 
 break; 
 case 3://3为顶端居右 
 $posx = $this->imagewidth - $waterwidth; 
 $posy = 0; 
 break; 
 case 4://4为中部居左 
 $posx = 0; 
 $posy = ($this->imageheight - $waterheight) / 2; 
 break; 
 case 5://5为中部居中 
 $posx = ($this->imagewidth - $waterwidth) / 2; 
 $posy = ($this->imageheight - $waterheight) / 2; 
 break; 
 case 6://6为中部居右 
 $posx = $this->imagewidth - $waterwidth; 
 $posy = ($this->imageheight - $waterheight) / 2; 
 break; 
 case 7://7为底端居左 
 $posx = 0; 
 $posy = $this->imageheight - $waterheight; 
 break; 
 case 8://8为底端居中 
 $posx = ($this->imagewidth - $waterwidth) / 2; 
 $posy = $this->imageheight - $waterheight; 
 break; 
 case 9://9为底端居右 
 $posx = $this->imagewidth - $waterwidth-20; 
 $posy = $this->imageheight - $waterheight-10; 
 break; 
 default://随机 
 $posx = rand(0,($this->imagewidth - $waterwidth)); 
 $posy = rand(0,($this->imageheight - $waterheight)); 
 break;   
 }
 imagealphablending($this->imageresource, true); 
 if($iswaterimage) {
 imagecopy($this->imageresource, $waterimgres, $posx, $posy, 0, 0, $waterwidth,$waterheight);  
 } else { 
 $r = hexdec(substr($textcolor,1,2)); 
 $g = hexdec(substr($textcolor,3,2)); 
 $b = hexdec(substr($textcolor,5)); 
 $textcolor = imagecolorallocate($this->imageresource, $r, $g, $b);
 imagestring ($this->imageresource, $textfont, $posx, $posy, $watertext, $textcolor);     
 }
 $this->newresource = $this->imageresource;
 $this->newrestype = $this->imagetype;
 }
 /**
 * 生成验证码图片
 * @param int $width 宽
 * @param string $height 高
 * @param int $length 长度
 * @param int $validtype 0=数字,1=字母,2=数字加字母
 * @param string $textcolor 文字颜色
 * @param string $backgroundcolor 背景颜色
 * @return void
 */
 public function imagevalidate($width, $height, $length = 4, $validtype = 1, $textcolor = '#000000', $backgroundcolor = '#ffffff') {
 if ($validtype==1) {
 $validstring = 'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz';
 $validlength = 52;
 } elseif ($validtype==2) {
 $validstring = '0123456789abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz';
 $validlength = 62;
 } else {
 $validstring = '123456789';
 $validlength = 9;
 }
 srand((int)time());
 $valid = '';
 for ($i=0; $i<$length; $i++) {
 $valid .= $validstring{rand(0, $validlength-1)};
 }
 $this->newresource = imagecreate($width,$height);
 $bgr = hexdec(substr($backgroundcolor,1,2));
 $bgg = hexdec(substr($backgroundcolor,3,2));
 $bgb = hexdec(substr($backgroundcolor,5,2));
 $backgroundcolor = imagecolorallocate($this->newresource, $bgr, $bgg, $bgb);
 $tr = hexdec(substr($textcolor,1,2));
 $tg = hexdec(substr($textcolor,3,2));
 $tb = hexdec(substr($textcolor,5,2));
 $textcolor = imagecolorallocate($this->newresource, $tr, $tg, $tb);
 for ($i=0;$i<strlen($valid);$i++){ 
 imagestring($this->newresource,5,$i*$width/$length+3,2, $valid[$i],$textcolor); 
 }
 $this->newrestype = imagetype_jpeg;
 return $valid;
 }
 /**
 * 显示输出图像
 * @return void
 */
 public function display($filename='', $quality=100) {
 $imgtype = $this->newrestype;
 $imagesrc = $this->newresource;
    switch ($imgtype) {
 case imagetype_gif:
 if ($filename=='') {
 header('content-type: image/gif');
 }
 imagegif($imagesrc, $filename, $quality);
 break;
 case imagetype_jpeg:
 if ($filename=='') {
 header('content-type: image/jpeg');
 }
 imagejpeg($imagesrc, $filename, $quality);
 break;
 case imagetype_png:
 if ($filename=='') {
 header('content-type: image/png');
 imagepng($imagesrc);
 } else {
 imagepng($imagesrc, $filename);
 }
 break;
 case imagetype_wbmp:
 if ($filename=='') {
 header('content-type: image/wbmp');
 }
 imagewbmp($imagesrc, $filename, $quality);
 break;
 case imagetype_xbm:
 if ($filename=='') {
 header('content-type: image/xbm');
 }
 imagexbm($imagesrc, $filename, $quality);
 break;
 default:
 throw new exception('unsupport image type');
 }
 imagedestroy($imagesrc);
 }
 /**
 * 保存图像
 * @param int $filenametype 文件名类型 0使用原文件名,1使用指定的文件名,2在原文件名加上后缀,3产生随机文件名
 * @param string $folder 文件夹路径 为空为与原文件相同
 * @param string $param 参数$filenametype为1时为文件名2时为后缀
 * @return void
 */
 public function save($filenametype = 0, $folder = null, $param = '_miniature') {
 if ($folder==null) {
 $folder = dirname($this->filename).directory_separator;
 }
 $fileextname = filesystem::fileext($this->filename, true);
 $filebesicname = filesystem::getbasicname($this->filename, false);
 switch ($filenametype) {
 case 1:
 $newfilename = $folder.$param;
 break;
 case 2:
 $newfilename = $folder.$filebesicname.$param.$fileextname;
 break;
 case 3:
 $tmp = date('ymdhis');
 $filebesicname = $tmp;
 $i = 0;
 while (file_exists($folder.$filebesicname.$fileextname)) {
 $filebesicname = $tmp.$i;
 $i++;
 }
 $newfilename = $folder.$filebesicname.$fileextname;
 break;
 default:
 $newfilename = $this->filename;
 break;
 }
 $this->display($newfilename);
 return $newfilename;
 }
}
?>

希望本文所述对大家的php程序设计有所帮助。