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

使用php GD库将透明渐变与图像混合,php,image,gd,gradient,Php,Image,Gd,Gradient,我尝试在图像顶部渲染渐变,它需要从全色到透明,这是我的代码。我得到了黑色的图像,若我把起始值设置为0,我得到了白色的渐变,但并没有图像。输出图像为338x100像素,但如果图像较窄,则需要将输入图像向右对齐 function hex2rgb($hex) { $rgb[0] = hexdec(substr($hex, 0, 2)); $rgb[1] = hexdec(substr($hex, 2, 2)); $rgb[2] = hexdec(substr($hex, 4,

我尝试在图像顶部渲染渐变,它需要从全色到透明,这是我的代码。我得到了黑色的图像,若我把起始值设置为0,我得到了白色的渐变,但并没有图像。输出图像为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);
    }
}
函数hex2rgb($hex){ $rgb[0]=hexdec(substr($hex,0,2)); $rgb[1]=hexdec(substr($hex,2,2)); $rgb[2]=hexdec(substr($hex,4,2)); 返回$rgb; } 函数int2rgb($color){ $result[]=($color>>16)和0xFF; $result[]=($color>>8)&0xFF; $result[]=$color&0xFF; 返回$result; } if(isset($获取['start'])和&isset($获取['stop'])和&isset($获取['color'])){ $input=imagecreatefrompng('file.png'); $width=imagesx($input); $output=ImageCreateTureColor(338100); $color=hex2rgb($_GET['color']); $fill=imagecolorallocate($output,$color[0],$color[1],$color[2]);
对于($x=0;$x),如果使用它创建图像,则会获得黑色背景。使用它可以轻松更改图像的背景。该函数可以创建包含透明度的颜色。127表示完全透明,0表示不透明

它现在可以工作了,我简化了您的代码:

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);
}
函数hex2rgb($hex){ $rgb[0]=hexdec(substr($hex,0,2)); $rgb[1]=hexdec(substr($hex,2,2)); $rgb[2]=hexdec(substr($hex,4,2)); 返回$rgb; } if(isset($获取['start'])和&isset($获取['stop'])和&isset($获取['color'])){ $color=hex2rgb($_GET['color']); $range=$\u GET['stop']-$\u GET['start']; //创建输入图像 $input=imagecreatefrompng('file.png'); //创建输出图像 $height=imagesy($input); $width=imagesx($input); $output=ImageCreateTureColor($width,$height); //在上面放一个透明的背景 $trans_color=imagecolorallocatealpha($output,0,0,0127); 图像填充($output,0,0,$trans_color); //创建渐变 对于($x=0;$x<$width;++$x){
$alpha=$x是否必须使用
GD
?@Baba否,如果它能工作的话。太棒了。我花了很长时间才弄明白如何在另一个屏幕上使用它来实现alpha透明度(右)图像的侧面:
$alpha=$x我知道这个问题已经存在了好几年了,不过从图像底部开始,在照片中进行渐变处理,而不是从底部开始,这是翻译这个问题的最佳方式吗?