Example: php code used to add a watermark to an image.
- /*
- // Example
- $ Image = new Gimage ();
- $ Image-> limit = 600; // maximum length and width
- $ Image-> wm_text = "www.linuxlaptop.cn"; // watermark text
- $ Image-> wm_fontfile = "font/xsuni. ttf"; // font file
- $ Image-> wm_color = "# ff0000 ″;
- $ Image-> save_file = export ltcn.jpg "; // save to xx file
- $ Image-> create(“linuxlaptop.jpg "); // create a file from xx
- */
- /*
- + ------------------------------
- | Create a thumbnail and watermark image
- + ------------------------------
- */
- Class Gimage {
-
- Var $ input_type = ""; // format of the input image
- Var $ output_type = "jpg"; // output Image format
- Var $ limit = 0; // image size limit
- Var $ filename = ""; // enter the image file name (or image data)
- Var $ pai_quality = 90; // jpeg image quality
- Var $ save_file = ''; // output file name
- Var $ wm_text = ""; // watermark text (Chinese: '() not supported :'()
- Var $ wm_size = 12; // watermark text size
- Var $ wm_angle = 0; // watermark text angle
- Var $ wm_x = 30; // watermark x coordinate
- Var $ wm_y = 30; // y coordinate of the watermark
- Var $ wm_color = "# cccccc"; // watermark color
- Var $ wm_fontfile = "geodesic. ttf"; // watermark font file
-
- Function create ($ filename = "")
- {
- If ($ filename) $ this-> filename = $ filename;
-
- If (! $ This-> input_type) $ this-> get_type ();
- If (! $ This-> output_type) $ this-> output_type = $ this-> input_type;
-
- If ($ this-> input_type = "jpg") $ this-> input_type = "jpeg ";
- If ($ this-> output_type = "jpg") $ this-> output_type = "jpeg ";
-
- Switch ($ this-> input_type ){
- Case 'GIF ':
- $ Src_img = ImageCreateFromGIF ($ this-> filename );
- Break;
-
- Case 'jpeg ':
- $ Src_img = ImageCreateFromJPEG ($ this-> filename );
- Break;
-
- Case 'PNG ':
- $ Src_img = ImageCreateFromPNG ($ this-> filename );
- Break;
-
- Default:
- $ Src_img = ImageCreateFromString ($ this-> filename );
- Break;
- }
- $ Src_w = ImageSX ($ src_img );
- $ Src_h = ImageSY ($ src_img );
- If ($ src_w >=$ src_h ){
- If ($ src_w> $ this-> limit ){
- $ New_w = $ this-> limit;
- $ New_h = ($ this-> limit/$ src_w) * $ src_h;
- }
- }
- Else {
- If ($ src_h> $ this-> limit ){
- $ New_h = $ this-> limit;
- $ New_w = ($ this-> limit/$ src_h) * $ src_w;
- }
- }
-
- If ($ new_h ){
- $ Dst_img = imagecreatetruecolor ($ new_w, $ new_h );
- Imagecopyresampled ($ dst_img, $ src_img, 0, 0, 0, $ new_w, $ new_h, ImageSX ($ src_img), ImageSY ($ src_img ));
- }
- Else {
- $ Dst_img = $ src_img;
- }
-
- If ($ this-> wm_text)
- {
- If (preg_match ("/([a-f0-9] [a-f0-9]) ([a-f0-9] [a-f0-9]) ([a-f0-9])/I", $ this-> wm_color, $ color ))
- {
- $ Red = hexdec ($ color [1]);
- $ Green = hexdec ($ color [2]);
- $ Blue = hexdec ($ color [3]);
- }
- $ Wm_color = imagecolorallocatealpha ($ dst_img, $ red, $ green, $ blue, 90 );
- Imagettftext ($ dst_img, $ this-> wm_size, $ this-> wm_angle, $ this-> wm_x, $ this-> wm_y, $ wm_color, $ this-> wm_fontfile, $ this-> wm_text );
- }
-
- If ($ this-> save_file)
- {
- Switch ($ this-> output_type ){
- Case 'GIF ':
- $ Src_img = ImagePNG ($ dst_img, $ this-> save_file );
- Break;
-
- Case 'jpeg ':
- $ Src_img = ImageJPEG ($ dst_img, $ this-> save_file, $ this-> 1__quality );
- Break;
-
- Case 'PNG ':
- $ Src_img = ImagePNG ($ dst_img, $ this-> save_file );
- Break;
-
- Default:
- $ Src_img = ImageJPEG ($ dst_img, $ this-> save_file, $ this-> 1__quality );
- Break;
- }
- }
- Else
- {
- Header ("Content-type: image/{$ this-> output_type }");
- Switch ($ this-> output_type ){
- Case 'GIF ':
- $ Src_img = ImagePNG ($ dst_img );
- Break;
-
- Case 'jpeg ':
- $ Src_img = ImageJPEG ($ dst_img, "", $ this-> pai_quality );
- Break;
-
- Case 'PNG ':
- $ Src_img = ImagePNG ($ dst_img );
- Break;
-
- Default:
- $ Src_img = ImageJPEG ($ dst_img, "", $ this-> pai_quality );
- Break;
- }
- }
- Imagedestroy ($ dst_img );
-
- }
- Function get_type () // Obtain the image file type
- {
- $ Name_array = explode (".", $ this-> filename );
- If (preg_match ("/\. (jpg | jpeg | gif | png) $/", $ this-> filename, $ matches ))
- {
- $ This-> input_type = strtolower ($ matches [1]);
- }
- Else
- {
- $ This-> input_type = "string ";
- }
- }
- }
|