Re[2]: [PHP-GTK] Windows & backgrounds
| From: | Tom Rogers | Date: | Sat, 07 Aug 2004 03:10:15 +0000 |
| Subject: | Re[2]: [PHP-GTK] Windows & backgrounds | ||
| References: | 1 2 3 4 5 6 | Groups: | php.gtk.general |
| Request: | Send a blank email to php-gtk-general+get-4943@lists.php.net to get a copy of this message | ||
Hi,
Saturday, August 7, 2004, 4:01:00 AM, you wrote:
JDG> There are several ways to do this.
JDG> The preferred way:
JDG> http://us3.php.net/manual/en/function.imagettftext.php
JDG> But you need to have php with xpm compiled in.
JDG> Or, the hard way which is a lot of programming:
JDG> Take the following font files I have at
JDG> http://lucifer.intercosmos.net/fonts/
JDG> and map them over to what you need to display. (you can find others
JDG> online as examples of fonts).
JDG> For a "general" purpose (i.e. work on anything/anywhere) the second
JDG> method would be okay, but would require a lot of work and would be
JDG> pain. I would say after you have created the xpm in php, you just use
JDG> the builtin text writing funtions.
JDG> I have some examples here in the source for that weather app I sent
JDG> you a screencap of earlier:
JDG> http://lucifer.intercosmos.net/index.php?view=phpalphawall
JDG> That is heavy on the alpha channels though, so ignore most of that and
JDG> remove the alpha parts if they are around in there too much. Pay
JDG> special attention to the "placetext" function.
JDG> Hope that helps
JDG> -joeldg
Here is a function I made to convert png files to xpm you may find
useful. It will create the transparent xpm and is meant to be used on
the command line. The file to convert is hard coded at the moment.
(the xpm it creates is php safe as there are no $\" characters used)
<?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;
}
//To use:
$input = 'C:\icons\small-icons\icons\stock_delete.png';
//function called with filename and background colour (r,g,b)
echo pngtoxpm($input,214,214,214);
?>
--
regards,
Tom