Php image application-generate an image with a watermark text-generate an image with a watermark icon-generate a thumbnail (// For the annotation content, without affecting File Execution ),

Source: Internet
Author: User
Tags imagecopy imagejpeg switch loop

Php image application-generate an image with a watermark text-generate an image with a watermark icon-generate a thumbnail (// For the annotation content, without affecting File Execution ),
<? Php
//// // Start of the watermark text function //////////////////
Function watertext ($ I, $ t = 'copyright holder ', $ s = 20, $ c = 'white', $ p = 9 ){
$ Img = imagecreatefromjpeg ($ I); // imagecreatefromjpeg-creates a new image from a file or URL.

$ Cc = '';
/////// Switch cycle, cycle watermark text color start //////
Switch ($ c ){
Case 'white ':
$ Cc = imagecolorallocatealpha ($ img, 255,255,255, 50 );
Break;
Case 'red ':
$ Cc = imagecolorallocatealpha ($ img, 0 );
Break;
Case 'green ':
$ Cc = imagecolorallocatealpha ($ img, 0,255 );
Break;
Case 'blue ':
$ Cc = imagecolorallocatealpha ($ img );
Break;
Default:
$ Cc = imagecolorallocatealpha ($ img, mt_rand (0,255), mt_rand (0,255), mt_rand (0,255), mt_rand (0,100 ));
Break;
}
/////// Switch loop, the color of the circular watermark text ends //////

// Imagecolorallocatealpha-assign color + alpha to an image
$ C = imagecolorallocatealpha ($ img, 255,255,255, 80 );

// Define the font of the watermark text
$ Font = dirname (_ FILE _). '/h. ttf'; // dirname-directory part of the returned path

// Imagettfbbox-get the text range using the TrueType font
$ R = imagettfbbox ($ s, 0, $ font, iconv ('gbk', 'utf-8', $ t ));
$ Ttfw = $ r [2]-$ r [0] + 10;
$ Ttfh = $ r [1] + 10;


$ X = imagesx ($ img)-$ ttfw; // imagesx-Get Image Width
$ Y = imagesy ($ img)-$ ttfh; // imagesy-Get Image Height

// Imagettftext-use the TrueType font to write text to the image
Imagettftext ($ img, $ s, 0, $ x, $ y, $ cc, $ font, iconv ('gbk', 'utf-8', $ t ));
Imagejpeg ($ img, $ I); // imagejpeg-outputs an image to a browser or file.
Imagedestroy ($ img); // imagedestroy-destroy an image
}
//// // Define the end Of the watermark text function //////////////////


//// // Define the watermark icon function start //////////////////
Function watericon ($ I ){
$ Img = imagecreatefromjpeg ($ I); // imagecreatefromjpeg-creates a new image from a file or URL.

// Find the watermark icon file created in a folder
$ L = dirname (_ FILE _). '/logo.png'; // dirname-directory part of the returned path
$ Logo = imagecreatefrompng ($ l); // imagecreatefrompng-create a new image from a file or URL.


$ Xx = imagesx ($ logo); // imagesx-get the watermark image width
$ Yy = imagesy ($ logo); // imagesy-watermark image height


$ X = imagesx ($ img)-$ xx-15; // define the x coordinate of the watermark icon in the image
$ Y = imagesy ($ img)-$ yy-15; // defines the y coordinate of the watermark icon in the image


Imagecopy ($ img, $ logo, $ x, $ y, $ xx, $ yy); // imagecopy-copy part of the image

Imagejpeg ($ img, $ I); // imagejpeg-outputs an image to a browser or file.
Imagedestroy ($ img); // imagedestroy-destroy an image
Imagedestroy ($ logo); // imagedestroy-destroy an image
}
// A.jpg s_a.jpg
/// // Define the watermark icon function end //////////////////




/////// Define the thumbnail function to start //////////
Function thumd ($ I, $ w = 170, $ h = 0) {// The default width is 170, with a high ratio.
$ Ww = $ w;
$ Hh = $ h;

$ Img = imagecreatefromjpeg ($ I); // imagecreatefromjpeg-creates a new image from a file or URL.
$ Iw = imagesx ($ img); // imagesx-obtain the watermark image width
$ Ih = imagesy ($ img); // imagesy-watermark image height

// If the height is 0, the system automatically calculates the value based on the ratio of the height.
If ($ h = 0 ){
$ Hh = $ w/$ iw * $ ih;
}

$ New = imagecreatetruecolor ($ ww, $ hh); // imagecreatetruecolor-create a true color image

// Imagecopyresampled-copy part of the image and resize it
Imagecopyresampled ($ new, $ img, 0, 0, 0, $ ww, $ hh, $ iw, $ ih );

// Pathinfo-returned file path information
$ Path = pathinfo ($ I, PATHINFO_DIRNAME). '/s _'. pathinfo ($ I, PATHINFO_BASENAME );
Imagejpeg ($ new, $ path); // imagejpeg-outputs an image to a browser or file.
Imagedestroy ($ new); // imagedestroy-destroy an image
Imagedestroy ($ img); // imagedestroy-destroy an image
}
/////// Define the thumbnail function end //////////


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.