Results 1 to 3 of 3

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Åêñïåðèìåíòàòîð Nuclear's Avatar
    Join Date: Sep:2004
    Location: Ñîôèÿ, êâ. Ãå(ò)î Ìèëåâ
    Posts: 3,352

    PHP convert text to image, ëîøî êà÷åñòâî.

    Çäðàâåéòå.
    Ðåøèõ äà íàïðàâÿ user friendly ñèñòåìêà, êîÿòî ïðåîáðàçóâà çàäàäåí òåêñò â êàðòèíêà ñ ïîìîùòà íà php.
    Èçïîëçâàì ôóíêöèÿòà imageTTFText(). Âçåõ åäíî ñêðèïò÷å è ñå ïîëó÷è ñëåäíîòî:
    Code:
    <?
    /*
    text to image converter
    daftlogic 
    www.daftlogic.com
    */
    Header("Content-type: image/png");
    class textPNG 
    {
    	var $font = '../equation/fonts/verdana.ttf'; //default font. directory relative to script directory.
    	var $msg = "no text to display"; // default text to display.
    	var $size = 10; // default font size.
    	var $rot = 0; // rotation in degrees.
    	var $pad = 0; // padding.
    	var $transparent = 1; // transparency set to on.
    	var $red = 0; // black text...
    	var $grn = 0;
    	var $blu = 0;
    	var $bg_red = 200; // on white background.
    	var $bg_grn = 200;
    	var $bg_blu = 200;
    	
    	function draw() 
    	{
    		$width = 0;
    		$height = 0;
    		$offset_x = 0;
    		$offset_y = 0;
    		$bounds = array();
    		$image = "";
    	
    		// get the font height.
    		$bounds = ImageTTFBBox($this->size, $this->rot, $this->font, "W");
    		if ($this->rot < 0) 
    		{
    			$font_height = abs($bounds[7]-$bounds[1]);		
    		} 
    		else if ($this->rot > 0) 
    		{
    		$font_height = abs($bounds[1]-$bounds[7]);
    		} 
    		else 
    		{
    			$font_height = abs($bounds[7]-$bounds[1]);
    		}
    		// determine bounding box.
    		$bounds = ImageTTFBBox($this->size, $this->rot, $this->font, $this->msg);
    		if ($this->rot < 0) 
    		{
    			$width = abs($bounds[4]-$bounds[0]);
    			$height = abs($bounds[3]-$bounds[7]);
    			$offset_y = $font_height;
    			$offset_x = 0;
    		} 
    		else if ($this->rot > 0) 
    		{
    			$width = abs($bounds[2]-$bounds[6]);
    			$height = abs($bounds[1]-$bounds[5]);
    			$offset_y = abs($bounds[7]-$bounds[5])+$font_height;
    			$offset_x = abs($bounds[0]-$bounds[6]);
    		} 
    		else
    		{
    			$width = abs($bounds[4]-$bounds[6]);
    			$height = abs($bounds[7]-$bounds[1]);
    			$offset_y = $font_height;;
    			$offset_x = 0;
    		}
    		
    		$image = imagecreate($width+($this->pad*2)+1,$height+($this->pad*2)+1);
    		$background = ImageColorAllocate($image, $this->bg_red, $this->bg_grn, $this->bg_blu);
    		$foreground = ImageColorAllocate($image, $this->red, $this->grn, $this->blu);
    	
    		if ($this->transparent) ImageColorTransparent($image, $background);
    		ImageInterlace($image, false);
    	
    		// render the image
    		ImageTTFText($image, $this->size, $this->rot, $offset_x+$this->pad, $offset_y+$this->pad, -$foreground, $this->font, $this->msg);
    	
    		// output PNG object.
    		imagePNG($image);
    		}
    	}
    
    	$text = new textPNG;
    
    	if (isset($_GET['text'])) $msg=$_GET['text'];
    	if (isset($msg)) $text->msg = $msg; // text to display
    	if (isset($font)) $text->font = $font; // font to use (include directory if needed).
    	if (isset($size)) $text->size = $size; // size in points
    	if (isset($rot)) $text->rot = $rot; // rotation
    	if (isset($pad)) $text->pad = $pad; // padding in pixels around text.
    	if (isset($red)) $text->red = $red; // text color
    	if (isset($grn)) $text->grn = $grn; // ..
    	if (isset($blu)) $text->blu = $blu; // ..
    	if (isset($bg_red)) $text->bg_red = $bg_red; // background color.
    	if (isset($bg_grn)) $text->bg_grn = $bg_grn; // ..
    	if (isset($bg_blu)) $text->bg_blu = $bg_blu; // ..
    	if (isset($tr)) $text->transparent = $tr; // transparency flag (boolean).
    
    	$text->draw(); // GO!!!!!
    ?>
    Ñêðèò/Hidden: êîä


    Êàêòî ñå âèæäà, èçïîëçâàì øðèôò verdana, ðàçìåð 10 (àêî íå ñå ëúæà, ìíåíèÿòà âúâ ôîðóìà èçëèçàò ñ íåãî - ïîëçâàì ãî çà ñðàâíåíèå). Âçåë ñúì verdana.ttf îò ïàïêàòà Fonts íà Windows XP.
    Îáà÷å êà÷åñòâîòî õè÷ íå ìå óñòðîéâà. Íÿêîè ÷åðòè÷êè îò áóêâèòå ñå ãóáÿò, íà íÿêîè ìåñòà òå ñà ïî-òåñíè, íà íÿêîè - ïî-äåáåëè, ðàçñòîÿíèÿòà ì/ó áóêâèòå ñà ïî-ìàëêè íà íÿêîè ìåñòà è ïî-ãîëåìè íà äðóãè.
    Eòî ïðèìåð:


    Íà ïúðâèÿò ðåä òåêñòúò å ãåíåðèðàí îò ôîðóìà.
    Íà âòîðèÿ ðåä èìàìå êàðòèíêà, ãåíåðèðàíà îò ãîðíèÿ ñêðèïò. Ëè÷è ñè, ÷å å ïî-çëå. Ïðåäïîëàãàì, ÷å íèêúäå â ñêðèïòà íå å ïðèëàãàí resize, êîéòî áè äîâåë äî âëîøàâàíå íà êà÷åñòâîòî. Ïðîáâàõ ñ äðóãè øðèôòîâå, ïî-ãîëÿì ðàçìåð, íî ïàê èìà íåðàâíîìåðíîñòè.

    http://nuclear868.net/texttoimage/im...e%20lazy%20dog - òîâà å ñàìèÿò ñêðèïò, ìîæå äà ñå âèäè êàêâî ñå ïîëó÷àâà. (ñëåä text= å òåêñòúò, êîéòî ùå ñå âêàðà â êàðòèíêàòà).
    Çàùî ñå ïîëó÷àâà òîâà?
     êðàåí ñëó÷àé áèõ ïîëçâàë è äðóãà ôóíêöèÿ êîÿòî äàâà íîðìàëíî êà÷åñòâî, ñòèãà äà âðúùà êàòî ðåçóëòàò íàïðàâî png êàðòèíêà, êàêòî ïðàâè òîçè (äà ìîãà äà ñëîæà ãîðíèÿ àäðåñ â IMG òàã, âñå åäíî ñúì äàë ëèíê êúì êàðòèíêà).
    Íå îòãîâàðÿì íà íåãðàìîòíè ïîñòîâå. Äà èçòèêàìå HWBG ïàïëà÷òà!

  2. #2
    Registered User
    Join Date: Aug:2006
    Location: Sofia
    Posts: 37
    Íå ñúì 100% ñèãóðåí, íî ìèñëÿ ÷å òè ñå ïîëó÷àâà òàêà çàðàäè òîâà, ÷å ïîëçâàø imagecreate, à íå imagecreatetruecolor. Ïðîáâàé ñ òîçè ïîäõîä:

    PHP Code:
    <?php
    header
    ('Content-type: image/png');
    $im imagecreatetruecolor(40030);
    $white imagecolorallocate($im255255255);
    $black imagecolorallocate($im000);
    imagefilledrectangle($im0039929$white);
    $text 'Testing...';
    $font './verdana.ttf';
    imagettftext($im1001020$black$font$text);
    imagepng($im);
    imagedestroy($im);
    Ïîãëåäíè è â äîêóìåíòàöèÿòà íà ôóíêöèèòå - èìà èíòåðåñíè ïîÿñíåíèÿ îò ïîòðåáèòåëèòå.

  3. #3
    Åêñïåðèìåíòàòîð Nuclear's Avatar
    Join Date: Sep:2004
    Location: Ñîôèÿ, êâ. Ãå(ò)î Ìèëåâ
    Posts: 3,352
    Áëàãîäàðÿ, ïîëó÷è ñå.
    Äàæå ãî íàïðàâèõ òàêà, ÷å êàðòèíêàòà äà å ñ ïðîçðà÷åí ôîí.
    Íå îòãîâàðÿì íà íåãðàìîòíè ïîñòîâå. Äà èçòèêàìå HWBG ïàïëà÷òà!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

Copyright © 1999-2011 Õàðäóåð ÁÃ. Âúçìîæíî å ñúäúðæàíèåòî íà òàçè ñòðàíèöà äà å îáåêò íà àâòîðñêè ïðàâà.
iskamPC.com | mobility.BG | Bloody's Techblog | Êðèïòîâàëóòè è ìàéíèíã | 3D Vision Blog | Ìàãàçèí çà åëåêòðîííè öèãàðè