Jump to content

Archived

This topic is now archived and is closed to further replies.

HAMM3R

Invert image colors

Recommended Posts

Hey.  I got this script from http://www.phparadise.de/php-code/image-editing/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:
[code]
<?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');

?>
[/code]

Thanks in advance!

Share this post


Link to post
Share on other sites
Have you tried changing the jpeg to png in:

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

?

Share this post


Link to post
Share on other sites
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.

[code=php:0]
<?php
if (imagetypes() & IMG_PNG) {
  echo "PNG Support is enabled";
}
?>[/code]

Share this post


Link to post
Share on other sites
Hmm.. give this a try:

replace
[code]else $bild = imagecreatefrompng($input);
[/code]

to

[code]
elseif($type =='png') $bild = imagecreatefrompng($input);
[/code]

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.