我得到了這樣的圖像(這是一個圖):可以在PHP中使用GD庫來交換圖像中的顏色嗎?
Gold Trade Graph http://www.kitconet.com/charts/metals/platinum/tny_pt_en_caoz_2.gif
我想改變顏色,所以白色是黑色的,圖線是淺藍色的,等等。是否有可能用GD和PHP實現?
我得到了這樣的圖像(這是一個圖):可以在PHP中使用GD庫來交換圖像中的顏色嗎?
Gold Trade Graph http://www.kitconet.com/charts/metals/platinum/tny_pt_en_caoz_2.gif
我想改變顏色,所以白色是黑色的,圖線是淺藍色的,等等。是否有可能用GD和PHP實現?
我不知道任何現成的功能。但我想你可以去槽圖像的每個像素,並改變它的顏色...
我還沒有嘗試過自己,但你可以看看GD庫中的功能imagecolorset()它做了一個顏色填充效果,這可能有助於白色背景。
您可以嘗試imagefilter功能http://lv.php.net/imagefilter - 但這不會讓您直接訪問用另一種顏色替換一種顏色,只需更改r/g/b組件即可。
可以使用imagesetpixel http://nl2.php.net/imagesetpixel來設置新像素值,從而實現極低級別的解決方案。
我遇到了麻煩,使該解決方案的工作。圖像不能是真實的彩色圖像。先將它轉換爲imagetruecolortopalette();
$imgname = "test.gif";
$im = imagecreatefromgif ($imgname);
imagetruecolortopalette($im,false, 255);
$index = imagecolorclosest ($im, 255,255,255); // get White COlor
imagecolorset($im,$index,92,92,92); // SET NEW COLOR
$imgname = "result.gif";
imagegif($im, $imgname); // save image as gif
imagedestroy($im);
我知道這是晚了,事後,但我已經把一個腳本,這將做一個稍大的規模。希望遇到這個帖子的人可以使用它。它需要許多源圖像是單色圖層(您的選擇)。您爲它提供源顏色和每個圖層的色調,並且該腳本將返回一個合成圖像(具有完全透明度),該圖像專用於您提供的十六進制代碼。
查看下面的代碼。更詳細的解釋可以在我的blog上找到。
function hexLighter($hex, $factor = 30) {
$new_hex = '';
$base['R'] = hexdec($hex{0}.$hex{1});
$base['G'] = hexdec($hex{2}.$hex{3});
$base['B'] = hexdec($hex{4}.$hex{5});
foreach ($base as $k => $v) {
$amount = 255 - $v;
$amount = $amount/100;
$amount = round($amount * $factor);
$new_decimal = $v + $amount;
$new_hex_component = dechex($new_decimal);
$new_hex .= sprintf('%02.2s', $new_hex_component);
}
return $new_hex;
}
// Sanitize/Validate provided color variable
if (!isset($_GET['color']) || strlen($_GET['color']) != 6) {
header($_SERVER['SERVER_PROTOCOL'] . ' 400 Bad Request', true, 400);
exit(0);
}
if (file_exists("cache/{$_GET['color']}.png")) {
header('Content-Type: image/png');
readfile("cache/{$_GET['color']}.png");
exit(0);
}
// Desired final size of image
$n_width = 50;
$n_height = 50;
// Actual size of source images
$width = 125;
$height = 125;
$image = imagecreatetruecolor($width, $height);
imagesavealpha($image, true);
imagealphablending($image, false);
$n_image = imagecreatetruecolor($n_width, $n_height);
imagesavealpha($n_image, true);
imagealphablending($n_image, false);
$black = imagecolorallocate($image, 0, 0, 0);
$transparent = imagecolorallocatealpha($image, 255, 255, 255, 127);
imagefilledrectangle($image, 0, 0, $width, $height, $transparent);
$layers = array();
$layers_processed = array();
$layers[] = array('src' => 'layer01.gif', 'level' => 0); // Border
$layers[] = array('src' => 'layer02.gif', 'level' => 35); // Background
$layers[] = array('src' => 'layer03.gif', 'level' => 100); // White Quotes
foreach ($layers as $idx => $layer) {
$img = imagecreatefromgif($layer['src']);
$processed = imagecreatetruecolor($width, $height);
imagesavealpha($processed, true);
imagealphablending($processed, false);
imagefilledrectangle($processed, 0, 0, $width, $height, $transparent);
$color = hexLighter($_GET['color'], $layer['level']);
$color = imagecolorallocate($image,
hexdec($color{0} . $color{1}),
hexdec($color{2} . $color{3}),
hexdec($color{4} . $color{5})
);
for ($x = 0; $x < $width; $x++)
for ($y = 0; $y < $height; $y++)
if ($black === imagecolorat($img, $x, $y))
imagesetpixel($processed, $x, $y, $color);
imagecolortransparent($processed, $transparent);
imagealphablending($processed, true);
array_push($layers_processed, $processed);
imagedestroy($img);
}
foreach ($layers_processed as $processed) {
imagecopymerge($image, $processed, 0, 0, 0, 0, $width, $height, 100);
imagedestroy($processed);
}
imagealphablending($image, true);
imagecopyresampled($n_image, $image, 0, 0, 0, 0, $n_width, $n_height, $width, $height);
imagealphablending($n_image, true);
header('Content-Type: image/png');
imagepng($n_image, "cache/{$_GET['color']}.png");
imagepng($n_image);
// Free up memory
imagedestroy($n_image);
imagedestroy($image);
woot這個答案幫助我也事後! GD在功能上很長,但文檔在現實世界的例子中很短。 – 2010-04-16 14:42:56