0

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

4

1 回答 1

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');
?>
于 2013-11-14T15:44:12.970 回答