4

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

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

4

3 回答 3

5

用 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);
}
于 2009-11-13T05:20:20.020 回答
3

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

这是 ImageMagick::compositeImage 的 PHP 手册页的链接(上面的示例来自该页面)。

于 2009-11-13T03:20:48.170 回答
0

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

于 2009-11-13T03:18:43.187 回答