Re[2]: [PHP-GTK] load png
| From: | Tom Rogers | Date: | Mon, 13 Dec 2004 22:48:26 +0000 |
| Subject: | Re[2]: [PHP-GTK] load png | ||
| References: | 1 2 3 | Groups: | php.gtk.general |
| Request: | Send a blank email to php-gtk-general+get-5359@lists.php.net to get a copy of this message | ||
Hi,
Tuesday, December 14, 2004, 2:21:39 AM, you wrote:
JDG> Also note, just loading png's without gd or something you are going to
JDG> be on your own.
JDG> You *could* use imagemagick "convert" or something, however having
JDG> done a lot of work with masks you need to be pretty specific on how
JDG> the images are converted if you want the mask to show correctly.
JDG> Anyway, the xpm image functions in the url below should at least get
JDG> you on your way to seeing how the masks work.
JDG> -joeldg
If your willing to accept GD here is a function that I use which also takes care
of the alpha channel.
<?php
function pngtoxpm($input,$bg_red,$bg_green,$bg_blue){
$chars = array(
'
','.','+','@','#','%','&','*','=','-',';','>',',',')','!',
'~','{',']','^','/','(','_',':','<','[','}','|','1','2','3',
'4','5','6','7','8','9','0','a','b','c','d','e','f','g','h',
'i','j','k','l','m','n','o','p','q','r','s','t','u','v','w',
'x','y','z','A','B','C','D','E','F','G','H','I','J','K','L',
'M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','`');
$len = count($chars);
$a = 2;
$b = 0;
$colors = array('000000'=>'. '); //preset black
$img = '';
$name = basename($input,".png");
$info = getimagesize($input);
$im = imagecreatefrompng($input);
for($y=0;$y<$info[1];$y++){
$img .= '"';
for($x=0;$x<$info[0];$x++){
$px = imagecolorat($im,$x,$y);
$rgb = imagecolorsforindex($im,$px);
if($rgb['alpha'] == 127){
$img .= ' '; // completly transparent
}elseif($rgb['alpha']){
// Calculate the colour mix depending on alpha
$bg = sprintf("%0.2f",($rgb['alpha']/127)); //% of background colour
$fg = 1 - $bg; // % of foreground colour
$red = intval($bg * $bg_red) + intval($rgb['red']*$fg);
if($red > 255) $red = 255;
$green = intval($bg * $bg_green) + intval($rgb['green']*$fg);
if($green > 255) $green = 255;
$blue = intval($bg * $bg_blue) + intval($rgb['blue']*$fg);
if($blue > 255) $blue = 255;
$i = sprintf("%02x%02x%02x",$red,$green,$blue);
if(!isset($colors[$i])){
$colors[$i] = $chars[$a].$chars[$b];
$a++;
if($a == $len){
$b++;
$a = 1;
if($b == $len){
echo "Overflow - too many colours\n";
return false;
}
}
}
$img .= $colors[$i]; //add pixel to output
}else{
$i =
sprintf("%02x%02x%02x",$rgb['red'],$rgb['green'],$rgb['blue']);
if(!isset($colors[$i])){
$colors[$i] = $chars[$a].$chars[$b];
$a++;
if($a == $len){
$b++;
$a = 1;
if($b == $len){
echo "Overflow\n";
return false;
}
}
}
$img .= $colors[$i]; //add pixel to output
}
}
$img .='",'."\n"; //new row
}
$c = count($colors)+1;
$r = '';
$r .= "/* XPM */\n";
$r .= 'static char* '.$name.'[] = {'."\n";
$r .= '"'.$info[0].' '.$info[1].' '.$c.'
2"'.",\n";
$r .= '" c None s None",'."\n";
foreach($colors as $hex=>$idx){
$r .= '"'.$idx.' c #'.$hex.'",'."\n";
}
$r .= substr($img,0,-2); //lose the last newline and comma
$r .= '};'."\n";
return $r;
}
$input = 'C:\icons\openoffice-stock\stock_dialog-question-32.png';
//function called with filename and background colour (r,g,b)
echo pngtoxpm($input,214,214,214);
?>
I don't use it that often, which is why the filename is hard coded :)
--
regards,
Tom