我有一个存储在数据库中的图像,我想识别特定像素的RGB代码。我想用PHP替换我的新RGB值。有人可以帮我提供有效的代码吗?
答案 0 :(得分:2)
<?
//create an image resource depending on your image type. http://php.net/manual-lookup.php?pattern=imagecreatefrom*&scope=quickref
$imgh = imagecreatefrompng("/path/to/png/image.png");
$xpos = 10;
$ypos = 14;
//Get the color information of the pixil you want http://php.net/manual/en/function.imagecolorat.php
$rgb = imagecolorat($imgh, $xpos, $ypos);
//Convert to RGB
$r = ($rgb >> 16) & 0xFF;
$g = ($rgb >> 8) & 0xFF;
$b = $rgb & 0xFF;
//Do whatever you need to do to determine new rgb.
$new_r = 12;
$new_g = 58;
$new_b = 200;
//Create a new color to apply to image. http://php.net/manual/en/function.imagecolorallocate.php
$new_color = imagecolorallocate ( $imgh , $new_r , $new_g , $new_b );
//replace pixel with new color http://php.net/imagesetpixel
imagesetpixel($imgh,$xpos,$ypos,$new_color);
//Save image to new filename. http://www.php.net/manual/en/function.imagepng.php
imagepng($imgh,'/path/to/png/new.png');
?>