You have several problems:
imagecreatefromstring
doesn't load images from a file, but takes a string that contains image data.imagecreatefromjpeg
will load from a file.- To display a page as an image, you have to send the proper header. For example,
header('Content-Type: image/jpeg');
imagejpeg
outputs an image. The return value is just whether or not it succeeded. Take a look at the example in the documentation.
To delete every color that isn't black/gray/white, you probably have to check each pixel individually, like so:
<?php
header('Content-type: image/jpeg');
$image = imagecreatefromjpeg('file.jpg');
$white = imagecolorallocate($image, 255, 255, 255);
for($x = 0; $x < imagesx($image); $x++) {
for($y = 0; $y < imagesy($image); $y++) {
$pixel = imagecolorat($image, $x, $y);
if(!isGray($pixel))
imagesetpixel($image, $x, $y, $white);
}
}
imagejpeg($image);
function isGray($pix) {
$r = ($pix >> 16) & 0xFF;
$g = ($pix >> 8) & 0xFF;
$b = $pix & 0xFF;
return ($r == $g) && ($g == $b);
}
?>
Since you're using JPGs, I can't think of an easier way. The artifacts the JPG compression process creates disrupt solid colors, so simply checking for, e.g., red or blue wouldn't work.
The code was adapted from https://stackoverflow.com/a/1607796/246847.