Part of the EllisLab Network
   
 
render text as image
Posted: 09 June 2008 09:20 AM   [ Ignore ]  
Grad Student
Rank
Total Posts:  76
Joined  06-03-2008

is there a convenient way to render text as image (antialiasing) in CI?
i found no helper/lib/whatever for this purpose…

Profile
 
 
Posted: 09 June 2008 09:50 AM   [ Ignore ]   [ # 1 ]  
Sr. Research Associate
RankRankRankRankRank
Total Posts:  4785
Joined  07-14-2006

a quick google got me TextToImage function. Check the image library your are working with for more function.

It’s best you cache the images instead of rendering it on every page request.

Profile
 
 
Posted: 09 June 2008 02:36 PM   [ Ignore ]   [ # 2 ]  
Grad Student
Rank
Total Posts:  76
Joined  06-03-2008
xwero - 09 June 2008 01:50 PM

a quick google got me TextToImage function. Check the image library your are working with for more function.

It’s best you cache the images instead of rendering it on every page request.

yeah caching is exactly the point. would be great if there would be a library for generating cached text-images out of a config array.
TYPO3 can do this out of the box, wondering.., its a feature which is quite often required IMO…

Profile
 
 
Posted: 09 June 2008 02:49 PM   [ Ignore ]   [ # 3 ]  
Sr. Research Associate
RankRankRankRankRank
Total Posts:  4785
Joined  07-14-2006

Instead of caching you can create static images when you change or add the text. Caching is good but static is better.

Profile
 
 
Posted: 09 June 2008 05:56 PM   [ Ignore ]   [ # 4 ]  
Grad Student
Rank
Total Posts:  76
Joined  06-03-2008
xwero - 09 June 2008 06:49 PM

Instead of caching you can create static images when you change or add the text. Caching is good but static is better.

where is the difference between caching and static images?

Profile
 
 
Posted: 09 June 2008 06:16 PM   [ Ignore ]   [ # 5 ]  
Sr. Research Associate
RankRankRankRankRank
Total Posts:  4785
Joined  07-14-2006

Caching works with time intervals, most of the time, which renders the images without need. A static image created when the text is added or changed only changes when it’s necessary.

Profile
 
 
Posted: 09 June 2008 06:21 PM   [ Ignore ]   [ # 6 ]  
Grad Student
Rank
Total Posts:  76
Joined  06-03-2008
xwero - 09 June 2008 10:16 PM

Caching works with time intervals, most of the time, which renders the images without need. A static image created when the text is added or changed only changes when it’s necessary.

i think i would need another caching algorithm in both cases: i think it should be able to check if there has been any change in one of the parameters (font, fontsize,..). cause i dont want to delete all files manually if i have to change a rendering-property.
so my aproach would be to create a checksum over the configuration array and use this checksum as filename…

Profile
 
 
Posted: 09 June 2008 06:25 PM   [ Ignore ]   [ # 7 ]  
Sr. Research Associate
RankRankRankRankRank
Total Posts:  4785
Joined  07-14-2006

It looks like a well thought out approach

Profile
 
 
Posted: 24 June 2008 11:25 AM   [ Ignore ]   [ # 8 ]  
Grad Student
Rank
Total Posts:  76
Joined  06-03-2008

So, finally heres my text2image_helper.php, hope you like i:

<?php

/**
*    Text To Image Function
*
*    PHP versions 4 and 5
*
*    @author     Stephan Petzl <spetzl@gmx.at>
*    @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 $data[6];
        
$y $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.052*$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']00$conf['font-size']$textcolor$conf['font-file']$text);
        
        
imagegif($res$imagepath);
    
}
    
return '<img src="'.$imagepath.'" border="0" alt="'.$text.'" '.$conf['params'].'/>';
    
}
?> 
Profile
 
 
Posted: 24 September 2011 01:51 PM   [ Ignore ]   [ # 9 ]  
Summer Student
Total Posts:  20
Joined  09-21-2011
stoefln - 24 June 2008 11:25 AM

So, finally heres my text2image_helper.php, hope you like i:

<?php

/**
*    Text To Image Function
*
*    PHP versions 4 and 5
*
*    @author     Stephan Petzl <spetzl@gmx.at>
*    @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 $data[6];
        
$y $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.052*$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']00$conf['font-size']$textcolor$conf['font-file']$text);
        
        
imagegif($res$imagepath);
    
}
    
return '<img src="'.$imagepath.'" border="0" alt="'.$text.'"/>';
    
}
?> 

Nice Work but how to use it as a library with codigniter

 Signature 

DNS Call

Profile
 
 
Posted: 24 September 2011 04:18 PM   [ Ignore ]   [ # 10 ]  
Summer Student
Total Posts:  20
Joined  09-21-2011

Thanks

I don’t saw that was a helper

it’s great work many thanks

 Signature 

DNS Call

Profile