LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 08-03-2004, 06:59 PM   #1
Proud
Senior Member
 
Registered: Dec 2002
Location: England
Distribution: Used to use Mandrake/Mandriva
Posts: 2,794

Rep: Reputation: 116Reputation: 116
PHP PNG image file parsing


Hi, I've just started messing with PHP and Apache, and knocked up the below quick script as a quirky idea. It's meant to read the hex colours of each pixel of a PNG file, and then print them as the colour for the background of a table division in a html page.

It does result in a grid of coloured cubes but my problem is the colour is wrong compared to the source image viewed enlarged in, say, the Gimp.

I suspect the problem lies in the int->hex code I copied from the PHP Manual, with the bitshifting and masking.
PHP Code:
<html>
<head>
<title>Pixel Picker</title>
</head>
<body>

<?php    
    $file 
"/usr/share/icons/crystalsvg/32x32/apps/kmenu.png";
    
$image ImageCreateFromPng($file);
    
$size GetImageSize($file);
    
$width $size[0];
    
$height $size[1];
    
    echo 
"<table>";
    
    for (
$i 0$i $height$i++) {
    
    echo 
"<tr>" ;
    
        for (
$j 0$j $width$j++) {
        
$rgb ImageColorAt($image$j$i);
        
$r = ($rgb >> 16) & 0xFF;
        
$g = ($rgb >> 8) & 0xFF;
        
$b $rgb 0xFF;

        echo 
"<td bgcolor=#";
        echo 
base_convert($r1016);
        echo 
base_convert($g1016);
        echo 
base_convert($b1016);
        echo 
"\">&nbsp&nbsp</td>";
        }
    
    echo 
"</tr>";
    }
    echo 
"</table>";
        
?>

</body>
</html>
Just wondering if anyone had a quick solution. Plus pointing out any bad coding habits would be appreciated.

Last edited by Proud; 08-03-2004 at 07:00 PM.
 
Old 08-03-2004, 07:10 PM   #2
hack124x768
Member
 
Registered: Mar 2004
Location: everett
Distribution: slak
Posts: 61

Rep: Reputation: 15
all i can think of would be that php may be set to only work with web standard colors, only 255. wheras your image may b in high, or true, colors, a lot more.
 
Old 08-04-2004, 01:20 PM   #3
Proud
Senior Member
 
Registered: Dec 2002
Location: England
Distribution: Used to use Mandrake/Mandriva
Posts: 2,794

Original Poster
Rep: Reputation: 116Reputation: 116
Well I improved the code and it works now well, except for not using the transparency values of the pixels.
PHP Code:
<html>
<head>
<title>Pixel Picker</title>
</head>
<body>

<?php    
    $file 
"/usr/share/icons/crystalsvg/32x32/apps/kmenu.png";
    
$image ImageCreateFromPng($file);
    
$size GetImageSize($file);
    
$width $size[0];
    
$height $size[1];
    
    echo 
"<table>\n";
    
    for (
$i 0$i $height$i++) {
    
    echo 
"<tr>\n" ;
    
        for (
$j 0$j $width$j++) {
        
$rgb ImageColorAt($image$j$i);
        
$r = ($rgb >> 16) & 0xFF;
        
$g = ($rgb >> 8) & 0xFF;
        
$b $rgb 0xFF;

        echo 
"<td bgcolor=\"#";
        
$red strtoupper(base_convert($r1016));
        
$green strtoupper(base_convert($g1016));
        
$blue =  strtoupper(base_convert($b1016));
        
        if (
strlen($red) == 1) {
        echo 
"0";
        echo 
$red;
        }
        else {
        echo 
$red;
        }
        if (
strlen($green) == 1) {
        echo 
"0";
        echo 
$green;
        }
        else {
        echo 
$green;
        }
        if (
strlen($blue) == 1) {
        echo 
"0";
        echo 
$blue;
        }
        else {
        echo 
$blue;
        }
        
        echo 
"\">&nbsp&nbsp</td>";
        }
    
    echo 
"\n</tr>\n";
    }
    echo 
"</table>\n";
        
?>

</body>
</html>
 
Old 08-04-2004, 01:34 PM   #4
Cedrik
Senior Member
 
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140

Rep: Reputation: 242Reputation: 242Reputation: 242
You can also short your code by replace your last if blocks by conditional statement like :

PHP Code:
//instead of:
if (strlen($red) == 1) {
        echo 
"0";
        echo 
$red;
} else {
        echo 
$red;

// I would do:
echo (strlen($red) == 1) ? "0$red" $red
 
Old 08-04-2004, 01:55 PM   #5
Proud
Senior Member
 
Registered: Dec 2002
Location: England
Distribution: Used to use Mandrake/Mandriva
Posts: 2,794

Original Poster
Rep: Reputation: 116Reputation: 116
Nice! Thanks
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to compress a .png image in Gimp? ganoo Linux - Software 10 09-30-2014 12:45 AM
plucker - image parsing problem chii-chan Linux - Software 3 12-07-2005 10:51 PM
Problem with PNG image in Java on Redhat Shariq Mustaquim Linux - Software 2 06-24-2005 07:37 AM
Image conversor bmp & png to jpeg Adony Linux - Software 2 06-19-2004 04:43 PM
failed to load image switchdesk.png file not found..?? Brutal Linux - General 2 09-03-2003 06:27 PM


All times are GMT -5. The time now is 01:27 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration