phpgtk__legacy/wiki1/wiki.d/Code.DisplayingNonXpmImages

24 lines
16 KiB
Text

version=pmwiki-1.0.5
newline=²
text=[[include:CodeSnippetsInc#TopBar#TopBarEnd]]²! displaying non xpm image using gd ²²this method is not perfect but it works fine for buttons icons or little images and use like said the gd library to work.²''no need for imagemagick or gdkpixbuf extensions anymore !''²it's he only way i found to achieve this on windows OS but work under linux too²it support jpeg png and gif convertion and is quite easy to use²you need to include the functions in your code and then use it like this: ²²[[include:Main/CodeTable]]²<?php²$file = "path to your image file a gif for this example";²²if(! $img = imagecreatefromgif($file))² echo "can't load the image $file\n";²²$xpm_d = image2xpm_d($img);//there's a file convertion function too²imagedestroy($img);//don't forget this one to free memory²²$pix = Gdk::pixmap_create_from_xpm_d($gdkwindow, null, $xpm_d);²$pixmapwid = &new GtkPixmap?($pix[0], $pix[1]);²//and then do what you want²?>²[[include:Main/CodeTableEnd]]²²code to include²²[[include:Main/CodeTable]]²<?php²/**²function set for image manipulation²@author Jonathan Gotti <nathan at the-ring dot homelinux dot net>²@copyright &copy; 2004 Jonathan Gotti²@category images²@license http://opensource.org/licenses/lgpl-license.php GNU Lesser ²General Public Licence²*/²/**²get the 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);² 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'){² $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 the next line² $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]) . $char_code ;² }² }² if($indexed_type == 'xpm')//choose indexed type² $outindex = $char_code;² else//assume gd indexed wanted² $outindex = $gdindex;² ² //the val according to it² switch($value_type){² case 'color':² if($gdcolor['alpha']>=127)//keep transparent on palette image² $out[$outindex] = 'None';² else² $out[$outindex] = strtoupper('#'.² zero_fill(dechex($gdcolor['red']),2).² zero_fill(dechex($gdcolor['green']),2).² zero_fill(dechex($gdcolor['blue']),2));² break;² case 'char_code':² $out[$outindex] = $char_code;² break;² case 'both':² if($gdcolor['alpha']>=127)//keep transparent on palette image² $out[$outindex] = array(0=>$char_code,1=>'None');² else² $out[$outindex] = array( 0=>$char_code,² 1=>strtoupper('#'.zero_fill(dechex($gdcolor['red']),2).² zero_fill(dechex($gdcolor['green']),2).² zero_fill(dechex($gdcolor['blue']),2)));² break;² }² }² return $out;²}²²/**²get xpm_data from gd image resource²@author jonathan.gotti@free.fr²@date 2003-12-20²@param resource gd image $img²@return xpm data²*/²function image2xpm_d($img,$trans=TRUE){² $w = imagesx($img);² $h = imagesy($img);² //the first case is only for PNG with indexed palette² if(imageistruecolor($img) && $trans){² //create an indexed palette copy of $img² //after various test best results are the 3 next lines² //could be fine to have a better way² $imgtmp = imagecreatetruecolor($w,$h);² imagecopymerge($imgtmp,$img,0,0,0,0,$w,$h,100);² imagetruecolortopalette($imgtmp,TRUE,255);//keep an index for trans² ² //set the trans color to a known one² $transindex = imagecolorallocate($imgtmp,1,1,1);² $transindex = imagecolortransparent($imgtmp,$transindex);² ² //get all palette information needed² $gdpalette = imagegetpalette($imgtmp);² $palette = gd2xpmpalette($gdpalette,'gd','both');² ² //image definition² list($color0k,$color0v )= each($palette);² $xpm_d[0]= "$w $h ".count($palette)." ".strlen($color0v[0]);² ² foreach($palette as $color){//recompose palette_data to xpm² $xpm_d[]=$color[0]." c ".$color[1];² }² ² for($y=0;$y<$h;$y++){//recompose image² $row = null;² for($x=0;$x<$w;$x++){² //obtain transparency from the original image² $color = imagecolorsforindex($img,imagecolorat($img,$x,$y));² $index = imagecolorat($imgtmp,$x,$y);² if($color['alpha']>126)² $row .= $palette[$transindex][0];² else² $row .= $palette[$index][0];² }² $xpm_d[]=$row;² }² }else{//non true color png will use this case² if(imageistruecolor($img))² imagetruecolortopalette($img,TRUE,255);² ² $gdpalette = imagegetpalette($img);² $palette = gd2xpmpalette($gdpalette,'gd','both');² ² list($color0k,$color0v )= each($palette);² $xpm_d[0]= "$w $h ".count($palette)." ".strlen($color0v[0]);²² foreach($palette as $color){//recompose palette_data² $xpm_d[]=$color[0]." c ".$color[1];² }² ² 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($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 xpm data²*/²function xpm_d2file($xpm_d,$file){² 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;²}²²/**²left_fill with zero²@param int|str $val to zerofill²@param int $nbchar²@return str²*/²function zero_fill($val,$nbchar=3){² settype($val,'string');² $len = strlen($val);² for($i=0;$i<($nbchar-$len);$i++){² $val = "0$val";² }² return $val;²}²?>²[[include:Main/CodeTableEnd]]²²if you think of a better way to do this or any enhancement please mail me i'll be very very²happy²²{{~Nathan}} at the-ring dot homelinux dot net²[[include:CodeSnippetsInc#BottomBar#BottomBarEnd]]
time=1090874328
diff:1090764879:1090764879:=1,241c1²< %define=codelines bgcolor=#FFFFE0 color=navy font-size=12px border=1%²< ! displaying non xpm image using gd ²< ²< this method is not perfect but it works fine for buttons icons or little images and use like said the gd library to work.²< ''no need for imagemagick or gdkpixbuf extensions anymore !''²< it's he only way i found to achieve this on windows OS but work under linux too²< it support jpeg png and gif convertion and is quite easy to use²< you need to include the functions in your code and then use it like this: ²< %codelines%[=²< <?php²< $file = "path to your image file a gif for this example";²< ²< if(! $img = imagecreatefromgif($file))²< echo "can't load the image $file\n";²< ²< $xpm_d = image2xpm_d($img);//there's a file convertion function too²< imagedestroy($img);//don't forget this one to free memory²< ²< $pix = Gdk::pixmap_create_from_xpm_d($gdkwindow, null, $xpm_d);²< $pixmapwid = &new GtkPixmap?($pix[0], $pix[1]);²< //and then do what you want²< ?>²< =]²< ²< code to include²< %codelines%[=²< <?php²< /**²< function set for image manipulation²< @author Jonathan Gotti <nathan at the-ring dot homelinux dot net>²< @copyright &copy; 2004 Jonathan Gotti²< @category images²< @license http://opensource.org/licenses/lgpl-license.php GNU Lesser ²< General Public Licence²< */²< /**²< get the 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);²< 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'){²< $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 the next line²< $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]) . $char_code ;²< }²< }²< if($indexed_type == 'xpm')//choose indexed type²< $outindex = $char_code;²< else//assume gd indexed wanted²< $outindex = $gdindex;²< ²< //the val according to it²< switch($value_type){²< case 'color':²< if($gdcolor['alpha']>=127)//keep transparent on palette image²< $out[$outindex] = 'None';²< else²< $out[$outindex] = strtoupper('#'.²< zero_fill(dechex($gdcolor['red']),2).²< zero_fill(dechex($gdcolor['green']),2).²< zero_fill(dechex($gdcolor['blue']),2));²< break;²< case 'char_code':²< $out[$outindex] = $char_code;²< break;²< case 'both':²< if($gdcolor['alpha']>=127)//keep transparent on palette image²< $out[$outindex] = array(0=>$char_code,1=>'None');²< else²< $out[$outindex] = array( 0=>$char_code,²< 1=>strtoupper('#'.zero_fill(dechex($gdcolor['red']),2).²< zero_fill(dechex($gdcolor['green']),2).²< zero_fill(dechex($gdcolor['blue']),2)));²< break;²< }²< }²< return $out;²< }²< ²< /**²< get xpm_data from gd image resource²< @author jonathan.gotti@free.fr²< @date 2003-12-20²< @param resource gd image $img²< @return xpm data²< */²< function image2xpm_d($img,$trans=TRUE){²< $w = imagesx($img);²< $h = imagesy($img);²< //the first case is only for PNG with indexed palette²< if(imageistruecolor($img) && $trans){²< //create an indexed palette copy of $img²< //after various test best results are the 3 next lines²< //could be fine to have a better way²< $imgtmp = imagecreatetruecolor($w,$h);²< imagecopymerge($imgtmp,$img,0,0,0,0,$w,$h,100);²< imagetruecolortopalette($imgtmp,TRUE,255);//keep an index for trans²< ²< //set the trans color to a known one²< $transindex = imagecolorallocate($imgtmp,1,1,1);²< $transindex = imagecolortransparent($imgtmp,$transindex);²< ²< //get all palette information needed²< $gdpalette = imagegetpalette($imgtmp);²< $palette = gd2xpmpalette($gdpalette,'gd','both');²< ²< //image definition²< list($color0k,$color0v )= each($palette);²< $xpm_d[0]= "$w $h ".count($palette)." ".strlen($color0v[0]);²< ²< foreach($palette as $color){//recompose palette_data to xpm²< $xpm_d[]=$color[0]." c ".$color[1];²< }²< ²< for($y=0;$y<$h;$y++){//recompose image²< $row = null;²< for($x=0;$x<$w;$x++){²< //obtain transparency from the original image²< $color = imagecolorsforindex($img,imagecolorat($img,$x,$y));²< $index = imagecolorat($imgtmp,$x,$y);²< if($color['alpha']>126)²< $row .= $palette[$transindex][0];²< else²< $row .= $palette[$index][0];²< }²< $xpm_d[]=$row;²< }²< }else{//non true color png will use this case²< if(imageistruecolor($img))²< imagetruecolortopalette($img,TRUE,255);²< ²< $gdpalette = imagegetpalette($img);²< $palette = gd2xpmpalette($gdpalette,'gd','both');²< ²< list($color0k,$color0v )= each($palette);²< $xpm_d[0]= "$w $h ".count($palette)." ".strlen($color0v[0]);²< ²< foreach($palette as $color){//recompose palette_data²< $xpm_d[]=$color[0]." c ".$color[1];²< }²< ²< 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($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 xpm data²< */²< function xpm_d2file($xpm_d,$file){²< 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;²< }²< ²< /**²< left_fill with zero²< @param int|str $val to zerofill²< @param int $nbchar²< @return str²< */²< function zero_fill($val,$nbchar=3){²< settype($val,'string');²< $len = strlen($val);²< for($i=0;$i<($nbchar-$len);$i++){²< $val = "0$val";²< }²< return $val;²< }²< ?>²< =]²< ²< if you think of a better way to do this or any enhancement please mail me i'll be very very²< happy²< ²< {{~Nathan}} at the-ring dot homelinux dot net²---²> Describe DisplayingNonXpmImages here.²\ No newline at end of file²
author=nathan
author:1090764879=nathan
host:1090764879=82.120.18.177
name=Code.DisplayingNonXpmImages
host=82.120.149.253
agent=Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.7) Gecko/20040615 Firefox/0.8 StumbleUpon/1.908
rev=5
diff:1090769457:1090764879:minor=1c1²< %define=codelines bgcolor=#FFFFE0 color=navy font-size=12px border=1%[[notitle]]²---²> %define=codelines bgcolor=#FFFFE0 color=navy font-size=12px border=1%²
author:1090769457=nathan
host:1090769457=82.120.18.177
diff:1090806976:1090769457:minor=1c1²< [[include:CodeSnippetsInc#TopBar#TopBarEnd]]²---²> %define=codelines bgcolor=#FFFFE0 color=navy font-size=12px border=1%[[notitle]]²242d241²< [[include:CodeSnippetsInc#BottomBar#BottomBarEnd]]²\ No newline at end of file²
author:1090806976=nathan
host:1090806976=82.124.244.245
diff:1090874102:1090806976:minor=9c9²< [[include:Main/CodeTable]]²---²> %codelines%[=²23c23,24²< [[include:Main/CodeTableEnd]]²---²> =]²> ²25c26²< [[include:Main/CodeTable]]²---²> %codelines%[=²235c236²< [[include:Main/CodeTableEnd]]²---²> =]²
author:1090874102=nathan
host:1090874102=82.120.149.253
diff:1090874328:1090874102:minor=9d8²< ²25d23²< ²27d24²< ²
author:1090874328=nathan
host:1090874328=82.120.149.253