首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >混浊重放

混浊重放
EN

Stack Overflow用户
提问于 2014-08-23 06:19:16
回答 2查看 1.6K关注 0票数 3

我想在另一个大图像上添加一个小图像,作为不透明的水印。

我用imagecopyresampled把图像放在另一个图像上。

但是,如何提供水印图像的不透明度。

请帮帮我。

我使用这个简单的示例代码在图像上添加水印,而没有透明度:

代码语言:javascript
复制
<?php

$background = imagecreatefrompng("background.png");

if ($background !== false) {
    $watermark = imagecreatefrompng("watermark.png");
    // Add watermark on background
    imagecopyresampled($background,$watermark,
        100, 100, 0, 0,
        128, 128, 128, 128);
    // Add image header
    header("Content-type: image/png");
    imagepng($background);
    imagedestroy($background);
}

例如,

这是背景或主要图像。

这是水印图像

我要这种类型的输出

是否可以在PHP中使用?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-08-23 06:35:27

尝试使用这个开放源码PHP项目:

https://github.com/Sybio/ImageWorkshop

票数 2
EN

Stack Overflow用户

发布于 2014-09-15 10:32:34

只需使用这个简单的PHP函数:

代码语言:javascript
复制
<?php

function filter_opacity(&$img, $opacity) //params: image resource id, opacity in percentage (eg. 80)
{
    if (!isset($opacity)) {
        return false;
    }
    $opacity /= 100;

    //get image width and height
    $w = imagesx($img);
    $h = imagesy($img);

    //turn alpha blending off
    imagealphablending($img, false);

    //find the most opaque pixel in the image (the one with the smallest alpha value)
    $minalpha = 127;
    for ($x = 0; $x < $w; $x++) {
        for ($y = 0; $y < $h; $y++) {
            $alpha = (imagecolorat($img, $x, $y) >> 24) & 0xFF;
            if ($alpha < $minalpha) {
                $minalpha = $alpha;
            }
        }
    }

    //loop through image pixels and modify alpha for each
    for ($x = 0; $x < $w; $x++) {
        for ($y = 0; $y < $h; $y++) {
            //get current alpha value (represents the TANSPARENCY!)
            $colorxy = imagecolorat($img, $x, $y);
            $alpha = ($colorxy >> 24) & 0xFF;
            //calculate new alpha
            if ($minalpha !== 127) {
                $alpha = 127 + 127 * $opacity * ($alpha - 127) / (127 - $minalpha);
            } else {
                $alpha += 127 * $opacity;
            }
            //get the color index with new alpha
            $alphacolorxy = imagecolorallocatealpha($img, ($colorxy >> 16) & 0xFF, ($colorxy >> 8) & 0xFF, $colorxy & 0xFF, $alpha);
            //set pixel with the new color + opacity
            if (!imagesetpixel($img, $x, $y, $alphacolorxy)) {
                return false;
            }
        }
    }

    return true;
}

使用示例:

代码语言:javascript
复制
<?php
$image = imagecreatefrompng("img.png");
filter_opacity($image, 75);
header("content-type: image/png");
imagepng($image);
imagedestroy($image);

来源:http://php.net/manual/en/function.imagefilter.php

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25459046

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档