php图片加水印_ATHER Shu(A.S)的Blog

php图片加水印
sshong 发表于2014年8月15日 22:50:42 更新于2014年8月15日 22:58:50
花了点时间为博客图片上传加水印功能。
其实很简单,用的无非是imagecopymerge或者imagecopy方法。
imagecopymerge可以设置水印透明度(可以总体设置水印图片透明度,但是水印图片自身不能带有alpha)。
imagecopy直接把水印覆盖到原图上(水印图片支持自身带有alpha,但不能总体设置水印图片透明度)。

如果想要水印图片带有alpha如png,同时又要支持设置整体水印图片透明度,则需要hack。
/**
* PNG ALPHA CHANNEL SUPPORT for imagecopymerge();
* This is a function like imagecopymerge but it handle alpha channel well!!!
**/
function imagecopymerge_alpha($dst_im, $src_im, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h, $pct){
$opacity=$pct;
// getting the watermark width
$w = imagesx($src_im);
// getting the watermark height
$h = imagesy($src_im);

// creating a cut resource
$cut = imagecreatetruecolor($src_w, $src_h);
// copying that section of the background to the cut
imagecopy($cut, $dst_im, 0, 0, $dst_x, $dst_y, $src_w, $src_h);
// inverting the opacity
$opacity = $opacity;

// placing the watermark now
imagecopy($cut, $src_im, 0, 0, $src_x, $src_y, $src_w, $src_h);
imagecopymerge($dst_im, $cut, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h, $opacity);

参考资料:Alpha support for PHP's imagecopymerge function

演示:
标签:无分类:PHP 阅读:6793
评论
暂无评论
添加评论
您的大名,限长10汉字,20英文(*)
电子信箱(*)
您的网站
正文,限长500汉字,1000英文(*)
验证码(*)
分享:

AltStyle によって変換されたページ (->オリジナル) /