识别图像的像素 RGB 代码,并使用 PHP 替换为新的 RGB 代码


Identify pixel RGB code of an Image and replace with new RGB code using PHP

我有一个存储在数据库中的图像,我想识别特定像素的RGB代码。我想使用 PHP 将其替换为我的新 RGB 值。任何人都可以帮我提供有效代码吗?

<?
    //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');
?>