<?php
/*打开图片*/
//设置图片路径
$src='1.jpg';
//获取图像信息
$info = getimagesize($img);
//通过图像的编号来获取图像的额类型;
$type = image_type_to_extension($info[2],false);
//在内存中创建一个和我们图像类型一样的图像
$fun = "imagecreatefrom{$type}";//$fun = imagecreatefromjpeg,png;
//把图像复制到我们的内存中
$image = $fun($src);//imagecreatefromjpeg($src);
/*操作图片*/
//设置字体路径
$font = 'msyh.ttf';
//填写水印内容
$content = 'hello world';
//设置字体颜色和透明度 参数:图片 三基色 透明度
$col = imagecolorallocatealpha($image,255,255,255,50);
//写入图片
imagettftext($image,fontsize,angle,x,y,$col,$font,$content);
//设置水印路径
$image_Mark='xxx.png';
//获取水印图片的基本信息
$info2 = getimagesize($image_Mark);
$type2 = image_type_to_extension($info2[2],false);
//复制到内存中
$fun2 = 'imagecreatefrom{$type2}';
$wwater = $fun2($image_Mark);
imagecopymerge($image,$water,20,30,0,0,$info2[0],$info[1],30);
//销毁水印图片
imagedestroy($water);
//压缩图片
//在内存中建立一个宽300,高200的真色彩图片;
$image_thumb = imagecreatetruecolor(300,200);
//讲原图复制到新建的真色彩的图片上,并按照一定的比例压缩
imagecopyresampled($image_thumb,$image,0,0,0,0,300,200,$info[0],$info[1]);
/*输出图片*/
//浏览器输出
header("Content-type:".$info['mime']);
$func = "image{$type}";
$func($image);
//保存图片
$func($image,'Newimage.'.$type);
/*销毁图片*/
imagedestroy($imge);
/*封装类*/
class Image {
private $info;
private $image
//打开图片
public function __construct($src) {
$info = getimagesize($src);
$this->info = array(
'width' => $info[0],
'height' => $info[1],
'type' => image_type_to_extension($info[2],false);
);
$fun = "imagecreatefrom{$this->info['type']}";
$this->image = $fun($src);
}
//压缩图片
public function thumb($width,$height) {
$image_thumb = imagecreatetruecolor($width,$height);
imagecopyresampled($image_thumb,$this->image,0,0,0,0,$width,$height,$this->info['width'],$this->info['height']);
imagedestroy($this->image);
$this->image = $image_thumb;
}
//添加文字水印
public function fontMark($content,$font_url,$size,$color,$local,$angle) {
$col = imagecolorallocatealpha($this->image,$color[0],$color[1],$color[2],$color[3]);
imagettftext($this->image,$size,$angle,$local['x'],$local['y'],$color,$font_url,$content);
}
//添加图片水印
public functionimageMark($source,$local,$alpha) {
$info = getimagesize($source);
$type = image_type_to_extension($info[2],false);
$fun = 'imagecreatefrom{$type2}';
$water = $fun($source);
imagecopymerge($this->image,$water,$local['x'],$local['y'],0,0,$info2[0],$info[1],$alpha);
imagedestroy($water);
}
//输出图片
public function show() {
header("Content-type:".$this->info['type']);
$func = "image{$this->info['type']}";
$func($this->image);
}
//保存图片
public function save($newname) {
$func = "image{$this->info['type']}";
$func($image,$newname.'.'.$type);
}
//销毁图片
public function __destruct() {
imagedestroy($this->image);
}
}
?>