SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    Umm. PHP Guru....Naaaah jaswinder_rana's Avatar
    Join Date
    Jul 2004
    Location
    canada
    Posts
    3,193
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    convert text file to png and store it in folder with php

    Hi,
    what i want to do is, i have a text area, where user enteres the text, when user submit the text, the text is to be converted to png file and to save that file in a folder for later access.

    i know i can convert text into png, but dont know how to convert it and then store it.

    please help
    ---------------------------
    Errors = Improved Programming.
    My Site

  2. #2
    Keep it simple, stupid! bokehman's Avatar
    Join Date
    Jul 2005
    Posts
    1,935
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The following is fully functional and will convert text to a png file.

    PHP Code:
    <?php

    $text 
    'Cool';
    $font 'Andyb';
    $font_color '000';
    $background_color 'fff';
    $font_size '40';
    $filename 'image.png';

    if(
    text_to_PNG_file($text$font$font_color$background_color$font_size$filename)){
        print 
    'The text was saved to '.$filename.'.';
    }else{
        print 
    'There was an error saving the text';
    }







    function 
    text_to_PNG_file($text$font$font_color$background_color$font_size$filename)
    {

        function 
    get_dip($font,$size)
        {
            
    $test_chars 'abcdefghijklmnopqrstuvwxyz' .
                          
    'ABCDEFGHIJKLMNOPQRSTUVWXYZ' .
                          
    '1234567890' .
                          
    '!@#$%^&*()\'"\\/;.,`~<>[]{}-+_-=' ;
            
    $box = @ImageTTFBBox($size,0,$font,$test_chars) ;
            return 
    $box[3] ;
        }
        
        
        function 
    hex_to_rgb($hex)
        {
            
    // remove '#'
            
    if(substr($hex,0,1) == '#')
                
    $hex substr($hex,1) ;
        
            
    // expand short form ('fff') color
            
    if(strlen($hex) == 3)
            {
                
    $hex substr($hex,0,1) . substr($hex,0,1) .
                       
    substr($hex,1,1) . substr($hex,1,1) .
                       
    substr($hex,2,1) . substr($hex,2,1) ;
            }
        
            if(
    strlen($hex) != 6) return FALSE;
            
            
    // convert
            
    $rgb['red'] = hexdec(substr($hex,0,2)) ;
            
    $rgb['green'] = hexdec(substr($hex,2,2)) ;
            
    $rgb['blue'] = hexdec(substr($hex,4,2)) ;
        
            return 
    $rgb ;
        }
        
    $font_directory '';
        
    $font_file  $font_directory $font '.ttf' ;
        
    $transparent_background  true ;
        
    $mime_type 'image/png' ;
        
    $send_buffer_size 4096 ;
        
        
    // check for GD support
        
    if(!function_exists('ImageCreate')) return FALSE;
        
        
        
    // clean up text
        
    if(empty($text)) return FALSE;
        
        
        
    // check font availability
        
    if(!$font_found is_readable($font_file)) return FALSE
        
        
        
    // create image
        
    $background_rgb hex_to_rgb($background_color) ;
        
    $font_rgb hex_to_rgb($font_color);
        
    $dip get_dip($font_file,$font_size) ;
        
    $box = @ImageTTFBBox($font_size,0,$font_file,$text) ;
        
    $image = @ImageCreate(abs($box[2]-$box[0]),abs($box[5]-$dip)) ;
        if(!
    $image || !$box) return FALSE;
        
        
        
    // allocate colors and draw text
        
    $background_color = @ImageColorAllocate($image,$background_rgb['red'],
            
    $background_rgb['green'],$background_rgb['blue']) ;
        
    $font_color ImageColorAllocate($image,$font_rgb['red'],
            
    $font_rgb['green'],$font_rgb['blue']) ;   
        
    ImageTTFText($image,$font_size,0,-$box[0],abs($box[5]-$box[3])-$box[1],
            
    $font_color,$font_file,$text) ;
        
        
    // set transparency
        
    if($transparent_background)
            
    ImageColorTransparent($image,$background_color) ;
        
        
    ob_start();
        
    ImagePNG($image);
        if(
    $fp fopen($filename"w") and fwrite($fpob_get_clean()) and fclose($fp)){
            return 
    TRUE;
        }
        return 
    FALSE;
        
        
    }

    ?>
    Last edited by bokehman; Aug 15, 2005 at 08:28.

  3. #3
    Umm. PHP Guru....Naaaah jaswinder_rana's Avatar
    Join Date
    Jul 2004
    Location
    canada
    Posts
    3,193
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks, its almost working, but i again got stuck at the font directory,

    i am on shared server, so how do i tell it what font directory is it, or can i make my own directory
    ---------------------------
    Errors = Improved Programming.
    My Site

  4. #4
    Keep it simple, stupid! bokehman's Avatar
    Join Date
    Jul 2005
    Posts
    1,935
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by jaswinder_rana
    thanks, its almost working, but i again got stuck at the font directory,

    i am on shared server, so how do i tell it what font directory is it, or can i make my own directory
    Don't worry about that. Just upload the font files you need into the same directory as your script.

  5. #5
    Umm. PHP Guru....Naaaah jaswinder_rana's Avatar
    Join Date
    Jul 2004
    Location
    canada
    Posts
    3,193
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks, that worked,
    ---------------------------
    Errors = Improved Programming.
    My Site

  6. #6
    Keep it simple, stupid! bokehman's Avatar
    Join Date
    Jul 2005
    Posts
    1,935
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by jaswinder_rana
    thanks, that worked,
    Aren't you lucky!

  7. #7
    Umm. PHP Guru....Naaaah jaswinder_rana's Avatar
    Join Date
    Jul 2004
    Location
    canada
    Posts
    3,193
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i'll say, with you guys around, i have no worries now
    ---------------------------
    Errors = Improved Programming.
    My Site


Bookmarks

Posting Permissions

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