php 网站 水印,PHP水印

我正在使用此代码创建水印.

$image = '1.jpg';

$overlay = 'stamp.png';

$opacity = "20";

if (!file_exists($image)) {

die("Image does not exist.");

}

// Set offset from bottom-right corner

$w_offset = 0;

$h_offset = 100;

$extension = strtolower(substr($image, strrpos($image, ".") + 1));

// Load image from file

switch ($extension)

{

case 'jpg':

$background = imagecreatefromjpeg($image);

break;

case 'jpeg':

$background = imagecreatefromjpeg($image);

break;

case 'png':

$background = imagecreatefrompng($image);

break;

case 'gif':

$background = imagecreatefromgif($image);

break;

default:

die("Image is of unsupported type.");

}

// Find base image size

$swidth = imagesx($background);

$sheight = imagesy($background);

// Turn on alpha blending

imagealphablending($background, true);

// Create overlay image

$overlay = imagecreatefrompng($overlay);

// Get the size of overlay

$owidth = imagesx($overlay);

$oheight = imagesy($overlay);

// Overlay watermark

imagecopymerge($background, $overlay, $swidth - $owidth - $w_offset, $sheight - $oheight - $h_offset, 0, 0, $owidth, $oheight, $opacity);

imagejpeg($background,$image);

// Destroy the images

imagedestroy($background);

imagedestroy($overlay);

png图像包含一个文本,其他所有区域都是透明的.

但是当我执行这段代码时,它会将png应用于jpg,但是没有保持png的透明度.它显示在一个盒子里.

我怎么能实现这一点.即如果一个png包含透明部分,它应该在该部分显示下面的图像….?

解决方法:

用imagecopy替换imagecopymer解决了这个问题.这是新代码

function watermark($image){

$overlay = '../../../photos/photosets/stamp.png';

$opacity = "20";

if (!file_exists($image)) {

die("Image does not exist.");

}

// Set offset from bottom-right corner

$w_offset = 0;

$h_offset = 100;

$extension = strtolower(substr($image, strrpos($image, ".") + 1));

// Load image from file

switch ($extension)

{

case 'jpg':

$background = imagecreatefromjpeg($image);

break;

case 'jpeg':

$background = imagecreatefromjpeg($image);

break;

case 'png':

$background = imagecreatefrompng($image);

break;

case 'gif':

$background = imagecreatefromgif($image);

break;

default:

die("Image is of unsupported type.");

}

// Find base image size

$swidth = imagesx($background);

$sheight = imagesy($background);

// Turn on alpha blending

imagealphablending($background, true);

// Create overlay image

//$overlay = imagecreatefrompng($overlay);

// Get the size of overlay

$owidth = imagesx($overlay);

$oheight = imagesy($overlay);

$photo = imagecreatefromjpeg($image);

$watermark = imagecreatefrompng($overlay);

// This is the key. Without ImageAlphaBlending on, the PNG won't render correctly.

imagealphablending($photo, true);

// Copy the watermark onto the master, $offset px from the bottom right corner.

$offset = 10;

imagecopy($photo, $watermark, imagesx($photo) - imagesx($watermark) - $offset, imagesy($photo) - imagesy($watermark) - $offset, 0, 0, imagesx($watermark), imagesy($watermark));

// Output to the browser

header("Content-Type: image/jpeg");

imagejpeg($photo,$image);

// Overlay watermark

// Destroy the images

imagedestroy($background);

imagedestroy($overlay);

}

标签:php,image,gd,watermark

来源: https://codeday.me/bug/20190527/1159973.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值