Php allows you to create watermarks and thumbnails in common image formats (object-oriented ),. Php allows you to create watermarks and thumbnails in common image formats (object-oriented). examples in this article share the php watermark and thumbnail creation code, use the object-oriented method to implement common images. use php to implement watermarks and thumbnails for common image formats (object-oriented ),
Examples in this article share the php watermark and thumbnail creation code, and use object-oriented methods to create watermarks and thumbnails for common image formats, such as jpg, png, and gif, the details are as follows:
<? 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'; // creation of thumb // the default thumbnail function enables private $ thumb_on = 1; // public $ thumb_type = 1; // Generate the width of the thumbnail public $ thumb_width; // Generate the height public $ thumb_height of the thumbnail; // Generate the thumbnail suffix public $ thumb_fix = '_ dq '; // thumbnails function processing public function thumb ($ img, $ outfile = '', $ t_type ='', $ t_w = '', $ t_h = '') {// verify that the image meets the requirements if (! $ This-> check ($ img) |! $ This-> thumb_on) return FALSE; // defines the initial value of the thumbnail $ t_type = $ t_type? $ T_type: $ this-> thumb_type; $ t_w = $ t_w? $ T_w: $ this-> thumb_width; $ t_h = $ t_h? $ T_h: $ this-> thumb_height; // Obtain the source image information $ img_info = getimagesize ($ img); $ img_w = $ img_info [0]; $ img_h = $ img_info [1]; // Obtain the image file suffix $ img_type = image_type_to_extension ($ img_info [2]); // obtain the relevant size $ thumb_size = $ this-> thumb_size ($ img_w, $ img_h, $ t_w, $ t_h, $ t_type ); // determine the original image type // use a custom function to determine the image type $ func = "imagecreatefrom ". substr ($ img_type, 1); $ res_img = $ func ($ img); // The thumbnail resource editing image resource moon if ($ img_type = '. g If '| $ img_type = '.png') {$ res_thumb = imagecreate ($ thumb_size [0], $ thumb_size [1]); $ color = imagecolorallocate ($ res_thumb, 255, 0, 0);} else {$ res_thumb = imagecreatetruecolor ($ thumb_size [0], $ thumb_size [1]);} // Create a thumbnail if (function_exists ("imagecopyresampled ")) {imagecopyresampled ($ res_thumb, $ res_img, 0, 0, 0, 0, $ thumb_size [0], $ thumb_size [1], $ thumb_size [2], $ thumb_size [3]);} else {imagecop Yresized ($ res_thumb, $ res_img, 0, 0, 0, 0, $ thumb_size [0], $ thumb_size [1], $ thumb_size [2], $ thumb_size [3]);} // process transparent color if ($ img_type }'.gif '| $ img_type = '.png') {imagecolortransparent ($ res_thumb, $ color );} // Configure the output file name $ outfile = $ outfile? $ Outfile: $ outfile. substr ($ img, 0, strripos ($ img ,'. ')). $ this-> thumb_fix. $ img_type; // save and output the file $ func = "image ". substr ($ img_type, 1); $ func ($ res_thumb, $ outfile); if (isset ($ res_thumb) imagedestroy ($ res_thumb); if (isset ($ res_img )) imagedestroy ($ res_img); return $ outfile;} 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 ($ re S_img, $ this-> size, 0, $ x, $ y, $ color, $ this-> font, $ text );} // generate the 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: imagepng ($ 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 ', '.w.', '.png', '.gif'); $ img_type = strtolower (strrchr ($ img ,'. '); return extension_loaded ('gd') & file_exists ($ img) & in_array ($ img_type, $ type );} // Obtain the proportion of the thumbnail. // Obtain the processing type of the image. private function thumb_size ($ img_w, $ img_h, $ t_w, $ t_h, $ t_type) {// define the thumbnail size $ w = $ t_w; $ h = $ t_h; // define the original image size $ cut_w = $ img_w; $ cut_h = $ img_h; // if ($ img_w <= $ t_w & $ Img_h <$ t_h) {$ w = $ img_w; $ h = $ img_h;} else {if (! Empty ($ t_type) & $ t_type> 0) {switch ($ t_type) {// when the width is fixed, case 1: $ h = $ t_w/$ img_w * $ img_h; break; // case 2: $ w = $ t_h/$ img_h * $ img_w; break; // fixed width, height cropping case 3: $ cut_h = $ img_w/$ t_w * $ t_h; break; // fixed height, width cropping case 4: $ cut_w = $ img_h/$ t_h * $ t_w; break; // proportional scaling default: if ($ img_w/$ t_w)> ($ img_h/$ t_h) {$ h = $ t_w/$ img_w * $ t_h ;} elseif ($ img_w/$ t_w) <($ img_h/$ t_h) {$ w = $ t_h/$ img_h * $ t_w;} else {$ w = $ t_w; $ h = $ t_h ;}}}$ arr [0] = $ t_w; $ arr [1] = $ t_h; $ arr [2] = $ cut_w; $ arr [3] = $ cut_h; return $ arr ;}}
The above is all the content of this article. I hope it will help you learn PHP programming.
Watermark (object-oriented), this example shares the php watermark and thumbnail creation code, using the object-oriented method to implement common diagrams...