Php implements the text and image watermark techniques for uploading images,

Source: Internet
Author: User
Tags imagejpeg learn php programming

Php implements the text and image watermark techniques for uploading images,

The functions implemented in this article are especially suitable for some malls and image sites. They share the skills of adding text and image watermarks when uploading images for your reference. The specific content is as follows:

1. water. class. php

<? Phpheader ('content-Type: text/html; charset = UTF-8 ');/** To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. * /// Add the watermark Class Water to the image {// enable the watermark private $ watermark_on = '1'; public $ water_img; // The watermark position public $ pos = 1; // compression ratio public $ pct = 80; // transparency public $ quality = 80; public $ text = 'fun Network zlblog.sinaapp.com '; public $ size = 12; public $ color =' #000000 '; public $ font = 'font. ttf'; public function watermark ($ img, $ pos = '', $ out_img ='', $ water_img = '', $ text ='') {if (! $ This-> check ($ img) |! $ This-> watermark_on) return false; $ water_img = $ water_img? $ Water_img: $ this-> water_img; // watermark enabling Status $ waterimg_on = $ this-> check ($ water_img )? 1: 0; // determine whether to operate on the source image $ out_img = $ out_img? $ Out_img: $ img; // determine the watermark position $ pos = $ pos? $ Pos: $ this-> pos; // watermark text $ text = $ text? $ Text: $ this-> text; $ img_info = getimagesize ($ img); $ img_w = $ img_info [0]; $ img_h = $ img_info [1]; // determine the watermark image type if ($ waterimg_on) {$ w_info = getimagesize ($ water_img); $ w_w = $ w_info [0]; $ w_h = $ w_info [1]; if ($ img_w <$ w_w | $ img_h <$ w_h) return false; switch ($ w_info [2]) {case 1: $ w_img = imagecreatefromgif ($ water_img ); break; case 2: $ w_img = imagecreatefromjpeg ($ water_img); break; case 3: $ W_img = imagecreatefrompng ($ water_img); break ;}} else {if (empty ($ text) | strlen ($ this-> color )! = 7) return FALSE; $ text_info = imagettfbbox ($ this-> size, 0, $ this-> font, $ text ); $ w_w = $ text_info [2]-$ text_info [6]; $ w_h = $ text_info [3]-$ text_info [7];} // create the source image resource switch ($ img_info [2]) {case 1: $ res_img = imagecreatefromgif ($ img); break; case 2: $ res_img = imagecreatefromjpeg ($ img ); break; case 3: $ res_img = imagecreatefrompng ($ img); break;} // determine the watermark Position switch ($ pos) {case 1: $ x = $ y = 25; break; case 2: $ x = ($ img_w-$ w_w)/2; $ y = 25; break; case 3: $ x = $ img_w-$ w_w; $ y = 25; break; case 4: $ x = 25; $ y = ($ img_h-$ w_h)/2; break; case 5: $ x = ($ img_w-$ w_w)/2; $ y = ($ img_h-$ w_h)/2; break; case 6: $ x = $ img_w-$ w_w; $ y = ($ img_h-$ w_h)/2; break; case 7: $ x = 25; $ y = $ img_h-$ w_h; break; case 8: $ x = ($ img_w-$ w_w)/2; $ y = $ img_h-$ w_h; break; case 9: $ x = $ img_w-$ w_w; $ y = $ img_h-$ w_h; break; default: $ x = mt_rand (25, $ img_w-$ w_w); $ y = mt_rand (25, $ img_h-$ w_h);} // write image resources if ($ waterimg_on) {imagecopymerge ($ res_img, $ w_img, $ x, $ y, 0, 0, $ w_w, $ w_h, $ this-> pct);} else {$ r = hexdec (substr ($ this-> color, 1, 2 )); $ g = hexdec (substr ($ this-> color, 3, 2); $ B = hexdec (substr ($ this-> color, 5, 2 )); $ color = imagecolorallocate ($ res_img, $ r, $ g, $ B); imagettftext ($ res_img, $ this-> size, 0, $ x, $ y, $ color, $ this-> font, $ text);} // generate an image type switch ($ img_info [2]) {case 1: imagecreatefromgif ($ res_img, $ out_img); break; case 2: // imagecreatefromjpeg ($ res_img, $ out_img); imagejpeg ($ res_img, $ out_img); break; case 3: imagejpeg ($ res_img, $ out_img); break ;} if (isset ($ res_img) imagedestroy ($ res_img); if (isset ($ w_img) imagedestroy ($ w_img); return TRUE ;} // verify whether the image has private function check ($ img) {$ type = array('.jpg', '.hangzhou', '.png', '.gif '); $ img_type = strtolower (strrchr ($ img ,'. '); return extension_loaded ('gd') & file_exists ($ img) & in_array ($ img_type, $ type );}}

2. test1.php

<?php /*  * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. *///header('Content-Type:text/html;charset=utf-8');include 'water.class.php';$image = new Water();$image->watermark('12.jpg',5);//$image->watermark('12.jpg',1);

3.

The above is all the content of this article. I hope it will help you learn PHP programming.

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.