To add a watermark to the picture, in fact, is the original image and Watermark added together, the following small series of recently collated information to share.
PHP to the image file operation is mainly to use the GD library extension. When we frequently use PHP to manipulate pictures, we naturally encapsulate many functions, otherwise we will write too many duplicate code. When there are a lot of related functions to the picture, we can consider the functions also sorted, so there is the idea of encapsulation into classes.
The operation of the picture mainly after four steps:
First step: Open the picture
Step Two: Operation picture
Step three: Output picture
Fourth step: Destroy the picture
1,3,4 three steps to write every time, each time is similar. The only thing that really needs to be modified is to manipulate the picture. Operation of the picture and often through 1 or more major GD functions to complete.
This article encapsulates four kinds of methods, text watermark (Imagettftext ()), Image Watermark (Imagecopymerge ()), picture compression, picture shearing (imagecopyresampled ()), the rest of the common GD function will not repeat.
Directly on the code:
<?php 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-array) * @param array $location position * @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 picture 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 picture * @param [type] $thumbSize compress picture size * @return [type] [description]/Public function thumb ($thumbS
ize) {$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 cropping size * @param [type] $location cropping 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; /** * Display picture * @return [Type] [description]/Public function show () {header ("Content-type:". $this->info[' mi
Me ']);
$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);
The Public Function __destruct () {Imagedestroy ($this->image); }}?>
If you need anything else, just add it to the class.
To add a watermark code to a picture:
First look at the file check_image_addwatermark.php code
<?php//Modify picture effect $db = mysql_connect (' localhost ', ' root ', ' Ctrip07185419 ') or die (' can not connect to database ');
mysql_select_db (' MovieSite ', $db) or Die (Mysql_error ($db));
The path to upload file $dir = ' D:\Serious\phpdev\test\images '; Set environment variable putenv (' gdfontpath= '. ')
C:\Windows\Fonts ');
$font = "Arial"; The upload_image.php page passes over the parameter, if upload picture if ($_post[' submit '] = = ' upload ') {if ($_files[' uploadfile '] [' ERROR ']!= Upload_ ERR_OK) {switch ($_files[' uploadfile '] [' ERROR ']) {case Upload_err_ini_size:die (' the uploaded file exceeds th
e upload_max_filesize directive ');
Break Case Upload_err_form_size:die (' The UPLOAD file exceeds the max_file_size directive this is specified in the HTML for
M ');
Break
Case Upload_err_partial:die (' The uploaded file is only partially uploaded ');
Break
Case Upload_err_no_file:die (' NO FILE is uploaded ');
Break
Case Upload_err_no_tmp_dir:die (' The server is missing a temporary folder ');
Break Case Upload_err_cant_write:die (' The server fail to WRITE the uploaded file to the disk ');
Break
Case Upload_err_extension:die (' The UPLOAD stopped by EXTENSION ');
Break
}} $image _caption = $_post[' caption '];
$image _username = $_post[' username '];
$image _date = Date (' y-m-d ');
List ($width, $height, $type, $attr) = getimagesize ($_files[' uploadfile '] [' tmp_name ']);
$error = ' The file you upload are not a supported filetype ';
Switch ($type) {case imagetype_gif: $image = imagecreatefromgif ($_files[' uploadfile '] [' tmp_name ']) or Die ($error);
Break
Case Imagetype_jpeg: $image = imagecreatefromjpeg ($_files[' uploadfile '] [' tmp_name ']) or Die ($error);
Break
Case Imagetype_png: $image = imagecreatefrompng ($_files[' uploadfile '] [' tmp_name ']) or Die ($error);
Break
Default:break; $query = ' INSERT into images (image_caption,image_username,image_date) VALUES ('. $image _caption. ' "," '. $image _
Username. ' "," '. $image _date. ") '; $result = mysql_query ($query, $db) or diE (Mysql_error ($db));
$last _id = mysql_insert_id (); $imagename = $last _id.
JPG ';
Imagejpeg ($image, $dir. '/' $imagename);
Imagedestroy ($image);
$image _id = $last _id; Imagejpeg ($image, $dir. $image _id.
JPG ');
Imagedestroy ($image); else//If the picture has already been uploaded, take the picture name from the database {$query = ' Select image_id,image_caption,image_username,image_date from images where image
_id= '. $_post[' id '];
$result = mysql_query ($query, $db) or Die (Mysql_error ($db));
Extract (Mysql_fetch_assoc ($result)); List ($width, $height, $type, $attr) = getimagesize ($dir. '/'. $image _id.
JPG '); //If it is to save the picture if ($_post[' submit '] = = ' Save ') {if (Isset ($_post[' id ')) && ctype_digit ($_post[' id ']) && file _exists ($dir. '/'. $_post[' id ']. JPG ') {$image = Imagecreatefromjpeg ($dir. '/'. $_post[' id ').
JPG ');
else {die (' invalid image specified '); } $effect = (isset ($_post[' effect '))?
$_post[' effect ']:-1; Switch ($effect) {case Img_filter_negate:imagefilter ($image, img_filter_negate); In the imageAll colors reverse break; Case Img_filter_grayscale:imagefilter ($image, Img_filter_grayscale);
A break that converts an image to grayscale; Case Img_filter_emboss:imagefilter ($image, Img_filter_emboss);
Make the image emboss break; Case Img_filter_gaussian_blur:imagefilter ($image, Img_filter_gaussian_blur);
Gaussian algorithm is used to blur image break;
} if (Isset ($_post[' emb_caption ')) {imagettftext ($image, 0, 0, $font, $image _caption);
} if (Isset ($_post[' Emb_logo ')) {//Get the size of the watermark picture and create the watermark list ($wmk _width, $wmk _height) = getimagesize (' images/logo.png ');
$x = ($width-$WMK _width)/2;
$y = ($height-$WMK _height)/2;
$WMK = imagecreatefrompng (' images/logo.png ');
Combine the watermark picture with the original picture Imagecopymerge ($image, $WMK, $x, $y, 0, 0, $wmk _width, $wmk _height, 20);
Clear watermark Picture Imagedestroy ($WMK); Imagejpeg ($image, $dir. ' $_post[' id ').
JPG ', 100); ?>
This is mainly to add Watermark options, if selected add watermark will be logo.png as a watermark picture and the original picture merged together.
The corresponding logic is added to the preview file as follows:
<?php $dir = ' D:\Serious\phpdev\test\images '; Set environment variable putenv (' gdfontpath= '. ')
C:\Windows\Fonts ');
$font = "Arial"; if (Isset ($_get[' id ')) && ctype_digit ($_get[' id ']) && file_exists ($dir. '/'. $_get[' id ']. JPG ') {$image = Imagecreatefromjpeg ($dir. '/'. $_get[' id ').
JPG '); else {die (' invalid image specified ');} $effect = (isset ($_get[' e '))?
$_get[' E ']:-1;
Switch ($effect) {case Img_filter_negate:imagefilter ($image, img_filter_negate);
Break
Case Img_filter_grayscale:imagefilter ($image, Img_filter_grayscale);
Break
Case Img_filter_emboss:imagefilter ($image, Img_filter_emboss);
Break
Case Img_filter_gaussian_blur:imagefilter ($image, Img_filter_gaussian_blur);
Break
} if (Isset ($_get[' Capt ')) {//echo $_get[' Capt '];
Imagettftext ($image, 0, 0, $font, $_get[' Capt ')); } if (Isset ($_get[' logo ')) {list ($widht, $height) = getimagesize ($dir. '/'. $_get[' id '].
JPG '); List ($wmk _width, $wmk _height) = getimagesize (' Images/loGo.png ');
$x = ($WIDHT-$WMK _width)/2;
$y = ($height-$WMK _height)/2;
$WMK = imagecreatefrompng (' images/logo.png ');
Imagecopymerge ($image, $WMK, $x, $y, 0, 0, $wmk _width, $wmk _height, 20);
Imagedestroy ($WMK);
Header (' Content-type:image/jpeg '); Imagejpeg ($image, ', M ');?>
The last uploaded watermark image effect is as follows:
Note that the main logic is to use the Imagecopymerge () method to merge two images together to create a watermark effect. Take a look at the method prototypes and parameters for this method:
BOOL Imagecopymerge (Resource $DST _im, resource $src _im, int $dst _x, int $dst _y, int$src_x, int $src _y, int $src _w , int $src _h, int $pct)
The coordinates of the src_im image are started from the src_x,src_y, the width is src_w, and the height is part of the src_h copy to the Dst_im image in the location of Dst_x and dst_y. The two images will be based on the PCT to determine the degree of consolidation, the value range from 0 to 100. When pct = 0 o'clock, in fact nothing is done, when 100 for the palette image this function and imagecopy () exactly the same, it is true color image to achieve alpha transparency.
The above content is this article introduces the PHP to the picture to add the watermark compression cut the entire content of the package class, hoped everybody is interested in this article introduction.