Jump to content

Php 4.x Invert And B&w


markiemrboo

Recommended Posts

<?php
$im = imagecreatefromjpeg($_GET['image']);

if ($_GET['invert'] == 1 || $_GET['bw'] == 1) {
 imagetruecolortopalette($im, true, 256);
 $tc = imagecolorstotal($im);
 $maxcol = 255;

 for ($i = 0; $i < $tc; $i++) {
 	$col = imagecolorsforindex($im, $i);

 	if ($_GET['bw'] == 1) {
   $min = min($col['red'],$col['green'],$col['blue']);
   $max = max($col['red'],$col['green'],$col['blue']);
   $bw = ($max + $min) / 2;
   imagecolorset($im, $i, $bw, $bw, $bw);
   $col['red'] = $col['green'] = $col['blue'] = $bw;
 	}

 	if ($_GET['invert'] == 1) {
   // make jpeg compressed black proper black so inverting will make white proper white
   if ($col['red'] <= 10 && $col['green'] <= 10 && $col['blue'] <= 10) {
   	$col['red'] = 0;
   	$col['green'] = 0;
   	$col['blue'] = 0;
   }

   imagecolorset($im, $i, $maxcol - $col['red'], $maxcol - $col['green'], $maxcol - $col['blue']);
 	}
 }
}

header("Content-type: image/jpeg");
imagejpeg($im);
imagedestroy($im);
?>

 

Just a little something I hacked up that I thought I might share. It'll open a jpeg image and invert the colours or make it greyscale (or both). If you've using PHP 5 you can use imagefilter() but, well, I am still using 4.x. Might be useful to someone out there I suppose....

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...