PHP implementation to a picture plus watermark effect _php instance

Source: Internet
Author: User
Tags getcolor image identifier imagecopy imagejpeg rand rtrim strcmp unsupported

PHP implementation to add a watermark to a picture

<?php/** * Function: To add a watermark effect to a picture * $i to add a watermark effect of the picture * $t Watermark Text * $size Text size * $pos Watermark location * $color The color of the text * $flag is a Boolean value, mainly used to distinguish is not the original watermark * $type if $flag is equal to false, the new map added watermark new file name _txt.jpg/function txt ($i, $t = ' copyright
  All ', $size =25, $pos =5, $color = ' Rand ', $flag =true, $type = ' _txt ') {$img = Imagecreatefromjpeg ($i);
  $w = Imagesx ($img);
  $h = Imagesy ($img); $font = DirName (__file__). '
  /font/ls.ttf ';
  $ps = Imagettfbbox ($size, 0, $font, $t);
  $TW = $ps [4];
  $th = ABS ($ps [5]);  
    Switch ($pos) {case 1:break;  
    Case 2:break;  
    Case 3:break;  
    Case 4:break;  
    Case 5: $x = ($w-$TW)/2; $y = ($h-$th)/2+ $th;  
    Case 6:break;  
    Case 7:break;  
    Case 8:break;  
    Case 9:break;
  Default:break;
  } $c = GetColor ($img, $color);
  Imagettftext ($img, $size, 0, $x, $y, $c, $font, $t); 
  if ($flag) {imagejpeg ($img, $i);
    }else{$ext = ext ($i); $PPP = RTrim ($i, '. ')
    $ext); $PPP = $ppp. $type.
    $ext;
  Imagejpeg ($img, $PPP); }} Function GetColor ($i, $c = ' rand ', $a =50) {$cc = ';
    Switch ($c) {case ' white ': $cc =imagecolorallocatealpha ($i, 255,255,255, $a);
    Case of ' black ': $cc =imagecolorallocatealpha ($i, 0,0,0, $a);
    Case ' red ': $cc =imagecolorallocatealpha ($i, 255,0,0, $a);
    Case ' green ': $cc =imagecolorallocatealpha ($i, 0,255,0, $a);
    Case ' Blue ': $cc =imagecolorallocatealpha ($i, 0,0,255, $a);
    Case ' orange ': $cc =imagecolorallocatealpha ($i, 0xff,0x66,0x33, $a);
    Case ' yellow ': $cc =imagecolorallocatealpha ($i, 255,255,0, $a);
    Case ' rand ': $CC =imagecolorallocatealpha ($i, Mt_rand (0,255), Mt_rand (0,255), Mt_rand (0,255), $a);
      Default: $cs = substr ($c, 1);
      $ok = Str_split ($cs, 2);
    $CC = Imagecolorallocatealpha ($i, Hexdec ($ok [0]), Hexdec ($ok [1]), Hexdec ($ok [2]), $a);   
  Break
return $CC;
  /** * function is: return extension/function ext ($f) {$exts = explode ('. ', $f);
Return end ($EXTS); /** * function is: return filename, without extension */function name ($f) {$s = explode ('/', $f);
  $FN = end ($s); return RTrim ($FN, '. ')
Ext ($f));
 }

Let's take another look. Support to add a watermark to a picture and text in two different ways. The picture supports gif,png,jpg three formats, and the watermark image supports PNG and GIF

