Created
November 17, 2013 08:22
-
-
Save mattcdavis1/7510787 to your computer and use it in GitHub Desktop.
Replace all pixels of one color with pixels of another color
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$img = \imagecreatefromjpeg('someimage.jpg'); | |
for ($y = 0; $y < imagesy($img); $y++) { | |
for ($x = 0; $x < imagesx($img); $x++) { | |
// find color index at x,y | |
$pixelColorIndex = imagecolorat($img, $x, $y); | |
$pixelColorRGB = imagecolorsforindex($img, $pixelColorIndex); | |
// set new color if test color matches | |
if ($pixelColorRGB['red'] == 255 && $pixelColorRGB['green'] == 255 && $pixelColorRGB['blue'] == 255) { | |
$newPixelColorIndex = imagecolorallocate($img, 231, 230, 230); | |
imagesetpixel($img, $x, $y, $newPixelColorIndex); | |
} | |
} | |
} | |
imagejpeg($img, 'somenewimage.jpg', 100); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment