<?php
/**
* Converte arquivo de imagem para Pixmap
*/
function file2pixmap($gdkwindow, $file, $ext=null, $return_widget = true, $resize = null)
{
if (! file_exists($file))
return false;
static $pixs;
$trans = true;//the default case
if (!$ext)
$ext = substr($file,-3);
switch (strtolower($ext)) {
case 'peg':
case 'jpg':
if (! $img = imagecreatefromjpeg($file))
return false;
$trans = false;
break;
case 'gif':
if (! $img = imagecreatefromgif($file))
return false;
break;
case 'png':
if (! $img = imagecreatefrompng($file))
return false;
break;
case 'xpm':
$pixs[$file] = @Gdk::pixmap_create_from_xpm($gdkwindow,null, $file);
if (! $return_widget)
return $pixs[$file];
$pixmapwid = &new GtkPixmap($pixs[$file][0], $pixs[$file][1]);
return $pixmapwid;
break;
default:
echo "unsupported image type $file\n";
return false;
}
//redimensionnement de l'image
if (is_array($resize)) {
$imgtmp = imagecreatetruecolor($resize[0],$resize[1]);
imagecopyresized($imgtmp, $img, 0, 0, 0, 0, $resize[0], $resize[1],
imagesx($img),imagesy($img));
imagedestroy($img);
$img = &$imgtmp;
}
$xpm_d = image2xpm_d($img, $trans);
imagedestroy($img);
$pixs[$file] = @Gdk::pixmap_create_from_xpm_d($gdkwindow, null, $xpm_d);
if (! $return_widget)
return $pixs[$file];
$pixmapwid = &new GtkPixmap($pixs[$file][0], $pixs[$file][1]);
return $pixmapwid;
}
/**
* Return gtkpixmap from a gd image pointer
* @param gdkwindow &$gdkwindow the gdkwindow needed to create the pixmap
* generally the the main window->window
* @param image resource $img the gd image resource pointer
* @param bool $trans force to keep transparency
* @param bool $return_widget default is true if set to false then return
* pixmap and mask instead
* @return GtkPixmap gtkpixmap|array('gdkpixmap,gdkbitmap)|false depend on
* $return_widget value
* @uses xpmd2pixmap
*/
function img2pixmap(&$gdkwindow, $img, $trans=true, $return_widget=true)
{
$xpm_d = image2xpm_d($img, $trans);
imagedestroy($img);
return xpmd2pixmap($gdkwindow, $xpm_d, $return_widget);
}
/**
* get xpm_data from gd image resource
* @author jonathan.gotti@free.fr
* @date 2003-12-20
* @param resource gd image $img
* @return xpm data
* @param gtkprogress &$progress is an optional progressbar to trace activity
* (will received a value between 0 to 100)
* @uses gtkgui_refresh, imagegetpalette, gd2xpmpalette,
*/
function image2xpm_d($img, $trans=true, $progress=null)
{
$exectime_start=get_microtime();
if ($GLOBALS['VERBOSE'])
echo "recomposing xpm data \n";
$w = imagesx($img);
$h = imagesy($img);
if(true){
if($progress) {
$progress->set_format_string('keep transparency');
gtkgui_refresh();
}
//creation d'une copie de l'image en mode palette
$imgtmp = imagecreatetruecolor($w, $h);
//pour en recuperer la palette
imagecopymerge($imgtmp, $img, 0, 0, 0, 0, $w, $h, 100);
imagetruecolortopalette($imgtmp, true, 255);
//set the trans color to a known one
$transindex = imagecolorallocate($imgtmp, 1, 1, 1);
$transindex = imagecolortransparent($imgtmp, $transindex);
if ($progress) {
$progress->set_format_string('read palette');
gtkgui_refresh();
}
$gdpalette = imagegetpalette($imgtmp);
if($progress) {
$progress->set_format_string('XPMize palette');
gtkgui_refresh();
}
$palette = gd2xpmpalette($gdpalette, 'gd', 'both');
list($color0k,$color0v )= each($palette);
//image definition
$xpm_d[0]= "$w $h ".count($palette)." ".strlen($color0v[0]);
if ($progress) {
$progress->set_format_string('format palette');
gtkgui_refresh();
}
foreach ($palette as $color) { //recompose palette_data
$xpm_d[]=$color[0]." c ".$color[1];
}
if ($progress) {
$progress->set_format_string('recomposing image');
gtkgui_refresh();
}
for ($y=0;$y<$h;$y++) {//recompose image
$row = null;
for ($x=0;$x<$w;$x++) {
//verifie la transparence sur l'image d'origine
$color = imagecolorsforindex($img, imagecolorat($img, $x, $y));
//recupere l'index de la couleur ds la copie qui nous a donné la palette
$index = imagecolorat($imgtmp, $x, $y);
if ($color['alpha']>126)
$row .= $palette[$transindex][0];
else
$row .= $palette[$index][0];
}
if ($progress) {
$progress->set_percentage(($y+1)/$h);
gtkgui_refresh();
}
$xpm_d[]=$row;
}
} else {
if (imageistruecolor($img))
imagetruecolortopalette($img, true, 255);
if ($progress) {
$progress->set_format_string('read palette');
gtkgui_refresh();
}
$gdpalette = imagegetpalette($img);
if ($progress) {
$progress->set_format_string('XPMize palette');
gtkgui_refresh();
}
$palette = gd2xpmpalette($gdpalette, 'gd', 'both');
list($color0k,$color0v )= each($palette);
//image definition
$xpm_d[0]= ($deletelastcol?($w-1):$w)." $h ".count($palette)." ".
strlen($color0v[0]);
if ($progress) {
$progress->set_format_string('format palette');
gtkgui_refresh();
}
$palette_num = count($palette);
foreach ($palette as $color) {//recompose palette_data
$xpm_d[]=$color[0]." c ".$color[1];
if ($progress) {
$progress->set_percentage(++$i/$palette_num);
gtkgui_refresh();
}
}
if ($progress) {
$progress->set_format_string('recomposing image');
gtkgui_refresh();
}
for ($y=0;$y<$h;$y++) {//recompose image
$row = null;
for ($x=0;$x<($deletelastcol?($w-1):$w);$x++) {
$row .= $palette[imagecolorat($img, $x, $y)][0];
}
$xpm_d[]=$row;
if ($progress) {
$progress->set_percentage(($y+1)/$h);
gtkgui_refresh();
}
}
}
if($imgtmp)
imagedestroy($imgtmp);
return $xpm_d;
}
/**
* write an xpm file from xpm data
* @author jonathan.gotti@free.fr
* @date 2003-12-20
* @param resource gd image $img
* @return bool
*/
function xpm_d2file($xpm_d, $file)
{
if($GLOBALS['VERBOSE'])
echo "write $file\n";
if(! $f = @fopen($file,'w'))
return false;
fputs($f,"/* XPM */\nstatic char * ".basename($file)."[] = {\n");
foreach($xpm_d as $k => $row){
fputs($f,"\"$row\",");
fputs($f,"\n");
}
$pos = ftell($f);
fputs($f,"};");
fclose($f);
return true;
}
function get_microtime()
{
list($usec, $sec) = explode(" ", microtime());
return ((float)$usec + (float)$sec);
}
/**
* get the image palette
* @author jonathan.gotti@free.fr
* @date 2003-12-20
* @param ressource image $img
* @return array
*/
function imagegetpalette($img)
{
$width = imagesx($img);
$height= imagesy($img);
//try an alternate method since the image is a paletted image better 5 or 6
//seconds on a 1280x1024 png 256clrs
for ($i=0; $i<imagecolorstotal($img); $i++) {
$colors[$i]=imagecolorsforindex($img, $i);
}
return $colors;
}
/**
* get a xpm style palette from a gd one
* @author jonathan.gotti@free.fr
* @date 2003-12-20
* @param array $gdpalette
* @param string $indexed_type 'gd' keep the gd_index,'xpm' indexed by xpm char
* code
* @param string $value_type 'char_code' the xpm char code index, 'color' the
* xpm color value,'both' associated value is are array containing xpm char
* code and xpm color value
* @param array $trans array of transparent color values
* @return array
*/
function gd2xpmpalette($gdpalette, $indexed_type='xpm', $value_type='color', $trans=null)
{
if ($GLOBALS['VERBOSE'])
echo "transform to xpm palette \n";
$nb_colors = count($gdpalette);
//initialize char array if needed
if ($indexed_type=='xpm' || $value_type!='color') {
$first_char = 48;//lowest used ASCII char code
$last_char = 126;//highest used ASCII char code
//Thanks to my friend RHONONO for this one
$nb_char = intval(log($nb_colors)/log($last_char-$first_char)) + 1;
for($i=0; $i<=$nb_char; $i++) { //initialize array of char
$chars[$i]=$first_char;
}
}
foreach ($gdpalette as $gdindex => $gdcolor) {
//recompose xpm character coding only if needed
if ($indexed_type=='xpm' || $value_type!='color') {
$char_code = null;//initialise char_code
for($i=0; $i<$nb_char; $i++) {//increment other chars
$chars[0]++;//increment chars
if($chars[$i]>$last_char) { //check chars are in the good range
$chars[$i]=$first_char;
$chars[$i+1]++;
}
$char_code = chr(($chars[$i]==92?47:$chars[$i])) . $char_code ;
}
}
if($indexed_type == 'xpm')//choose indexed type
$outindex = $char_code;
else//assume gd indexed wanted
$outindex = $gdindex;
//the val acoording to it
switch($value_type) {
case 'color':
if($gdcolor['alpha']>=127)
$out[$outindex] = 'None';
else
$out[$outindex] = strtoupper('#'.str_pad(dechex($gdcolor['red']),
2, '0', STR_PAD_LEFT).
str_pad(dechex($gdcolor['green']),
2, '0', STR_PAD_LEFT).
str_pad(dechex($gdcolor['blue']),
2, '0', STR_PAD_LEFT));
break;
case 'char_code':
$out[$outindex] = $char_code;
break;
case 'both':
if($gdcolor['alpha']>=127)
$out[$outindex] = array(0=>$char_code, 1=>'None');
else
$out[$outindex] = array( 0=>$char_code, 1=>strtoupper('#'.
str_pad(dechex($gdcolor['red']), 2, '0', STR_PAD_LEFT).
str_pad(dechex($gdcolor['green']), 2, '0', STR_PAD_LEFT).
str_pad(dechex($gdcolor['blue']), 2, '0', STR_PAD_LEFT)));
break;
}
}
return $out;
}
/*
@author Max Miller <thefoxmail@gmail.com>
@param $url = http://... ou file://... ou /...
@date 21/08/2006
@return MiniXml Element
*/
function xml_load_file($url) {
$win = new Window();
$fp = @fopen(addslashes($url),'r');
if ( !$fp ) {
$win->alert('Erro ao carregar arquivo');
}
$fp = @fopen('/usr/share/kinux/doc/index.xml','r');
while ( $line = @fgets($fp) ) {
$content .= $line . "\n";
}
@fclose($fp);
if ( !$fp ) {
return false;
}
$xml = new MiniXMLDoc();
$xml->fromString($content);
return $xml; //->toArray();
}
?> |