function Setwater ($IMGSRC, $MARKIMG, $markText, $TextColor, $markPos, $fontType, $markType) {$srcInfo = @getimagesize ($
  IMGSRC);
  $SRCIMG _w = $srcInfo [0];
    
  $SRCIMG _h = $srcInfo [1]; 
      Switch ($srcInfo [2]) {Case 1: $srcim =imagecreatefromgif ($IMGSRC); 
    Break 
      Case 2: $srcim =imagecreatefromjpeg ($IMGSRC); 
    Break 
      Case 3: $srcim =imagecreatefrompng ($IMGSRC); 
    Break 
      Default:die ("Unsupported picture file type"); 
  Exit
    } if (!strcmp ($markType, "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; 
        Switch ($markImgInfo [2]) {Case 1: $markim =imagecreatefromgif ($MARKIMG); 
      Break Case 2: $markim =imagecreatefromjpeg ($marKIMG); 
      Break 
        Case 3: $markim =imagecreatefrompng ($MARKIMG); 
      Break 
        Default:die ("Unsupported watermark picture file type"); 
    Exit
    } $logow = $markImg _w;
  $logoh = $MARKIMG _h;
    } if (!strcmp ($markType, "text")) {$fontSize = 16;
      if (!empty ($markText)) {if (!file_exists ($fontType)) {return;
    } else {return;
    $box = @imagettfbbox ($fontSize, 0, $fontType, $markText);
    $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 = +5;
      $y = +5;
    Break
      Case 2: $x = ($srcImg _w-$logow)/2;
      $y = +5;
    Break
      Case 3: $x = $srcImg _w-$logow-5;
      $y = +15;
    Break
      Case 4: $x = +5;
      $y = ($srcImg _h-$logoh)/2;
    Break Case 5: $x = ($srcImg _w-$logow)/2;
      $y = ($srcImg _h-$logoh)/2;
    Break
      Case 6: $x = $srcImg _w-$logow-5;
      $y = ($srcImg _h-$logoh)/2;
    Break
      Case 7: $x = +5;
      $y = $srcImg _h-$logoh-5;
    Break
      Case 8: $x = ($srcImg _w-$logow)/2;
      $y = $srcImg _h-$logoh-5;
    Break
      Case 9: $x = $srcImg _w-$logow-5;
      $y = $srcImg _h-$logoh-5;
    Break 
      Default:die ("This position does not support");
  Exit
    
  $DST _img = @imagecreatetruecolor ($srcImg _w, $srcImg _h);
    
  Imagecopy ($dst _img, $SRCIM, 0, 0, 0, 0, $srcImg _w, $srcImg _h);
    if (!strcmp ($markType, "img")) {imagecopy ($dst _img, $markim, $x, $y, 0, 0, $logow, $logoh);
  Imagedestroy ($markim);
      
    } if (!strcmp ($markType, "text")) {$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:imagejpeg ($dst _img, $IMGSRC); 
    Break 
      Case 3:imagepng ($dst _img, $IMGSRC);
    Break 
      Default:die ("Unsupported watermark picture file type"); 
  Exit
  } Imagedestroy ($dst _img);
Imagedestroy ($SRCIM);

 }

Parameter description:

$IMGSRC: Target picture, with relative directory address,
$MARKIMG: Watermark image, with relative directory address, PNG and GIF support two formats, such as watermark image in the execution file mark directory, can be written as: Mark/mark.gif
$markText: Watermark text added to a picture
$TextColor: Font color for watermark text
$markPos: Image Watermark added location, value range: 0~9
0: Random position, randomly select a position between the 1~8
1: Top left 2: Top Center 3: Top right 4: center left
5: Picture Center 6: Right Center 7: Bottom left 8: Bottom Center 9: Bottom Right
$fontType: A specific font library with a relative directory address
$markType: The way to add a watermark to the image, IMG represents the image, the text represents the text to add a watermark

Code comments:

Line 4th to 6th: Get the width and height of the target picture
Line 8th to 22nd: Call different functions according to picture type, get action image identifier

getimagesize function Knowledge Point: getimagesize does not need to install GD degree to use, its return value array has four elements. The index value of 0 is the picture height. The index value of 1 is the width of the picture. The index value 2 is the file format of the picture, its value 1 is GIF format, 2 is jpeg/jpg format, and 3 is in PNG format. Index value 3 is the high and wide string for the picture, Height=xxx width=yyy. The returned picture width and height units are all pixels (pixel)

Line 24th to 58th: When you choose a picture to add a watermark to the target picture, get the width and height of the watermark picture, usually the website logo. If the target picture is less than the width or height of the watermark picture or the watermark picture does not exist, jump out of the function.

Return statement knowledge Point: Direct returns means nothing is returned and ends this function directly. can also be interpreted as returning NULL.

Line 60th to 77th: When you choose Text to add a watermark to the target image, first set the size of the watermark text, the default I set to 16PX, you can adjust the font size according to your needs. If the font file does not exist, jump out of the function and finally get the virtual aspect of the formatted text through the Imagettfbbox function.

Imagettfbbox function Knowledge Point: This function returns an array of 8 cells representing the four corners of the outer frame of the text. Index value meaning: 0 represents the lower left corner x position, 1 represents the sitting angle y position, 2 represents the lower right corner x position, 3 represents the lower right corner y position, 4 represents the upper right corner x position, 5 represents the upper right corner y position , 6 represents the X position in the upper left corner, and 7 represents the Y position in the upper left corner. This function also requires support from the GD library and the FreeType library
The Max function returns the largest value in the parameter.

Line 79th to 125th: According to the set of picture watermark location to calculate the specific coordinates, you can be specific to the effect of the watermark location.

Line 127th to 129th: Create a new picture with the same size as the target picture.

Note: Because the Imagecreatetruecolor function range is a black picture, if your target picture is transparent, the resulting new graph will not be transparent.

Line 131th to 162th: According to the picture or text, the final generation of the image added watermark.

Call Description:

Call as a function call, of course, you can also be encapsulated in a class, or you can further subdivide the function as needed to the module can also be. Of course you use this now is no problem, I have tested, please rest assured that use.

Other Notes:

Because the Imagettftext and Imagettfbbox functions need the support of GD Library and FreeType Library, if your running environment does not support the GD library and the FreeType library, then the text mode will not be realized. You can use the Imagestring function to add a text watermark to the picture, while setting the $logow and $logoh values under the text mode.

The Imagejpeg function can also set the quality of the synthesized picture.

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.