The image turns black after the PHP License Watermark. please advise! -Php Tutorial

Source: Internet
Author: User
Tags imagecopy imagejpeg transparent image
The image turns black after the PHP License Watermark. please advise!
// Embed the watermark program. add the text watermark and image watermark respectively !!! Function setWater ($ imgSrc, $ markImg, $ markText, $ TextColor, $ markPos, $ fontType, $ markType, $ fontSize) {/* parameter description: $ imgSrc: target image, it can contain relative directory addresses, $ markImg: watermark image, which can contain relative directory addresses. PNG and GIF formats are supported. for example, if the watermark image is in the mark Directory of the execution file, it can be written: mark/mark.gif $ markText: the watermark text added to the image $ TextColor: the font color of the watermark text $ markPos: the position where the image watermark is added. value range: 0 ~ 90: random position, ranging from 1 ~ Select a random position between 8: 1: Top center left 2: Top Center 3: Top center right 4: Left Center 5: Image Center 6: Right Center 7: bottom center left 8: bottom center 9: bottom center right $ fontType: specifies the fontType Library, which can contain the relative directory address $ markType: The image watermark method. img indicates the image method, text indicates adding a watermark */$ srcInfo = @ getimagesize ($ imgSrc); $ srcImg_w = $ srcInfo [0]; $ srcImg_h = $ srcInfo [1]; switch ($ srcInfo [2]) {case 1: $ srcim = @ imagecreatefromgif ($ imgSrc); if ($ srcim = false) echo "opening failed! "; Break; case 2: // header (" Content-Type: image/jpeg "); $ srcim = @ imagecreatefromjpeg ($ imgSrc); // print_r (" hi, I am jpg file "); // imagejpeg ($ srcim);/* imagejpeg ($ srcim," temp.jpg ");?>
 Hello! Img "); if (! File_exists ($ markImg) | empty ($ markImg) {return;} $ markImgInfo = @ getimagesize ($ markImg); $ markImg_w = $ markImgInfo [0]; $ markImg_h = $ markImgInfo [1]; if ($ srcImg_w <$ markImg_w | $ srcImg_h <$ markImg_h) {return;} // print_r ("
The markimginfo is "); // print_r ($ markImgInfo [2]); switch ($ markImgInfo [2]) {case 1: $ markim = @ imagecreatefromgif ($ markImg ); if ($ markim = false) echo "opening failed! "; Break; case 2: $ markim = @ imagecreatefromjpeg ($ markImg); if ($ markim = false) echo" opening failed! "; Break; case 3: $ markim = @ imagecreatefrompng ($ markImg); if ($ markim = false) echo" opening failed! "; Break; default: die (" unsupported watermark image file type "); exit ;}$ logow = $ markImg_w; $ logoh = $ markImg_h;} if (! Strcmp ($ markType, "text") {// print_r ("
Hello! Text "); // $ fontSize = 86; // middle if (! Empty ($ markText) {// print_r ($ fontType); if (! File_exists ($ fontType) {// print_r ("
Hello! No fonttype "); return ;}} else {// print_r ("
Hello! No marktext "); return;} // print_r ("
Hello! I am before imagettfbbox "); $ box = imagettfbbox ($ fontSize, 0, $ fontType, $ markText); // print_r ("
Hello! I am after imagettfbbox "); $ logow = max ($ box [2], $ box [4])-min ($ box [0], $ box [6]); $ logoh = max ($ box [1], $ box [3])-min ($ box [5], $ box [7]);} if ($ markPos = 0) {$ markPos = rand (1, 9);} switch ($ markPos) {case 1: $ x = + 25; $ y = + 125; break; case 2: $ x = ($ srcImg_w-$ logow)/2; $ y = + 5; break; case 3: $ x = $ srcImg_w-$ logow-135; $ y = + 115; break; case 4: $ x = + 5; $ y = ($ srcImg_h-$ logoh) /2; break; case 5: // print_r ("
Hello! I AM AT 5 "); $ x = ($ srcImg_w-$ logow)/2; $ y = ($ srcImg_h-$ logoh)/2; break; case 6: // print_r ("
Hello! I AM AT 6 "); $ x = $ srcImg_w-$ logow-5; $ y = ($ srcImg_h-$ logoh)/2; break; case 7: $ x = + 25; $ y = $ srcImg_h-$ logoh-115; break; case 8: $ x = ($ srcImg_w-$ logow)/2; $ y = $ srcImg_h-$ logoh-5; break; case 9: // print_r ("
Hi, I am at markpos 9 "); $ x = $ srcImg_w-$ logow-5; $ y = $ srcImg_h-$ logoh-5; break; default: die ("This location does not support"); exit;}/* print_r ("
Hello I am before imagecreatetruecolor! "); Print_r ($ srcImg_w); print_r ($ srcImg_h); */$ dst_img = @ imagecreatetruecolor ($ srcImg_w, $ srcImg_h); // create a blank background, true color image // $ color = imagecolorAllocate ($ img, 200,200,200); // assign a gray image // imagefill ($ img, $ color ); // fill in the Gray $ trans_colour = imagecolorallocatealpha ($ dst_img, 0, 0, 0,127) from the upper left corner; // Transparent image imagefill ($ dst_img, 0, 0, $ trans_colour ); // $ dst_img = imagecreate ($ srcImg_w, $ srcImg_h);/* print_r ($ dst_img); print_r ("
Hello I am after imagecreatetruecolor !! "); */Imagecopy ($ dst_img, $ srcim, 0, 0, 0, 0, $ srcImg_w, $ srcImg_h); // print_r ("
Hello! "); If (! Strcmp ($ markType, "img") {imagecopy ($ dst_img, $ markim, $ x, $ y, 0, 0, $ logow, $ logoh ); imagedestroy ($ markim);} if (! Strcmp ($ markType, "text") {// print_r ("
Hello! Text2 "); $ rgb = explode (',', $ TextColor); $ color = imagecolorallocate ($ dst_img, $ rgb [0], $ rgb [1], $ rgb [2]); imagettftext ($ dst_img, $ fontSize, 0, $ x, $ y, $ color, $ fontType, $ markText );} switch ($ srcInfo [2]) {case 1: imagegif ($ dst_img, $ imgSrc); break; case 2: // print_r ("hello! "); Imagejpeg ($ dst_img, $ imgSrc, 97); // bool imagejpeg (resource image [, string filename [, int quality]) quality is a unique parameter of imagejpeg (0 ~ 100), the default value is 75, 0 is the worst, the file is the smallest, 100 is the best, and the file is the largest. Break; case 3: imagepng ($ dst_img, $ imgSrc); break; default: die ("unsupported watermark image file type"); exit ;} /* $ marked_img = ""; switch ($ srcInfo [2]) {case 1: imagegif ($ dst_img, $ marked_img); break; case 2: // print_r ("hello! "); Imagejpeg ($ dst_img, $ marked_img); break; case 3: imagepng ($ dst_img, $ marked_img); break; default: die ("unsupported watermark image file type"); exit;} */imagedestroy ($ dst_img); imagedestroy ($ srcim); // return $ marked_img ;}


Reply to discussion (solution)

This problem has been solved!

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.