简体   繁体   English

使用php GD库将透明渐变与图像混合

[英]Blend transparent gradient with an image using php GD library

I try to render gradient on top of an image it need to be from full color to transparent, here is my code. 我尝试在图像上渲染渐变,它需要从全彩色到透明,这是我的代码。 I get black image and if I put start more then 0 I got white gradient but no image. 我得到黑色图像,如果我开始多一点,那么0我得到白色渐变,但是没有图像。 the output image is 338x100 px but the input image need to be aligned to right if the image is narrower. 输出图像为338x100像素,但如果图像较窄,则输入图像需要右对齐。

function hex2rgb($hex) {
    $rgb[0] = hexdec(substr($hex, 0, 2));
    $rgb[1] = hexdec(substr($hex, 2, 2));
    $rgb[2] = hexdec(substr($hex, 4, 2));
    return $rgb;
}

function int2rgb($color) {
    $result[] = ($color >> 16) & 0xFF;
    $result[] = ($color >> 8) & 0xFF;
    $result[] = $color & 0xFF;
    return $result;
}

if (isset($_GET['start']) && isset($_GET['stop']) && isset($_GET['color'])) {
    $input = imagecreatefrompng('file.png');
    $width = imagesx($input);
    $output = imagecreatetruecolor(338, 100);
    $color = hex2rgb($_GET['color']);
    $fill = imagecolorallocate($output, $color[0], $color[1], $color[2]);


    for ($x=0; $x<$_GET['start']; ++$x) {
        for ($y=0; $y<100; ++$y) {
            imagesetpixel($output, $x, $y, $fill);
        }
    }
    $range = $_GET['stop']-$_GET['start'];
    for ($x=$_GET['start']; $x<$_GET['stop']; ++$x) {
        $alpha = round(255-($x*255/$range));
        $correct_x = $width < 338 ? $x+$width-338 : $x;
        for ($y=0; $y<100; ++$y) {
            $input_color = int2rgb(imagecolorat($input, $correct_x, $y));

            $new_color = imagecolorallocate($output,
                                            (($color[0]-$alpha)*$input_color[0])/255,
                                            (($color[1]-$alpha)*$input_color[1])/255,
                                            (($color[2]-$alpha)*$input_color[2])/255);
            imagesetpixel($output, $x, $y, $new_color);
        }
    }
    if ($_GET['stop']<338) {
        $stop = $width < 338 ? $_GET['stop']+$width-338 : $_GET['stop'];
        imagecopy($input, $output, $stop, 0, $_GET['stop'], 0, 338-$stop, 100);
        header('Content-Type: image/png');
        imagepng($output);
    }
}

I run the script with gradient.php?start=20&stop=200&color=ff0000 and got this instead of red gradient. 我使用gradient.php?start=20&stop=200&color=ff0000运行脚本并获得此代替红色渐变。

在此输入图像描述在此输入图像描述

How to make that gradient red from full color to full transparent? 如何使渐变红色从全彩色变为完全透明? So it look like this: 所以它看起来像这样:

在此输入图像描述

If you create a image using imagecreatetruecolor it gets a black background. 如果使用imagecreatetruecolor创建图像,它将获得黑色背景。 Using imagefill you can change the background of an image easily. 使用imagefill可以轻松更改图像的背景。 The imagecolorallocatealpha function let's you create a color containing transparency. 使用imagecolorallocatealpha函数可以创建包含透明度的颜色。 127 means fully transparent and 0 not transparent. 127表示完全透明,0表示不透明。

It works now and I simplified your code a bit: 它现在有效,我简化了你的代码:

function hex2rgb($hex) {
    $rgb[0] = hexdec(substr($hex, 0, 2));
    $rgb[1] = hexdec(substr($hex, 2, 2));
    $rgb[2] = hexdec(substr($hex, 4, 2));
    return $rgb;
}

if (isset($_GET['start']) && isset($_GET['stop']) && isset($_GET['color'])) {
    $color = hex2rgb($_GET['color']);
    $range = $_GET['stop']-$_GET['start'];

    // create input image
    $input = imagecreatefrompng('file.png');


    // create output image
    $height = imagesy($input);
    $width = imagesx($input);
    $output = imagecreatetruecolor($width, $height);

    // put a transparent background on it
    $trans_colour = imagecolorallocatealpha($output, 0, 0, 0, 127);
    imagefill($output, 0, 0, $trans_colour);

    // create the gradient
    for ($x=0; $x < $width; ++$x) {
        $alpha = $x <= $_GET['start'] ? 0 : round(min(($x - $_GET['start'])/$range, 1)*127);
        $new_color = imagecolorallocatealpha($output, $color[0], $color[1], $color[2], $alpha);
        imageline($output, $x, $height, $x, 0, $new_color);
    }

    // copy the gradient onto the input image
    imagecopyresampled($input, $output, 0, 0, 0, 0, $width, $height, $width, $height);

    // output the result
    header('Content-Type: image/png');
    imagepng($input);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用gd库php增加图像大小并叠加在透明图像上 - increase size of image and superimpose on transparent image using gd library php 如何使用PHP GD库将具有不同大小的多个透明png合并到一张图像中,而无需裁剪? - How to merge multiple transparent pngs with different size into one image without cropping, using PHP GD library? 我正在尝试使用PHP中的GD库使图像透明,但运行以下代码,只有一部分是透明的 - I'm trying to make a image transparent using GD library from PHP but running following code, only a portion will be transparent PHP GD:模糊获得透明图像 - PHP GD: Blur for transparent image 无法使用GD库php上传图像 - unable to upload an image using gd library php 使用PHP GD库上传图片时出错 - Error in image upload using PHP GD Library 如何使用Gd库调整图像大小? 的PHP - How to resize image using Gd library ? PHP PHP GD库-在将一张GD半透明图像覆盖到另一张GD半透明图像上时发现意外的白点 - PHP GD library - Unexpected white spots found on overlaying one gd semi-transparent image over the other 可以使用GD和PHP为透明图像添加背景颜色 - Possible to add background color to transparent image using GD and PHP 如何使用PHP GD旋转图像并在透明背景下调整大小 - How to rotate image and resize with transparent background using PHP GD
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM