Php watermarks images 4-PHP source code

Source: Internet
Author: User
Ec (2); & lt ;? & Nbsp; ** & nbsp; * generate a watermark. Two methods are called to generate a watermark text and a watermark image. & nbsp; & nbsp; * & nbsp; function_createMask () & nbsp; & nbsp ;{& nbsp; & nbsp; & nbsp; & script ec (2); script

/**
* Generate a watermark. Two methods are called to generate a watermark text and a watermark image.
*/
Function _ createMask ()
{
If ($ this-> mask_word)
{
// Obtain the font information
$ This-> _ setFontInfo ();

If ($ this-> _ isFull ())
{
Die ("the watermark text is too large ");
}
Else
{
$ This-> h_dst = imagecreatetruecolor ($ this-> dst_w, $ this-> dst_h );
$ White = ImageColorAllocate ($ this-> h_dst, 255,255,255 );
Imagefilledrectangle ($ this-> h_dst, $ this-> dst_w, $ this-> dst_h, $ white); // fill in the background color
$ This-> _ drawBorder ();
Imagecopyresampled ($ this-> h_dst, $ this-> h_src,
$ This-> start_x, $ this-> start_y,
$ This-> src_x, $ this-> src_y,
$ This-> fill_w, $ this-> fill_h,
$ This-> copy_w, $ this-> copy_h );
$ This-> _ createMaskWord ($ this-> h_dst );
}
}

If ($ this-> mask_img)
{
$ This-> _ loadMaskImg (); // get the width and height when loading

If ($ this-> _ isFull ())
{
// Generate the watermark on the source image and copy it again
$ This-> _ createMaskImg ($ this-> h_src );
$ This-> h_dst = imagecreatetruecolor ($ this-> dst_w, $ this-> dst_h );
$ White = ImageColorAllocate ($ this-> h_dst, 255,255,255 );
Imagefilledrectangle ($ this-> h_dst, $ this-> dst_w, $ this-> dst_h, $ white); // fill in the background color
$ This-> _ drawBorder ();
Imagecopyresampled ($ this-> h_dst, $ this-> h_src,
$ This-> start_x, $ this-> start_y,
$ This-> src_x, $ this-> src_y,
$ This-> fill_w, $ this-> fill_h,
$ This-> copy_w, $ this-> copy_h );
}
Else
{
// Create a new image and copy it
$ This-> h_dst = imagecreatetruecolor ($ this-> dst_w, $ this-> dst_h );
$ White = ImageColorAllocate ($ this-> h_dst, 255,255,255 );
Imagefilledrectangle ($ this-> h_dst, $ this-> dst_w, $ this-> dst_h, $ white); // fill in the background color
$ This-> _ drawBorder ();
Imagecopyresampled ($ this-> h_dst, $ this-> h_src,
$ This-> start_x, $ this-> start_y,
$ This-> src_x, $ this-> src_y,
$ This-> fill_w, $ this-> fill_h,
$ This-> copy_w, $ this-> copy_h );
$ This-> _ createMaskImg ($ this-> h_dst );
}
}

If (empty ($ this-> mask_word) & empty ($ this-> mask_img ))
{
$ This-> h_dst = imagecreatetruecolor ($ this-> dst_w, $ this-> dst_h );
$ White = ImageColorAllocate ($ this-> h_dst, 255,255,255 );
Imagefilledrectangle ($ this-> h_dst, $ this-> dst_w, $ this-> dst_h, $ white); // fill in the background color
$ This-> _ drawBorder ();

Imagecopyresampled ($ this-> h_dst, $ this-> h_src,
$ This-> start_x, $ this-> start_y,
$ This-> src_x, $ this-> src_y,
$ This-> fill_w, $ this-> fill_h,
$ This-> copy_w, $ this-> copy_h );
}
}

/**
* Draw borders
*/
Function _ drawBorder ()
{
If (! Empty ($ this-> img_border_size ))
{
$ C = $ this-> _ parseColor ($ this-> img_border_color );
$ Color = ImageColorAllocate ($ this-> h_src, $ c [0], $ c [1], $ c [2]);
Imagefilledrectangle ($ this-> h_dst, $ this-> dst_w, $ this-> dst_h, $ color); // fill the background color
}
}

/**
* Generate watermark text
*/
Function _ createMaskWord ($ src)
{
$ This-> _ countMaskPos ();
$ This-> _ checkMaskValid ();

$ C = $ this-> _ parseColor ($ this-> mask_font_color );
$ Color = imagecolorallocatealpha ($ src, $ c [0], $ c [1], $ c [2], $ this-> mask_txt_pct );

If (is_numeric ($ this-> font ))
{
Imagestring ($ src,
$ This-> font,
$ This-> mask_pos_x, $ this-> mask_pos_y,
$ This-> mask_word,
$ Color );
}
Else
{
Imagettftext ($ src,
$ This-> font_size, 0,
$ This-> mask_pos_x, $ this-> mask_pos_y,
$ Color,
$ This-> font,
$ This-> mask_word );
}
}

/**
* Generate a watermark image
*/
Function _ createMaskImg ($ src)
{
$ This-> _ countMaskPos ();
$ This-> _ checkMaskValid ();
Imagecopymerge ($ src,
$ This-> h_mask,
$ This-> mask_pos_x, $ this-> mask_pos_y,
0, 0,
$ This-> mask_w, $ this-> mask_h,
$ This-> mask_img_pct );

Imagedestroy ($ this-> h_mask );
}

/**
* Attach a watermark image
*/
Function _ loadMaskImg ()
{
$ Mask_type = $ this-> _ getImgType ($ this-> mask_img );
$ This-> _ checkValid ($ mask_type );

// File_get_contents function requires php version> 4.3.0
$ Src = '';
If (function_exists ("file_get_contents "))
{
$ Src = file_get_contents ($ this-> mask_img );
}
Else
{
$ Handle = fopen ($ this-> mask_img, "r ");
While (! Feof ($ handle ))
{
$ Src. = fgets ($ fd, 4096 );
}
Fclose ($ handle );
}
If (empty ($ this-> mask_img ))
{
Die ("the watermark image is empty ");
}
$ This-> h_mask = ImageCreateFromString ($ src );
$ This-> mask_w = $ this-> getImgWidth ($ this-> h_mask );
$ This-> mask_h = $ this-> getImgHeight ($ this-> h_mask );
}

/**
* Image output
*/
Function _ output ()
{
$ Img_type = $ this-> img_type;
$ Func_name = $ this-> all_type [$ img_type] ['output'];
If (function_exists ($ func_name ))
{
// Judge the browser. If IE is used, no headers are sent.
If (isset ($ _ SERVER ['HTTP _ USER_AGENT '])
{
$ Ua = strtoupper ($ _ SERVER ['HTTP _ USER_AGENT ']);
If (! Preg_match ('/^. * MSIE. * \) $/I', $ ua ))
{
Header ("Content-type: $ img_type ");
}
}
$ Func_name ($ this-> h_dst, $ this-> dst_img, $ this-> img_display_quality );
}
Else
{
Return false;
}
}

/**
* Color Analysis
*
* @ Param string $ color hexadecimal color
*/
Function _ parseColor ($ color)
{
$ Arr = array ();
For ($ ii = 1; $ ii {
$ Arr [] = hexdec (substr ($ color, $ ii, 2 ));
$ Ii ++;
}

Return $ arr;
}

/**
* Calculate the coordinates
*/
?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.