Php 透明png上的渐变覆盖
在我的网站上,我有一个PHP脚本,它根据一个Php 透明png上的渐变覆盖,php,image,png,image-manipulation,linear-gradients,Php,Image,Png,Image Manipulation,Linear Gradients,在我的网站上,我有一个PHP脚本,它根据一个$\u GET查询将同一个图像转换为重新存储的版本。查询为07a的示例如下 以下是执行此操作的PHP脚本: <?php if (isset($_GET['color']) && $_GET['color'] !== "") $color = preg_match('/^([a-fA-F0-9]{3}|[a-fA-F0-9]{6})$/',$_GET['color']) ? $_GET['color'] : '777777
$\u GET
查询将同一个图像转换为重新存储的版本。查询为07a
的示例如下
以下是执行此操作的PHP脚本:
<?php
if (isset($_GET['color']) && $_GET['color'] !== "") $color = preg_match('/^([a-fA-F0-9]{3}|[a-fA-F0-9]{6})$/',$_GET['color']) ? $_GET['color'] : '777777';
else $color = '000000';
if (strlen($color) === 3) $color = $color[0].$color[0].$color[1].$color[1].$color[2].$color[2];
$color = str_split($color,2);
foreach ($color as $k=>$m){ $color[$k] = intval($color[$k],16); }
$img = imageCreateFromPng('splat-base.png');
$background = imagecolorallocate($img, 0, 0, 0);
imagecolortransparent($img, $background);
imagealphablending($img, false);
imagesavealpha($img, true);
imagefilter($img, IMG_FILTER_COLORIZE, intval($color[0] - 255 ,16), $color[1], $color[2]);
header('Content-type: image/png');
imagePng($img);
imagedestroy($img);
exit;
?>
实际工作是由imagefilter
函数完成的,其余的只是为了保持透明度。我想做的是,通过$\u GET
发送两个颜色值,并使用给定的颜色值创建应用于图像的自上而下渐变。下面是一个示例,其值为08f
和0a7
(颜色总是较浅):
我想到的一个可能的解决方案是用梯度生成与我的splat base.png
(1000 x 1000 px)大小相同的图像,然后通过一些技巧将其应用于splat图像,但我不知道如何生成梯度图像,或者如何将其应用于splat图像
我没有能力安装像ImageMagick这样的PHP扩展,因此我必须使用PHP的内置函数和只使用这些函数的库。因此,我在谷歌上搜索了如何在PHP中创建图像渐变。其思想是,定义开始和结束颜色,并定义步长
$stepSize=($end-$start)/($imgHeight)
。创建一个for循环,在循环的每个步骤中,创建一个与图像一样宽的1像素高的矩形,并将其颜色设置为$start+$i*$stepSize
。我在这里发现了这个想法和代码:
该代码创建了一个线性渐变的新图像。对于您的情况,我的基本想法是:从源图像中刮去1个像素行,并将每行IMG\u FILTER\u COLORIZE
调整为渐变的颜色
代码如下:
<?php
//move alpha preserving code to its own function
//since it is used several times
function preserveAlpha(&$img) {
$background = imagecolorallocate($img, 0, 0, 0);
imagecolortransparent($img, $background);
imagealphablending($img, false);
imagesavealpha($img, true);
}
$img = imagecreatefrompng('splat-base.png');
preserveAlpha($img);
//grab width and height
$width = imagesx($img);
$height = imagesy($img);
//the final image
$final = imagecreatetruecolor($width, $height);
preserveAlpha($final);
//start and end values of the gradient
$start = $_GET['start'];
$end = $_GET['end'];
//convert 3 color codes to 6
if (strlen($start) == 3) {
$start = $start[0] . $start[0] . $start[1] . $start[1] . $start[2] . $start[2];
}
if (strlen($end) == 3) {
$end = $end[0] . $end[0] . $end[1] . $end[1] . $end[2] . $end[2];
}
//make sure the lighter color is first
$starthex = intval($start, 16);
$endhex = intval($end, 16);
if ($endhex > $starthex) {
//then we need to flip
$temp = $end;
$end = $start;
$start = $temp;
}
//convert from hex to rgb
$s = array(
hexdec(substr($start, 0, 2)),
hexdec(substr($start, 2, 2)),
hexdec(substr($start, 4, 2))
);
$e = array(
hexdec(substr($end, 0, 2)),
hexdec(substr($end, 2, 2)),
hexdec(substr($end, 4, 2))
);
//the for loop. go through each 1 pixel row of the image,
//shave it off, apply a filter, and append it to our new image
for ($i = 0; $i < $height; $i++) {
//grab the $ith row off of the image
$pixelRow = imagecreatetruecolor($width, 1);
preserveAlpha($pixelRow);
imagecopy($pixelRow, $img, 0, 0, 0, $i, $width, 1);
//calculate the next color in the gradient
$r = intval($s[0] - ((($s[0] - $e[0]) / $height) * $i));
$g = intval($s[1] - ((($s[1] - $e[1]) / $height) * $i));
$b = intval($s[2] - ((($s[2] - $e[2]) / $height) * $i));
//apply the filter
imagefilter($pixelRow, IMG_FILTER_COLORIZE, $r - 255, $g, $b);
//append it to our new iamge
imagecopy($final, $pixelRow, 0, $i, 0, 0, $width, 1);
}
header('Content-Type: image/png');
imagepng($final);
/* Some Cleanup */
imagedestroy($final);
exit;
?>
这似乎是一个有效的解决方案。谢谢你的回答!