首页 > php图像处理 > php 给图片加上文字水印代码

php 给图片加上文字水印代码

给图片加水印是很多网站会做的事情,下面这段代码只支持给图片加上文字水印,现在暂时只支持英文的,如果要让此程序能支持中文汉字就把$font=\'verdana.ttf\';调用一个中文字体就OK了,实例代码如下:

<?php
$image = $_get[id];
$im = getimagesize($image);
//print_r($im);
//以下是读取文字的宽高
$font = 'verdana.ttf'; //字体
$font_size = $im[0] / 20; //文字大小
$size = imagettfbbox($font_size, 0, $font, 'phprm.com'); //文字宽高
$textwidth = $size[4]; //取出宽
$textheight = abs($size[7]); //取出高
$pic_w = abs(($im[0] - $textwidth) / 2); //文字存放宽的位置
$pic_h = abs(($im[1] - $textheight) / 2); //文字存放高的位置
switch ($im[2]) {
    case 1:
        $im = imagecreatefromgif($image);
        break;
    case 2:
        $im = imagecreatefromjpeg($image);
        break;
    case 3:
        $im = imagecreatefrompng($image);
        break;
}
//$logo="2.png";//商标图片
//$ing=getimagesize($logo);
//
// switch($ing[2])
//
//{
//case 1:
//$in=imagecreatefromgif($logo);
//break;
//
//case 2:
//$in=imagecreatefromjpeg($logo);
//break;
//
//case 3:
//$in=imagecreatefrompng($logo);
//break;
//}
//imagecopy($im,$in,200,3,0,0,'120','110');//图片合并
$te = imagecolorclosestalpha($im, 255, 255, 255, 60); //最后一个数值是透明度越大越透明
$str = iconv("gbk", "utf-8", "phprm.com"); //设置文字的内容和编码,很简单吧
imagettftext($im, $font_size, 0, $pic_w, $pic_h, $te, 'verdana.ttf', $str);
header("content-type: image/jpeg");
imagejpeg($im);


本文地址:http://www.phprm.com/tuxiang/fs4489.html

转载随意,但请附上文章地址:-)

标签:php水印 php文字水印

相关文章

发表留言