Skip to content

Instantly share code, notes, and snippets.

@mattcdavis1
Created November 17, 2013 08:22
Show Gist options
  • Save mattcdavis1/7510787 to your computer and use it in GitHub Desktop.
Save mattcdavis1/7510787 to your computer and use it in GitHub Desktop.
Replace all pixels of one color with pixels of another color
<?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