PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Fri Sep 20, 2019 10:42 pm

All times are UTC - 5 hours




Post new topic Reply to topic  [ 19 posts ]  Go to page 1, 2  Next
Author Message
PostPosted: Tue Jan 02, 2007 11:27 am 
Offline
Forum Newbie

Joined: Wed Aug 16, 2006 11:45 pm
Posts: 16
onion2k | Please use
Syntax: [ Download ] [ Hide ]
,
[syntax=php]and
Syntax: [ Download ] [ Hide ]
  1. tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: to learn how to do it too. 
  2.  
  3.  
  4.  
  5.  
  6.  
  7. Hi, 
  8.  
  9.  
  10.  
  11. I've had great success getting the following code to work with GIF files in the same directory as the script. I've been tinkering with the code trying to get it to work for JPEG files. Stumped. Any ideas? 
  12.  
  13. Thanks. 
  14.  
  15.  
  16.  
  17. Andrew 
Syntax: [ Download ] [ Hide ]
<?PHP

function bildinfo($bild)

{

/*

******* FUNCTION BILDINFO() ******* ******* ******* *******

******* ******* ******* ******* ******* ******* ******* *******

******* Copyright 2003 by http://www.BLACKEYE.org ******* *******

******* ******* ******* ******* ******* ******* ******* *******

Please don't change the copyright in this file. You can modify

this file, but NEVER remove the copyright !

If YOU were the creator of this function, you would say the

same, eh ?



For more informations / How to use:

HOMEPAGE: http://www.blackeye.org

******* ******* ******* ******* ******* ******* ******* *******

*/


$fp = fopen($bild, "rb"); // Open image in binary mode (for windows)

$f = fread($fp, filesize($bild));

$c = bin2hex($f); // Binary file -> Hex

$c = ereg_replace("5c30", "0000", $c); // In gif-files "5c30" stands for real "00" values. why ? i dont know

$b = $c;



$pos = strpos($c, "fe0102"); // 6 Byte termination for the color table

if ($pos > 0)

{

$c = substr($c, 26);

$c = substr($c, 0, strpos($c, "fe0102")); // Termination for the color table of non-transparent images

}

else

{

$c = substr($c, 26);

$c = substr($c, 0, strpos($c, "21f904")); // Termination for the color table

}



echo "<table border='0'>";



$i = 0;

$y = 0;

$str = "";

$pix = chunk_split($c, 6, ";"); // 6 Char long block (color)

$pix = explode(";",$pix);

sort($pix);

foreach($pix as $col)

{

if ($col && !ereg($col.";", $str) && strlen($col) == 6) // So that no color is twice in the list

{

$str .= $col.";";

$i++; // F?r die Table anzeige

$y++; // Anzahl Farben

if ($i == 1) { echo ""; }

echo "<td width='6' height='8' bgcolor='#$col' style='border:1px solid #000000;font-size:1px;' title='?2003 by http://www.BLACKEYE.org'>";

echo " ";

echo "</td>";

if ($i == 10)

{

echo ""; $i = 0;

}

}

}



echo "</table>";

echo $y." visible colors";

}



$bild = ""; // Imagefile

bildinfo($bild);

?>

Only copy & paste this text into your .php file, where you want to show the image.


onion2k | Please use
Syntax: [ Download ] [ Hide ]
,
[syntax=php]and
Syntax: [ Download ] [ Hide ]
  1. tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: to learn how to do it too. 


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 02, 2007 11:39 am 
Offline
Jedi Mod
User avatar

Joined: Tue Dec 21, 2004 6:03 pm
Posts: 5263
Location: usrlab.com
GIF files and JPEG files are completely and utterly different. You'll need to rewrite that script to decompress JPEG's LWZ compression. That's will be incredibly difficult because it's complicated stuff.

Alternatively, rewrite the script to use GD's imagecolorat() function.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 02, 2007 11:54 am 
Offline
Forum Newbie

Joined: Wed Aug 16, 2006 11:45 pm
Posts: 16


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 02, 2007 12:05 pm 
Offline
Jedi Mod
User avatar

Joined: Tue Dec 21, 2004 6:03 pm
Posts: 5263
Location: usrlab.com


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 02, 2007 12:58 pm 
Offline
Forum Contributor
User avatar

Joined: Wed Dec 06, 2006 10:33 am
Posts: 454
Location: Picnic Garden, Kolkata, India


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 02, 2007 1:10 pm 
Offline
Jedi Mod
User avatar

Joined: Tue Dec 21, 2004 6:03 pm
Posts: 5263
Location: usrlab.com


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 02, 2007 1:18 pm 
Offline
Forum Contributor
User avatar

Joined: Wed Dec 06, 2006 10:33 am
Posts: 454
Location: Picnic Garden, Kolkata, India


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 02, 2007 2:22 pm 
Offline
Forum Newbie

