This article PHP picture operation package class inside four kinds of methods, text watermark (Imagettftext ()), Picture Watermark (Imagecopymerge ()), picture compression, picture clipping (imagecopyresampled ()), the rest of the usual GD function will not be described.
Directly on the code:
Class Image
{
Private $info;
Private $image;
Public $type;
Public function __construct ($SRC)
{
$this->info=getimagesize ($SRC);
$this->type=image_type_to_extension ($this->info[' 2 '],false);
$fun = "imagecreatefrom{$this->type}";
$this->image= $fun ($SRC);
}
/**
* Text watermark
* @param [type] $font font
* @param [Type] $content content
* @param [Type] $size text size
* @param [Type] $col text color (four-tuple array)
* @param array $location location
* @param integer $angle tilt angle
* @return [Type]
*/
Public Function Fontmark ($font, $content, $size, $col, $location, $angle =0) {
$col =imagecolorallocatealpha ($this->image, $col [' 0 '], $col [' 1 '], $col [' 2 '], $col [' 3 ']);
Imagettftext ($this->image, $size, $angle, $location [' 0 '], $location [' 1 '], $col, $font, $content);
}
/**
* Image Watermark
* @param [Type] $imageMark watermark image Address
* @param [Type] $dst the position of the watermark picture in the original picture
* @param [Type] $pct transparency
* @return [Type]
*/
Public Function ImageMark ($imageMark, $DST, $pct) {
$info 2=getimagesize ($imageMark);
$type =image_type_to_extension ($info 2[' 2 '],false);
$func 2= "Imagecreatefrom". $type;
$water = $func 2 ($imageMark);
Imagecopymerge ($this->image, $water, $DST [0], $DST [1], 0, 0, $info 2[' 0 '], $info 2[' 1 '], $pct);
Imagedestroy ($water);
}
/**
* Compress Pictures
* @param [Type] $thumbSize compress picture size
* @return [Type] [description]
*/
Public function Thumb ($thumbSize) {
$imageThumb =imagecreatetruecolor ($thumbSize [0], $thumbSize [1]);
Imagecopyresampled ($imageThumb, $this->image, 0, 0, 0, 0, $thumbSize [0], $thumbSize [1], $this->info[' 0 '], $this-& gt;info[' 1 ']);
Imagedestroy ($this->image);
$this->image= $imageThumb;
}
/**
* Crop Picture
* @param [Type] $cutSize crop size
* @param [type] $location crop position
* @return [Type] [description]
*/
Public function Cut ($cutSize, $location) {
$imageCut =imagecreatetruecolor ($cutSize [0], $cutSize [1]);
Imagecopyresampled ($imageCut, $this->image, 0, 0, $location [0], $location [1], $cutSize [0], $cutSize [1], $cutSize [0] , $cutSize [1]);
Imagedestroy ($this->image);
$this->image= $imageCut;
}
/**
* Show Pictures
* @return [Type] [description]
*/
Public function Show () {
Header ("Content-type:". $this->info[' mime ');
$funn = "image". $this->type;
$funn ($this->image);
}
/**
* Save Picture
* @param [Type] $newname new picture name
* @return [Type] [description]
*/
Public function Save ($newname) {
Header ("Content-type:". $this->info[' mime ');
$funn = "image". $this->type;
$funn ($this->image, $newname. $this->type);
}
Public Function __destruct () {
Imagedestroy ($this->image);
}
}
?>