PHP에서 GD 라이브러리를 사용하여 이미지에서 색상을 교체 할 수 있습니까?
-
19-08-2019 - |
문제
나는 다음과 같은 이미지를 얻었습니다 (그래프입니다).
(원천: kitconet.com)
색상을 변경하고 싶습니다. 흰색은 검은 색이고 그래프 라인은 밝은 파란색 등입니다. GD 및 PHP로 달성 할 수 있습니까?
다른 팁
이 솔루션을 작동시키는 데 어려움이있었습니다. 이미지는 진정한 색상 이미지가 될 수 없습니다. 먼저 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);
나는 이것이 늦고 사실 이후에 있다는 것을 알고 있지만, 나는 이것을 약간 더 큰 규모로 수행 할 대본을 만들었습니다. 이 게시물을 방문한 사람 이이 게시물을 사용할 수 있기를 바랍니다. 하나의 색상 레이어 (선택) 인 여러 소스 이미지가 필요합니다. 소스 색상과 각 레이어의 색조를 제공하고 스크립트는 제공된 16 진 코드에 특별히 색상을 입은 복합 이미지 (전체 투명성)를 반환합니다.
아래 코드를 확인하십시오. 내에서 더 자세한 설명을 찾을 수 있습니다 블로그.
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 );
나는 그것을 직접 시도하지 않았지만 당신은 기능을 볼 수 있습니다 imagecolorset () GD 라이브러리에서는 효과와 같은 색상을 채우므로 흰색 배경에 도움이 될 수 있습니다.
ImageFilter 기능을 시도 할 수 있습니다 http://lv.php.net/imagefilter - 그러나 그것은 당신의 직접적인 액세스 권한을 제공하지 않으며, 한 색상을 다른 색으로 바꾸고 R/G/B 구성 요소를 변경합니다.
imagesetpixel을 사용하여 매우 낮은 수준의 솔루션을 구현할 수 있습니다. http://nl2.php.net/imagesetpixel 새 픽셀 값을 설정합니다.
IMG_FILTER_NEGATE : 이미지의 모든 색상을 뒤집습니다.
http://www.php.net/manual/en/function.imagefilter.php
그것이 해결책 일 수 있습니까?
느리지 만 확실한 접근 방식은 모든 픽셀을 반복합니다.
function ReplaceColour($img, $r1, $g1, $b1, $r2, $g2, $b2)
{
if(!imageistruecolor($img))
imagepalettetotruecolor($img);
$col1 = (($r1 & 0xFF) << 16) + (($g1 & 0xFF) << 8) + ($b1 & 0xFF);
$col2 = (($r2 & 0xFF) << 16) + (($g2 & 0xFF) << 8) + ($b2 & 0xFF);
$width = imagesx($img);
$height = imagesy($img);
for($x=0; $x < $width; $x++)
for($y=0; $y < $height; $y++)
{
$colrgb = imagecolorat($img, $x, $y);
if($col1 !== $colrgb)
continue;
imagesetpixel ($img, $x , $y , $col2);
}
}
기성품 기능을 모릅니다. 그러나 나는 당신이 이미지의 모든 픽셀을 가로 질러 색상을 바꿀 수 있다고 생각합니다 ...