Смешать прозрачный градиент с изображением с помощью библиотеки php GD

Я пытаюсь сделать градиент поверх изображения, которое должно быть от полноцветного до прозрачного, вот мой код. Я получаю черное изображение, и если я начинаю больше, то 0, у меня есть белый градиент, но нет изображения. выходное изображение составляет 338×100 px, но входное изображение должно быть выровнено вправо, если изображение уже.

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

Я запускаю скрипт с gradient.php?start=20&stop=200&color=ff0000 и получил это вместо красного градиента.

введите описание изображения здесьвведите описание изображения здесь

Как сделать этот градиент красным от полного цвета до полного прозрачного? Итак, это выглядит так:

введите описание изображения здесь

Если вы создаете образ с помощью imagecreatetruecolor, он получает черный фон. С помощью imagefill вы можете легко изменить фон изображения. Функция imagecolorallocatealpha позволит вам создать цвет, содержащий прозрачность. 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); }