Add a watermark to a picture (support Chinese) and generate thumbnails

Source: Internet
Author: User
Tags int size
Thumbnail Map | Chinese

?

// **************************************** //
Features: Add watermarks to pictures (support Chinese) and generate thumbnails
Parameters: $srcFile picture file name
$dstFile Save picture file name
$markwords Watermark Text Content
$markimage Watermark Picture Address
Save width $dstW Picture
Save Height $dstH Picture
$rate Picture Preservation Quality
// **************************************** //
function Makethumb ($srcFile, $dstFile, $dstW, $dstH, $rate =100, $markwords =null, $markimage =null)
{
$data = getimagesize ($srcFile);
Switch ($data [2])
{
Case 1:
$im = @ImageCreateFromGIF ($srcFile);
Break
Case 2:
$im = @ImageCreateFromJPEG ($srcFile);
Break
Case 3:
$im = @ImageCreateFromPNG ($srcFile);
Break
}
if (! $im) return False;
$srcW =imagesx ($im);
$srcH =imagesy ($im);
$dstX = 0;
$dstY = 0;
if ($srcW * $dstH > $srcH * $dstW)
{
$fdstH = Round ($srcH * $dstW/$srcW);
$dstY = Floor (($dstH-$fdstH)/2);
$fdstW = $dstW;
}
Else
{
$fdstW = Round ($srcW * $dstH/$srcH);
$dstX = Floor (($dstW-$fdstW)/2);
$fdstH = $dstH;
}
$ni =imagecreatetruecolor ($dstW, $dstH);
$dstX = ($dstX <0) 0: $dstX;
$dstY = ($dstX <0) 0: $dstY;
$dstX = ($dstX > ($dstW/2)) Floor ($dstW/2): $dstX;
$dstY = ($dstY > ($dstH/2)) floor ($dstH/s): $dstY;
$white = Imagecolorallocate ($ni, 255,255,255);
$black = Imagecolorallocate ($ni, 0,0,0);
Imagefilledrectangle ($ni, 0,0, $dstW, $dstH, $white);/Fill Background color
Imagecopyresized ($ni, $im, $dstX, $dstY, 0,0, $fdstW, $fdstH, $srcW, $srcH);

//Generate watermarks
if ($markwords!=null)
{
$markwords =iconv ("gb2312", "UTF-8", $markwords);
//Convert text Encoding
Imagettftext ($ni, 9,0,10,15, $white, "Simhei.ttf", $markwords);
//imagettftext (int im,int size,int angle,int x,int y,int col,string fontfile,string text):
//This function will TTF (TrueType Fo NTS) font text is written to the picture.
//parameter: size is a glyph dimension;
///angle is the angle of the font, clockwise, 0 degrees horizontal (from left to right), 90 degrees to the top of the text;
//X,y Two parameter is the coordinate value of the text (the origin is upper left corner);
//col to Word The
//fontfile is the font file name;
//text is the string content.
}
ElseIf ($markimage!=null)
{
$wimage _data = getimagesize ($markimage);
Switch ($wimage _data[2])
{
Case 1:
$wimage = @ImageCreateFromGIF ($markimage);
break;
Case 2:
$wimage = @ImageCreateFromJPEG ($markimage);
break;
Case 3:
$wimage = @ImageCreateFromPNG ($markimage);
break;
}
Imagecopy ($ni, $wimage, 0,0,0,0,88,31);
Imagedestroy ($wimage);
}

Imagejpeg ($ni, $dstFile, $rate);
Imagedestroy ($im);
Imagedestroy ($ni);
End graphics, freeing up memory space
}
?>



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.