Re: displaying non xpm image using gd

From: Date: Tue, 20 Jan 2004 09:39:34 +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-4361@lists.php.net to get a copy of this message
jajajaja sthep calm!!!! the gpl license is not so bad I like GPL but I love PHP License xDDD ----- Original Message ----- From: "Steph" <steph.fox@virgin.net> To: "nathan" <nathan@the-ring.homelinux.net>; <php-gtk-general@lists.php.net> Sent: Tuesday, January 20, 2004 10:49 AM Subject: RE: [PHP-GTK] displaying non xpm image using gd > args! gpl! > > > -----Original Message----- > > From: nathan [mailto:nathan@the-ring.homelinux.net] > > Sent: 20 January 2004 09:28 > > To: php-gtk-general@lists.php.net > > Subject: [PHP-GTK] displaying non xpm image using gd > > > > > > Hi, > > it's been a long time for me trying to find a way of loading non xpm > > images in my php-gtk apps. > > as i don't want use the imagick funtion cause i don't want to force my > > users to install imagick and never succeed in getting pixbuf working, > > I try to find a way using the common GD library > > > > it's not perfect particulary with png (when truecolor) and output is 256 > > colors max, but results are really acceptable for button or little > > images for benchmark here's some test results with my amd 2200+ > > 512Mo ram : > > > > > > 128X128 png convertion time: 0,16s (command line convert 0.17s) > > 1280x1024png convertion time: 10,72s (command line convert 6,00s) > > 1280x1024jpg convertion time: 7,32s (command line convert 7,81s) > > > > 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/gpl-license.php > > GNU > > Public License > > */ > > /** > > * 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 (#4361) next »