Re: displaying non xpm image using gd

From: Date: Wed, 21 Jan 2004 14:54:35 +0000
Subject: Re: displaying non xpm image using gd
References: 1  Groups: php.gtk.general 
Request: Send a blank email to php-gtk-general+get-4363@lists.php.net to get a copy of this message
thanks :-) Your code is very good :-) to display non XPM files :-) You can see XPM2PHP too, in www.cweiske.de :-) the script of our friend Christian :-) See you! ----- Original Message ----- From: "nathan" <nathan@the-ring.homelinux.net> To: <php-gtk-general@lists.php.net> Sent: Wednesday, January 21, 2004 3:31 PM Subject: Re: [PHP-GTK] displaying non xpm image using gd > after a brief discuss about licence with steph here's the same code > under LGPL > > regards, > nathan. > > how to use it: > <? > $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 > $pix = Gdk::pixmap_create_from_xpm_d($gdkwindow, null, $xpm_d); > $pixmapwid = &new GtkPixmap($pix[0], $pix[1]); > //and then do what you want and be happy > ?> > > so here's my code hope it'll > <?php > /** > * function set for image manipulation > * @author Jonathan Gotti <nathan@the-ring.homelinux.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 please mail me i'll be very very > happy > > best regards > > malko > > -- > PHP-GTK General Mailing List (http://gtk.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > > -- > PHP-GTK General Mailing List (http://gtk.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php >

« previous php.gtk.general (#4363) next »