Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Jun 2011
    Posts
    15
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Convert Text to Image - Close to Solving it

    Hey guys,

    I am trying to read all elements of an array and save as .png...code is working fine, but only one image at a time. I can't seem to loop through entire array.
    Any help appreciated!

    This is what I have so far:
    Code:
    <?php
    $font = 'arial.ttf';
    $fontsize = 30;
    $myarr = array("element 0", "element 1", "element 2");
    $wwmyarr = wordwrap($myarr[0],38);
    
    $dims = imagettfbbox($fontsize, 0, $font, $wwmyarr);
    
    $width = ($dims[4]+50) - $dims[6];  
    $height = ($dims[3]+50) - $dims[5]; 
    
    $image = imagecreatetruecolor($width,$height);
    
    $bgcolor = imagecolorallocate($image, 100, 100, 100);
    $fontcolor = imagecolorallocate($image, 255, 255, 255);
    
    imagefilledrectangle($image, 0, 0, $width, $height, $bgcolor);
    
    $x = 20; 
    $y = 40;
    imagettftext($image, $fontsize, 0, $x, $y, $fontcolor, $font, $wwmyarr);
    
    header('Content-type: image/png');
    
    $imagename = "image" . date("U") .".png";
    imagepng($image, $imagename, 0, PNG_NO_FILTER);
    $imagedestroy($image);
    ?>

  • #2
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts
    Quote Originally Posted by noobcoder_ View Post
    I can't seem to loop through entire array.
    Doesn't look like you are trying to loop through it. Are you trying to output the entire array on one image, or one image per item?

    You don't need to send the image header if you are just saving the image(not outputting to the browser).

  • Users who have thanked Inigoesdr for this post:

    noobcoder_ (11-15-2011)

  • #3
    New Coder
    Join Date
    Jun 2011
    Posts
    15
    Thanks
    6
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Inigoesdr View Post
    Doesn't look like you are trying to loop through it. Are you trying to output the entire array on one image, or one image per item?

    You don't need to send the image header if you are just saving the image(not outputting to the browser).
    Tried with the loop, but no luck...
    I am trying to save each element of $myarr as different image.

    Loop added, but not working again...
    Code:
    $font = 'arial.ttf';
    $fontsize = 30;
    $myarr = array("element 0", "element 1", "element 2");
    
    foreach($myarr as $png){
    $wwmyarr = wordwrap($png,38);
    
    $dims = imagettfbbox($fontsize, 0, $font, $wwmyarr);
    
    $width = ($dims[4]+50) - $dims[6];  
    $height = ($dims[3]+50) - $dims[5]; 
    
    $image = imagecreatetruecolor($width,$height);
    
    $bgcolor = imagecolorallocate($image, 100, 100, 100);
    $fontcolor = imagecolorallocate($image, 255, 255, 255);
    
    imagefilledrectangle($image, 0, 0, $width, $height, $bgcolor);
    
    $x = 20; 
    $y = 40;
    imagettftext($image, $fontsize, 0, $x, $y, $fontcolor, $font, $wwmyarr);
    
    $imagename = "image" . date("U") .".png";
    imagepng($image, $imagename, 0, PNG_NO_FILTER);
    $imagedestroy($image);
    }
    When I run the script I get only one image in folder(element 0).

  • #4
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts
    You are really close, you only have 2 small errors.
    1) The imagedestroy() line at the end shouldn't have a dollar sign in front of it as it's not a variable.
    2) Your filenames are going to collide since the date() unix timestamp is only down to the second. You need to either use microtime(true) or something that hashes(like md5()) the text(assuming it's unique) like this:
    PHP Code:
    $font 'arial.ttf';
    $fontsize 30;
    $myarr = array("element 0""element 1""element 2");

    foreach(
    $myarr as $png)
    {
        
    $wwmyarr wordwrap($png,38);
        
        
    $dims imagettfbbox($fontsize0$font$wwmyarr);
        
        
    $width = ($dims[4]+50) - $dims[6];  
        
    $height = ($dims[3]+50) - $dims[5]; 
        
        
    $image imagecreatetruecolor($width,$height);
        
        
    $bgcolor imagecolorallocate($image100100100);
        
    $fontcolor imagecolorallocate($image255255255);
        
        
    imagefilledrectangle($image00$width$height$bgcolor);
        
        
    $x 20
        
    $y 40;
        
    imagettftext($image$fontsize0$x$y$fontcolor$font$wwmyarr);
        
        
    $imagename 'image' date('U') . '_' md5($wwmyarr) . '.png';
        
    imagepng($image$imagename0PNG_NO_FILTER);
        
    imagedestroy($image);


  • Users who have thanked Inigoesdr for this post:

    noobcoder_ (11-16-2011)

  • #5
    New Coder
    Join Date
    Jun 2011
    Posts
    15
    Thanks
    6
    Thanked 0 Times in 0 Posts
    So the name was the same for each image and they were rewriting each other...

    Thanks a lot, it's working perfectly!


  •  

    Posting Permissions

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