【问题标题】:PHP watermarkingPHP水印
【发布时间】:2009-11-13 03:11:40
【问题描述】:

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

    $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 包含透明部分,它应该在该部分显示下面的图像....?

【问题讨论】:

  • 从文件扩展名中获取图像类型是一种幼稚的方法:改用 getimagesize() !

标签: php image gd watermark


【解决方案1】:

用 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);
}

【讨论】:

    【解决方案2】:

    jpg 格式不支持透明度,因此从概念上讲,您必须:

    • 从较大的图像(jpeg)中获取像素并将它们放入缓冲区中
    • 从较小的图像(水印)中抓取不透明的像素并将它们移动到该缓冲区中,并沿途应用 Alpha

    你可能想让图书馆来做这件事。我喜欢ImageMagick,尤其是因为它内置在 php 中……下面是一个示例,说明如何在 PHP 中将其用于此目的:

    // Let's read the images. 
    $glasses = new Imagick(); 
    if (FALSE === $glasses->readImage($dir . '/glasses.png')) 
    { 
        throw new Exception(); 
    } 
    
    $face = new Imagick(); 
    if (FALSE === $face->readImage($dir . '/face.jpg')) 
    { 
        throw new Exception(); 
    } 
    
    // Let's put the glasses on (10 pixels from left, 20 pixels from top of face). 
    $face->compositeImage($glasses, Imagick::COMPOSITE_DEFAULT, 10, 20); 
    

    还有here's the link 到 ImageMagick::compositeImage 的 PHP 手册页(上面的例子就是从这里来的)。

    【讨论】:

      【解决方案3】:

      您是否尝试过使用 imagecopyresampled()? http://php.net/manual/en/function.imagecopyresampled.php

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-12-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-03-15
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多