Welcome Guest, Not a member yet? Register   Sign In
render text as image
#9

[eluser]stoefln[/eluser]
So, finally heres my text2image_helper.php, hope you like i:

Code:
<?php

/**
*    Text To Image Function
*
*    PHP versions 4 and 5
*
*    @author     Stephan Petzl <[email protected]>
*    @copyright  2005 &copy; Stephan Petzl
*    @license    non, just use the file as you like, but please dont throw this sentence away
*    @link       http://ra.synapsick.net
*/

/**
*    Text to image Function
*
*    Convert text to a image
*
*    @param string $text Text to be converted
*    @param string $configArray Optional configuration array allows you to set: 'background-color', 'color', 'font-size', 'font-file', 'params'
*    @param int $filepath Directory where generated images are stored
*    @return void
*/
function text2image($text,$configArray=array(),$filepath='public/images/generated')
{
    if(trim($text) == "")
        return "";
    $conf = array();
    $conf['background-color'] = isset($configArray['background-color']) ? $configArray['background-color'] : '#FFFFFF';
    $conf['color']            = isset($configArray['color'])            ? $configArray['color'] : '#404040';
    $conf['font-size']        = isset($configArray['font-size'])        ? $configArray['font-size'] : '19';
    $conf['font-file']        = isset($configArray['font-file'])        ? $configArray['font-file'] : APPPATH.'fonts/HelveticaNeue-Condensed.otf';
    $conf['params']           = isset($configArray['params'])           ? $configArray['params'] : '';
    
    // calculate a hash out of the configuration array-> image is only generated if its not found in the filepath
    $str = $text;
    foreach($conf as $key => $val){
        $str .= $key."=".$val;
    }
    $hash = md5($str);
    $imagepath = $filepath.'/'.$hash.'.gif';
    if(!file_exists($imagepath)){
        $data = imagettfbbox($conf['font-size'], 0, $conf['font-file'], $text);
        $x = 0 - $data[6];
        $y = 0 - $data[7]-$data[3];
        //print_r($data);
        
        $y *= 1.1;  //dunno why - but without this line the area will be a bit too small in hight
        //echo $y;
        $res = imagecreate($data[2]*1.05, 2*$data[3] + $y);
        $r = hexdec(substr($conf['background-color'],1,2));
        $g = hexdec(substr($conf['background-color'],3,2));
        $b = hexdec(substr($conf['background-color'],5,2));
        $backgroundcolor = imagecolorallocate($res,$r,$g, $b);
        $r = hexdec(substr($conf['color'],1,2));
        $g = hexdec(substr($conf['color'],3,2));
        $b = hexdec(substr($conf['color'],5,2));
        
        $textcolor = imagecolorallocate($res,$r, $g, $b);
        imagettftext($res, $conf['font-size'], 0, 0, $conf['font-size'], $textcolor, $conf['font-file'], $text);
        
        imagegif($res, $imagepath);
    }
    return '<img src="'.$imagepath.'" border="0" alt="'.$text.'" '.$conf['params'].'/>';
    
}
?&gt;


Messages In This Thread
render text as image - by El Forum - 06-09-2008, 07:20 AM
render text as image - by El Forum - 06-09-2008, 07:50 AM
render text as image - by El Forum - 06-09-2008, 12:36 PM
render text as image - by El Forum - 06-09-2008, 12:49 PM
render text as image - by El Forum - 06-09-2008, 03:56 PM
render text as image - by El Forum - 06-09-2008, 04:16 PM
render text as image - by El Forum - 06-09-2008, 04:21 PM
render text as image - by El Forum - 06-09-2008, 04:25 PM
render text as image - by El Forum - 06-24-2008, 09:25 AM
render text as image - by El Forum - 06-24-2012, 09:51 PM



Theme © iAndrew 2016 - Forum software by © MyBB