LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   PHP PNG image file parsing (https://www.linuxquestions.org/questions/programming-9/php-png-image-file-parsing-213069/)

Proud 08-03-2004 05:59 PM

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. :)

hack124x768 08-03-2004 06:10 PM

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.

Proud 08-04-2004 12:20 PM

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>


Cedrik 08-04-2004 12:34 PM

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


Proud 08-04-2004 12:55 PM

Nice! Thanks :)


All times are GMT -5. The time now is 04:06 AM.