在白色PNG图象的颜色覆盖物与透明度 [英] Color overlay on white PNG image with transparency
问题描述
我有以下图片: http://i.stack.imgur.com/mM8cY。 png
我想做的是像在Photoshop中一样创建颜色叠加效果。我需要一些代码,允许我将每个白色像素更改为RGB数字范围为0-255的指定值。我听说过ImageMagick课程,但我无法在任何地方找到它,而且即使有了这门课,我也不知道怎么做。我目前正在尝试使用 imagefilter
,但它不适用于白色图像。这是我目前的代码:
What I would like to do is create a "Color Overlay" effect like in Photoshop. I need some sort of code that will allow me to change each white pixels to the values specified in RGB numbers ranging from 0-255. I have heard about the ImageMagick class, but I could not find it anywhere, and I have no idea how to do it even with that class. I'm currently trying with imagefilter
, but it doesn't work with white images. Here is my current code:
<?php
$match = array();
if (isset($_GET['c']) && preg_match('/^#?(?:[A-Fa-f0-9]{2}){3}$/',$_GET['c'],$match)){
$match = str_split($match[0],2);
foreach ($match as $k=>$m){ $match[$k] = intval($match[$k],16); }
$img = imageCreateFromPng('splat.png');
$background = imagecolorallocate($img, 0, 0, 0);
imagecolortransparent($img, $background);
imagealphablending($img, false);
imagesavealpha($img, true);
//Transformation code
imagefilter($img, IMG_FILTER_COLORIZE, $match[0], $match[1], $match[2]);
header('Content-type: image/png');
imagePng($img);
exit;
}
?>
推荐答案
我找到了解决方案。我所做的只是 - 使用Photoshop - 我在图像上添加了一个红色覆盖图,所以现在它看起来像这样: http://i.stack.imgur.com/mVARN.png
I found the solution. All I did was - using Photoshop - I added a red color overlay on the image, so now it looks like this: http://i.stack.imgur.com/mVARN.png
然后使用以下PHP代码:
And then used the following PHP code:
<?php
$match = array();
$color = isset($_GET['c']) ? $_GET['c'] : false;
if ($color === false) isset($_GET['color']) ? $_GET['color'] : false;
$color = preg_replace('/^#/','',$color);
if (strlen($color) == 3) $color = $color[0].$color[0].$color[1].$color[1].$color[2].$color[2];
if (preg_match('/^(?:[A-Fa-f0-9]{2}){3}$/',$color,$match)){
$match = str_split($match[0],2);
foreach ($match as $k=>$m){ $match[$k] = intval($match[$k],16); }
// Load image
$img = imageCreateFromPng('splat.png');
$background = imagecolorallocate($img, 0, 0, 0);
imagecolortransparent($img, $background);
imagealphablending($img, false);
imagesavealpha($img, true);
imagefilter($img, IMG_FILTER_COLORIZE, intval( intval($match[0],16) - 255 ,16), $match[1], $match[2]);
header('Content-type: image/png');
imagePng($img);
exit;
}
?>
这里的关键是我从输入中减去255红色值:
The key here is that I substracted the 255 red color value from input with this:
intval( intval($match[0],16) - 255 ,16)
并且正确改变了颜色。
这篇关于在白色PNG图象的颜色覆盖物与透明度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!