PHP水印

标签 php image gd watermark

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

    $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 图像包含一个文本,所有其他区域都是透明的。 但是当我执行此代码时,它会在 jpg 上应用 png,但不会保持 png 的透明度。它显示在一个框中。

我怎样才能做到这一点。即如果 png 包含透明部分,它应该在该部分显示下图....?

最佳答案

用 imagecopy 替换 imagecopymerge 解决了这个问题。这是新代码

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水印,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1726894/

相关文章:

php - json_decode 安全

java - 图像IO奇怪的行为重置输入流

c# - 如何创建没有 xmnls 属性的 XML 节点(例如 "image:image")? C#

html - 3 张相同尺寸但样式相同的图片 - 一张看起来尺寸略有不同

linux - 为 map 服务器安装 gd 库时出错

php - 链接到同一 HTML 中 div 内的 div

javascript - 将 Javascript 字符串传递给 php 字符串

php - WAMP 上的 LDAP 身份验证

php - 如何防止在php中使用GD函数imagecreatefrom*时出现 "is not a valid image file"错误?

php - 如何使用 PHP 绘制 3D 几何形状?