PHP GD generating image with an extra 20(hex) in the file -
using php gd library generate png image, seems working fine, no errors. browser cannot render image.
i downloaded generated png , compare normal pngs in ultraedit, found there 20 (in hex mode) in beginning of file. after remove 20 ultraedit, png works fine.
i've check through code, , cannot find line gives 0x20.
relative part of code follow:
public function imggen($string = "enter own lyric.", $size = 35, $lineheight = 50, $font = "./fonts/w6.ttf", $meta = "project gy picture generation", $metasize = 10, $metalineh = 25, $bg_src = "./img/bg/bg1.png", $textcolor = "w", $width=640, $height=596, $x_offset = 30, $y_offset = 30, $position="cc", $bgpos = 5){ $logo_src ='./img/bg/wtm-'.$position[1].'-'.$textcolor.'.png'; $logo = imagecreatefrompng($logo_src); $bg = imagecreatefrompng($bg_src); $bgcrop = imagecreatetruecolor($width,$height); $bgw = imagesx($bg); $bgh = imagesy($bg); if ($bgpos == 1||$bgpos == 2||$bgpos == 3){$src_y = 0;} elseif ($bgpos == 4||$bgpos == 5||$bgpos == 6){$src_y = ($bgh - $height)/2;} else{$src_y = $bgh - $height;} if ($bgpos == 1||$bgpos == 4||$bgpos == 7){$src_x = 0;} elseif ($bgpos == 2||$bgpos == 5||$bgpos == 8){$src_x = ($bgw - $width)/2;} else{$src_x = $bgw - $width;} imagecopyresized($bgcrop,$bg,0,0,$src_x,$src_y,$width,$height,$width,$height); imagedestroy($bg); $logow = imagesx($logo); $logoh = imagesy($logo); $strings = explode("\n", $string); $color = ($textcolor == "b") ? imagecolorallocate($bgcrop, 0, 0, 0) : imagecolorallocate($bgcrop, 255, 255, 255); if ($position[1]=="l"){$anchorx = $x_offset;}//horiz. left elseif($position[1]=="c"){$anchorx = $width / 2;} //horiz. center elseif($position[1]=="r"){$anchorx = $width - $x_offset;} //horiz right if ($position[0]=="t"){$anchory = $y_offset;} //vert. top elseif($position[0]=="c"){$anchory = ($height / 2)-(($lineheight * count($strings) + $metalineh + 20+$logoh)/2);} //vert. center elseif($position[0]=="b"){$anchory = $height - ($lineheight * count($strings) + $metalineh + 20 + $logoh + $y_offset);} //vert. bottom $lineacuu = $anchory + $lineheight; //imageline($bgcrop,0,($lineacuu),300,($lineacuu),$color); foreach($strings $line){ if ($position[1]=="l"){imagettftext($bgcrop, $size, 0, ($anchorx), ($lineacuu), $color, $font, $line);}//horiz. left elseif($position[1]=="c"){$this->imagettftext_ca($bgcrop, $size, 0, ($anchorx), ($lineacuu), $color, $font, $line);} //horiz. center elseif($position[1]=="r"){$this->imagettftext_ra($bgcrop, $size, 0, ($anchorx), ($lineacuu), $color, $font, $line);} //horiz right $lineacuu += $lineheight; } $lineacuu += $metalineh - $lineheight; if ($position[1]=="l"){imagettftext($bgcrop, $metasize, 0, ($anchorx), ($lineacuu), $color, $font, $meta);}//horiz. left elseif($position[1]=="c"){$this->imagettftext_ca($bgcrop, $metasize, 0, ($anchorx), ($lineacuu), $color, $font, $meta);} //horiz. center elseif($position[1]=="r"){$this->imagettftext_ra($bgcrop, $metasize, 0, ($anchorx), ($lineacuu), $color, $font, $meta);} imageline($bgcrop,0,($lineacuu+10),1500,($lineacuu+10),$color); imagealphablending($bgcrop, true); if ($position[1]=="l"){$logox = $x_offset;}//horiz. left elseif($position[1]=="c"){$logox = $width / 2 - $logow/2;} //horiz. center elseif($position[1]=="r"){$logox = $width - $x_offset - $logow;} imagecopy($bgcrop, $logo, ($logox), ($lineacuu + 20), 0, 0, $logow, $logoh); imagepng($bgcrop); } public function imggen_db($img_id) { $fname = $this->config->item('fname'); $fpath = $this->config->item('fpath'); $post = $this->get_by_id($img_id); $bgpath = "./img/bg/bg".$post->background.".png"; $this->imggen($post->lyric, $post->size, $post->lineheight, str_replace($fname,$fpath,$post->font), $post->meta, $post->metasize, $post->metalineh, $bgpath, $post->textcolor, $post->width, $post->height, $post->x_offset, $post->y_offset, $post->style, $post->bgpos); }
full code available @ https://github.com/1a23/project-gy
this code works in codeigniter framework
./application/config/version.php has space before <?php
Comments
Post a Comment