PHP水印

发布于 2024-08-10 21:18:46 字数 1591 浏览 5 评论 0原文

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

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

i am using this code to create 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);

the png image contains a text with all other region as transparent.
but when i execute this code , it applys the png over jpg, but the transparecy is not maintained of the png. it shows in a box.

how can i acheive that . ie if a png contains transaparent part , it should show the below image in that part....?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

强辩 2024-08-17 21:18:46

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

replacing imagecopymerge with imagecopy solved the issue. here is the new code

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);
}
懒的傷心 2024-08-17 21:18:46

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 手册页(上面的例子就来自于此)。

The jpg format doesn't support transparency, so conceptually you will have to:

  • grab the pixels from the larger image (the jpeg) and put them into a buffer
  • grab the non-transparent pixels from the smaller image (the watermark) and move them into that buffer, applying the alpha along the way

You probably want to let a library do this. I like ImageMagick, especially since it's built in to php... here's an example of how to use it for this purpose from 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); 

And here's the link to the PHP manual page for ImageMagick::compositeImage (from which the above example came).

没有伤那来痛 2024-08-17 21:18:46

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

Have you tried using imagecopyresampled()?
http://php.net/manual/en/function.imagecopyresampled.php

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文