Joined: Wed Aug 16, 2006 11:45 pm
Posts: 16


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 02, 2007 2:46 pm 
Offline
Jedi Mod
User avatar

Joined: Tue Dec 21, 2004 6:03 pm
Posts: 5263
Location: usrlab.com


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 02, 2007 2:56 pm 
Offline
Forum Newbie

Joined: Wed Aug 16, 2006 11:45 pm
Posts: 16


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 02, 2007 3:01 pm 
Offline
Jedi Mod
User avatar

Joined: Tue Dec 21, 2004 6:03 pm
Posts: 5263
Location: usrlab.com
Syntax: [ Download ] [ Hide ]
$image = imagecreatefromjpeg($imageFilename);

$canvas_x = imagesx($image);

$canvas_y = imagesy($image);



        for ($x=0;$x<$canvas_x;$x++) {

                for ($y=0;$y<$canvas_y;$y++) {



                        $rgb = imagecolorat($image,$x,$y);

                        $r   = ($rgb >> 16) & 0xFF;

                        $g   = ($rgb >>  & 0xFF;

                        $b   = $rgb & 0xFF;



                        $r_total[$r]++;

                        $g_total[$g]++;

                        $b_total[$b]++;



                        $pixelcount++;



                }

        }



        imagedestroy($image);



        $r_range = (max($r_total)-min($r_total));

        $g_range = (max($g_total)-min($g_total));

        $b_range = (max($b_total)-min($b_total));



        $histogram = imagecreatetruecolor(300,324);



        $white = imagecolorallocate($histogram,255,255,255);

        imagefilledrectangle($histogram,0,0,300,324,$white);

        imagecolordeallocate($histogram,$white);



        $black = imagecolorallocate($histogram,0,0,0);



        for ($x=0;$x<256;$x++) {



                imageline($histogram,($x+20),100,($x+20),(100-(($r_total[$x]/$r_range)*80)),$black);

                $red = imagecolorallocate($histogram,255,$x,$x);

                imageline($histogram,($x+20),102,($x+20),104,$red);

                imagecolordeallocate($histogram,$red);



                imageline($histogram,($x+20),200,($x+20),(200-(($g_total[$x]/$g_range)*80)),$black);

                $green = imagecolorallocate($histogram,$x,255,$x);

                imageline($histogram,($x+20),202,($x+20),204,$green);

                imagecolordeallocate($histogram,$green);



                imageline($histogram,($x+20),300,($x+20),(300-(($b_total[$x]/$b_range)*80)),$black);

                $blue = imagecolorallocate($histogram,$x,$x,255);

                imageline($histogram,($x+20),302,($x+20),304,$blue);

                imagecolordeallocate($histogram,$blue);



        }



        imagestring($histogram,2,200,104,"Red Channel",$black);

        imagestring($histogram,2,200,204,"Green Channel",$black);

        imagestring($histogram,2,200,304,"Blue Channel",$black);



        header("Content-Type: image/jpeg");

        imageJPEG($histogram);

        imagedestroy($histogram);


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 02, 2007 3:24 pm 
Offline
DevNet Master
User avatar

Joined: Wed Dec 06, 2006 5:14 pm
Posts: 3635
Location: Toronto, Canada
You could use imagecolorstotal() to see what you're dealing with, then imagetruecolortopalette() to reduce it to a reasonable number if you so desire. This will be especially useful if you're expecting logos with just a few colours, as imagetruecolortopalette() will allow you to dither the result and restrict the number of colours in the final product.

I can only assume the most predominant colours will remain in the final palette, which is what I gather you're looking for.

If you get really fancy, you could write an algorithm that reduces the number of colours and compares the similarity of all the colours until only distinct ones are left.... I'd better stop now :-)


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 02, 2007 3:26 pm 
Offline
Forum Newbie

Joined: Wed Aug 16, 2006 11:45 pm
Posts: 16
Thanks everybody for your suggestions and help. I'll get to coding and let you know if I hit any roadblocks.

Andrew


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 04, 2007 3:45 am 
Offline
Forum Contributor
User avatar

Joined: Mon Nov 13, 2006 5:19 am
Posts: 137
Location: Argentina and Italy
I'll try to find a similar script I made long time ago.
One thing I remember is that using PNG(24) images was better than using JPG files but at this very moment I don't remember exactly why, if I find it I'll let you know.


Last edited by Jaxolotl on Thu Jan 04, 2007 4:21 am, edited 2 times in total.

Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 04, 2007 4:00 am 
Offline
Jedi Mod
User avatar

Joined: Tue Dec 21, 2004 6:03 pm
Posts: 5263
Location: usrlab.com


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 19 posts ]  Go to page 1, 2  Next

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group