这个是我的gd配置: GD Support enabled GD Version bundled (2.0.34 compatible) FreeType Support enabled FreeType Linkage with freetype FreeType Version 2.1.9 T1Lib Support enabled GIF Read Support enabled GIF Create Support enabled JPG Support enabled PNG Support enabled WBMP Support enabled XBM Support enabled
http://ken.bokele.com/?ArticleID=15492
2、如果是水印图片,则比较麻烦。你需要逐点拷贝到图片上比较简单的做法是预先制作好透明底色图片
之前我的想法是水印图片制作成透明无底色的png,但是水印到图片上后就变成了黑色底色的水印了。
要怎么做才能达到我想要的效果呢?
$imgstream = file_get_contents($image);
$im = imagecreatefromstring($imgstream);
$im2 = imagecreatefromstring($imgstream);for($i=0; $i<imagesy($im); $i++) {
imagecopyresampled ( $im, $im2, $i*0.3, 20+$i, 0, $i, imagesx($im)/2, 1, imagesx($im2), 1 );
}imageJpeg($im);
$imgstream = file_get_contents($image);
$im = imagecreatefromstring($imgstream);$im2 = imagecreate(80,20);
$background_color = imagecolorallocate($im2, 255, 255, 255);
$text_color = imagecolorallocate($im2, 255, 0, 0);
imagestring($im2, 5, 5, 5, "A Simple", $text_color);imagecolortransparent($im2, $background_color);//你把这句注释掉看看imagecopymerge($im, $im2, 10, 10, 0, 0, imagesx($im2), imagesy($im2), 100);imageJpeg($im);如果你的透明水印图片来自文件,你需要在该图片资源的透明色部分用 imagecolorat 函数取得同名色索引
imagecopymerge($source_img, $water_img, $wX, $wY, 0, 0, $width,$height,$w_pct);
http://blog.csdn.net/iasky/archive/2006/04/19/669765.aspx
因为 png 是真彩图片,而 gif 是调色板图片(最多255色)。
gd 处理时是要丢失一些信息的对于:如果用png,虽然没了透明效果,但是锯齿不明显,不认真看是看不出来的。
若没了 透明效果 那么就是一个矩形了,当然没有 锯齿至于:用png的话无论开不开混色模式都不会透明
我没做过(很少使用png),待会试试
先建一张png图片$im = imagecreate(80,20);
$background_color = imagecolorallocate($im, 255, 255, 255);
$text_color = imagecolorallocate($im, 255, 0, 0);
imagestring($im, 5, 5, 5, "A Simple", $text_color);
imagecolortransparent($im, $background_color);
imagepng($im, 'test.png');
然后用它做水印$image = 'http://avatar.csdn.net/0/0/0/1_natici.jpg';
$imgstream = file_get_contents($image);
$im = imagecreatefromstring($imgstream);
$im2 = imagecreatefrompng('test.png');
imagecopymerge($im, $im2, 10, 30, 0, 0, imagesx($im2), imagesy($im2), 100);
imageJpeg($im);
* 图片水印
*
* @param unknown_type $uploadFile
* @param unknown_type $waterImg
* @return unknown
*/
static function waterMark($uploadFile, $waterSize = '100x100', $waterPosition = 0, $waterPadding = 0, $waterTrans = 70, $waterImg, $text, $textSize = 12, $ttfPath)
{
$waterSize = empty($waterSize) ? '100x100' : $waterSize ;
$waterPosition = empty($waterPosition) ? 0 : $waterPosition ;
$waterPadding = empty($waterPadding) ? 0 : $waterPadding ;
$waterTrans = empty($waterTrans) ? 70 : $waterTrans ; if (!is_file($waterImg)) {
die('water file is lost');
}
if (!function_exists('getimagesize')) {
die('unknow getimagesize');
}
if (PHP_VERSION<4.3) {
die('php version < 4.3');
}
$uploadInfo = @getimagesize($uploadFile);
if (!$uploadInfo[0] || !$uploadInfo[1]) {
return false;
}
$water_size=explode('x', $waterSize);
if ($uploadInfo[0]<$water_size[0] || $uploadInfo[1]<$water_size[1]) return false;
switch ($uploadInfo['mime']) {
case 'image/jpeg':
$tmp=@imagecreatefromjpeg($uploadFile);
break;
case 'image/gif':
if (!function_exists('imagecreatefromgif')) {
$water_err=$lang_wm[5];
return false;
} else $tmp=@imagecreatefromgif($uploadFile);
break;
case 'image/png':
$tmp=@imagecreatefrompng($uploadFile);
break;
default:
$water_err=$lang_wm[6];
return false;
}
$size=@getimagesize($waterImg);
$width = $size[0];
$height = $size[1];
$positionPadding = ($waterPadding && $waterPadding > 0) ? $waterPadding : 0; // 边距
switch ($waterPosition) {
// right-bottom
case '0':
$pos_x = $uploadInfo[0] - $width - $positionPadding;
$pos_y = $uploadInfo[1] - $height - $positionPadding;
break;
// left-top
case '1':
$pos_x = $positionPadding;
$pos_y = $positionPadding;
break;
// left-bottom
case '2':
$pos_x = $positionPadding;
$pos_y = $uploadInfo[1] - $height - $positionPadding;
break;
// right-top
case '3':
$pos_x = $uploadInfo[0] - $width - $positionPadding;
$pos_y = $positionPadding;
break;
// mid
case '4':
$pos_x = ($uploadInfo[0] - $width)/2;
$pos_y = ($uploadInfo[1] - $height)/2;
break;
// random
default:
$pos_x = mt_rand(0, ($uploadInfo[0] - $width));
$pos_y = mt_rand(0, ($uploadInfo[1] - $height));
break;
}
//透明度
if($img=@imagecreatefromgif($waterImg)) {
imageAlphaBlending($img, true);
if ($waterTrans) {
@imagecopymerge($tmp, $img, $pos_x, $pos_y, 0, 0, $width, $height, $waterTrans);
} else {
@imagecopy($tmp, $img, $pos_x, $pos_y, 0, 0, $width, $height);
}
}
//文字水印
if($text){
$im = @imagecreatetruecolor($width,$height);
$color = @imagecolorallocate($im,255,255,255);
$text = @imagettftext($tmp,$textSize,45,$pos_x+10,$pos_y+75,$color,$ttfPath,$text);
}
switch ($uploadInfo['mime']) {
case 'image/jpeg':
@imagejpeg($tmp, $uploadFile, 100);
@imagedestroy($tmp);
break;
case 'image/gif':
@imagegif($tmp, $uploadFile);
@imagedestroy($tmp);
break;
case 'image/png':
@imagepng($tmp, $uploadFile);
@imagedestroy($tmp);
break;
default :
return;
} }
GD Support enabled
GD Version bundled (2.0.34 compatible)
FreeType Support enabled
FreeType Linkage with freetype
FreeType Version 2.1.9
T1Lib Support enabled
GIF Read Support enabled
GIF Create Support enabled
JPG Support enabled
PNG Support enabled
WBMP Support enabled
XBM Support enabled
你的代码是原图跟水印图都是同一张图,而且都是png。
我的原图是jpg,水印图是png。不过我这次测试的原图跟水印图都是同一png张图。问题出在哪里呢?
http://avatar.csdn.net/0/0/0/1_natici.jpg 这是你在 csdn 的头像你给出 原图 和 水印 的链接,我帮你看看
原图:http://hi.csdn.net/attachment/201008/10/267791_12814001881g8G.jpg
$image = 'http://hi.csdn.net/attachment/201008/10/267791_12814001881g8G.jpg';
$imgstream = file_get_contents($image);
$im = imagecreatefromstring($imgstream);$im2 = imagecreatefrompng('http://hi.csdn.net/attachment/201008/10/267791_1281400188c66Z.png');//直接拷贝没有问题
//imagecopy($im, $im2, 0, 0, 0, 0, imagesx($im2), imagesy($im2));//重新采样拷贝需指定透明色
$color = imagecolorat($im2, 10, 10); //取得透明部分的颜色索引,坐标视具体图片定
imagecolortransparent($im2, $color); //设置透明颜色
imagecopymerge($im, $im2, 0, 0, 0, 0, imagesx($im2), imagesy($im2), 50);imageJpeg($im);
$color = imagecolorat($im2, 10, 10); //取得透明部分的颜色索引,坐标视具体图片定
imagecolortransparent($im2, $color); //设置透明颜色
之前没认真看你的代码,大概看了下跟我的差不多就以为没区别,谢谢了!