Jump to content


Photo

Invert image colors


  • Please log in to reply
5 replies to this topic

#1 HAMM3R

HAMM3R
  • Members
  • PipPip
  • Member
  • 15 posts

Posted 10 August 2006 - 04:45 AM

Hey.  I got this script from http://www.phparadis...g/invert-image/
It works with jpegs but I cant get it to work with PNGs.  Everytime i try it i see a "The image “http://domain.com/graphs.php” cannot be displayed, because it contains errors." in firefox and IE just tells me action canceled.  Im sure ive just got something wrong in the code.  Can someone please tell me what ive done wrong?  Here is what im using:
<?php

function invert_image($input,$output,$color=false,$type='jpeg')
{
	if($type == 'jpeg') $bild = imagecreatefromjpeg($input);
	else $bild = imagecreatefrompng($input);
	$x = imagesx($bild);
	$y = imagesy($bild);
 
	for($i=0; $i<$y; $i++)
	{
		for($j=0; $j<$x; $j++)
		{
			$pos = imagecolorat($bild, $j, $i);
			$f = imagecolorsforindex($bild, $pos);
			if($color == true)
			{
				$col = imagecolorresolve($bild, 255-$f['red'], 255-$f['green'], 255-$f['blue']);
			}else{
				$gst = $f['red']*0.15 + $f['green']*0.5 + $f['blue']*0.35;
				$col = imagecolorclosesthwb($bild, 255-$gst, 255-$gst, 255-$gst);
			}
			imagesetpixel($bild, $j, $i, $col);
		}
	}
	if(empty($output)) header('Content-type: image/'.$type);
	if($type == 'jpeg') imagejpeg($bild,$output,90);
	else imagepng($bild,$output);
}
$input = 'http://www.game-monitor.com/server-players.php?ip=70.86.246.26:7777&type=day&key=f9f8b008dfd594f80f8f81468f5836e5&timezone=-4.png';
// for a color negative image, set the optional flag
// invert_image($input,'',true,'png');
// for a black and withe negative image use like this
//
// invert_image($input,'');
// if you want to save the output instead of just showing it,
// set the output to the path where you want to save the inverted image
//
// invert_image('path/to/original/image.jpg','path/to/save/inverted-image.jpg');
// if you want to use png you have to set the color flag as
// true or false and define the imagetype in the function call
//
invert_image($input,'',true,'png');

?>

Thanks in advance!

#2 xAtlas

xAtlas
  • Members
  • PipPip
  • Member
  • 18 posts

Posted 10 August 2006 - 05:46 PM

Have you tried changing the jpeg to png in:

function invert_image($input,$output,$color=false,$type='jpeg')

?

#3 HAMM3R

HAMM3R
  • Members
  • PipPip
  • Member
  • 15 posts

Posted 10 August 2006 - 07:23 PM

Yes i tired that and it made no difference :(

#4 xAtlas

xAtlas
  • Members
  • PipPip
  • Member
  • 18 posts

Posted 11 August 2006 - 12:15 PM

Is PNG supported? Most times they are I suppose but you can copy the below, save it as PNG.PHP and upload it to your website then run it: yoursite.com/png.php
It'll tell you if PNG is supported or not. Just one other thing to eliminate.

<?php
if (imagetypes() & IMG_PNG) {
   echo "PNG Support is enabled";
}
?>


#5 xAtlas

xAtlas
  • Members
  • PipPip
  • Member
  • 18 posts

Posted 11 August 2006 - 12:44 PM

Hmm.. give this a try:

replace
else $bild = imagecreatefrompng($input);

to

elseif($type =='png') $bild = imagecreatefrompng($input);


#6 HAMM3R

HAMM3R
  • Members
  • PipPip
  • Member
  • 15 posts

Posted 13 August 2006 - 03:26 AM

Yeah it says PNG support is enabled and i changed that line.  It made no difference :(

Maybe i can convert the png to jpg since i know the jpg invert works?  How do I do that?




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users