Home  >  Article  >  Backend Development  >  PHP simply supports Chinese watermark program code_PHP tutorial

PHP simply supports Chinese watermark program code_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 16:56:07804browse

The article introduces a simple watermark program that can add Chinese characters to pictures. Friends who need to know more can refer to it.

The code is as follows
 代码如下 复制代码

 

// **************************************** //
// 功能:给图片添加文字
// 参数: $img 图片文件名
// $new_img 另存图片文件名,如果为空表示不另存图片
// $text 字符串内容
// text_size 字符串大小
// text_angle 字型串输出角度
// text_x 字符串输出 x 坐标
// text_y 字符串输出 y 坐标
// $text_font 字型文件名
// $r,$g,$b 字符串颜色RGB值
// **************************************** //
function img_text($img, $new_img, $text, $text_size, $text_angle, $text_x, $text_y, $text_font, $r, $g, $b){

$text=iconv("gb2312","UTF-8",$text);
Header("Content-type: image/gif");
$im = @imagecreatefromstring(file_get_contents($img)) or die ("打开图片失败!");
$color = ImageColorAllocate($im, $r,$g,$b);

//ImageTTFText(int im, int size, int angle, int x, int y, int col, string fontfile, string text):
//本函数将 TTF (TrueType Fonts) 字型文字写入图片。
//参数: size 为字形的尺寸;
// angle 为字型的角度,顺时针计算,0 度为水平(由左到右),90 度则为由下到上的文字;
// x,y 二参数为文字的坐标值 (原点为左上角);
// col 为字的颜色;
// fontfile 为字型文件名称;
// text 是字符串内容。
ImageTTFText($im, $text_size, $text_angle, $text_x, $text_y, $color, $text_font, $text);

if ($new_img==""):
ImageGif($im); // 不保存图片,只显示
else:
ImageGif($im,$new_img); // 保存图片,但不显示
endif;

ImageDestroy($im); //结束图形,释放内存空间
}
?>

Copy code

// **************************************** //
// Function: Add text to pictures
// Parameter: $img image file name
// $new_img Save the image file name. If it is empty, it means not to save the image
// $text string content
// text_size string size
// text_angle font string output angle
// text_x string output x coordinate
// text_y string output y coordinate
// $text_font font file name
// $r,$g,$b string color RGB value
// **************************************** //
function img_text($img, $new_img, $text, $text_size, $text_angle, $text_x, $text_y, $text_font, $r, $g, $b){

//ImageTTFText(int im, int size, int angle, int x, int y, int col, string fontfile, string text):
//This function writes TTF (TrueType Fonts) font text into the image.
//Parameter: size is the size of the glyph;
// angle is the angle of the font, calculated clockwise, 0 degrees is horizontal (from left to right), 90 degrees is the text from bottom to top;
// The two parameters x and y are the coordinate values ​​of the text (the origin is the upper left corner);
// col is the color of the word;
// fontfile is the name of the font file;
// text is the string content.
ImageTTFText($im, $text_size, $text_angle, $text_x, $text_y, $color, $text_font, $text);
if ($new_img==""):
ImageGif($im); //Do not save the image, only display it
else:
ImageGif($im,$new_img); // Save the image but do not display it
endif;
ImageDestroy($im); //End graphics and release memory space
}
?>

http://www.bkjia.com/PHPjc/631634.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/631634.htmlTechArticleThe article introduces a simple watermark program that can add Chinese characters to pictures. Friends who need to know more can refer to it. one time. The code is as follows Copy code ?php // *****************...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn