<?php
/*
//示例
$image = new Gimage();
$image->limit = 600;//长宽限制
$image->wm_text=”www.
linuxlaptop.cn”;//水印文字
$image->wm_fontfile=”font/xsuni.ttf”;//字体文件
$image->wm_color=”#ff0000″;
$image->save_file = “ltcn.jpg”;//保存到xx文件
$image->create(“linuxlaptop.jpg”);//从xx文件创建
*/
/*
+------------------------------
| 生成缩略图&加水印的图片类
+------------------------------
*/
Class Gimage{
var $input_type = ""; //输入图片的格式
var $output_type = "jpg"; //输出图片的格式
var $limit = 0; //图片大小限制
var $filename = ""; //输入图片的文件名(也可以直接是图片数据)
var $jpeg_quality = 90; //jpeg图片质量
var $save_file = ''; //输出文件名
var $wm_text = ""; //水印文字( 不支持中文:'( )
var $wm_size = 12; //水印文字大小
var $wm_angle = 0; //水印文字角度
var $wm_x = 30; //水印x坐标
var $wm_y = 30; //水印y坐标
var $wm_color = "#cccccc"; //水印颜色
var $wm_fontfile = "geodesic.ttf";//水印字体文件
function create($filename="")
{
if ($filename) $this->filename = $filename;
if (!$this->input_type) $this->get_type();
if (!$this->output_type) $this->output_type = $this->input_type;
if ($this->input_type == "jpg") $this->input_type = "jpeg";
if ($this->output_type == "jpg") $this->output_type = "jpeg";
switch ($this->input_type){
case 'gif':
$src_img=ImageCreateFromGIF($this->filename);
break;
case 'jpeg':
$src_img=ImageCreateFromJPEG($this->filename);
break;
case 'png':
$src_img=ImageCreateFromPNG($this->filename);
break;
default:
$src_img=ImageCreateFromString($this->filename);
break;
}
$src_w=ImageSX($src_img);
$src_h=ImageSY($src_img);
if ($src_w>=$src_h){
if ($src_w>$this->limit){
$new_w=$this->limit;
$new_h=($this->limit / $src_w)*$src_h;
}
}
else{
if ($src_h>$this->limit){
$new_h=$this->limit;
$new_w=($this->limit / $src_h)*$src_w;
}
}
if ($new_h){
$dst_img=imagecreatetruecolor($new_w,$new_h);
imagecopyresampled($dst_img,$src_img,0,0,0,0,$new_w,$new_h,ImageSX($src_img),ImageSY($src_img));
}
else{
$dst_img = $src_img;
}
if ($this->wm_text)
{
if(preg_match("/([a-f0-9][a-f0-9])([a-f0-9][a-f0-9])([a-f0-9][a-f0-9])/i", $this->wm_color, $color))
{
$red = hexdec($color[1]);
$green = hexdec($color[2]);
$blue = hexdec($color[3]);
}
$wm_color = imagecolorallocatealpha($dst_img, $red, $green, $blue, 90);
imagettftext($dst_img, $this->wm_size, $this->wm_angle, $this->wm_x, $this->wm_y, $wm_color, $this->wm_fontfile, $this->wm_text);
}
if ($this->save_file)
{
switch ($this->output_type){
case 'gif':
$src_img=ImagePNG($dst_img, $this->save_file);
break;
case 'jpeg':
$src_img=ImageJPEG($dst_img, $this->save_file, $this->jpeg_quality);
break;
case 'png':
$src_img=ImagePNG($dst_img, $this->save_file);
break;
default:
$src_img=ImageJPEG($dst_img, $this->save_file, $this->jpeg_quality);
break;
}
}
else
{
header("Content-type: image/{$this->output_type}");
switch ($this->output_type){
case 'gif':
$src_img=ImagePNG($dst_img);
break;
case 'jpeg':
$src_img=ImageJPEG($dst_img, "", $this->jpeg_quality);
break;
case 'png':
$src_img=ImagePNG($dst_img);
break;
default:
$src_img=ImageJPEG($dst_img, "", $this->jpeg_quality);
break;
}
}
imagedestroy($dst_img);
}
function get_type()//获取图像文件类型
{
$name_array = explode(".",$this->filename);
if (preg_match("/.(jpg|jpeg|gif|png)$/", $this->filename, $matches))
{
$this->input_type = strtolower($matches[1]);
}
else
{
$this->input_type = "string";
}
}
}