PHP水印

时间:2009-11-13 03:11:40

标签: 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图像包含一个文本,其他所有区域都是透明的。 但是当我执行这段代码时,它会将png应用于jpg,但是没有保持png的透明度。它显示在一个方框中。

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

3 个答案:

答案 0 :(得分:4)

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

答案 1 :(得分: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); 

here's the link到ImageMagick :: compositeImage的PHP手册页(上面的例子来自)。

答案 2 :(得分:0)

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