I use this script to make html code into image using php-gd library:

<?php
    //put your html code here 
    $html_code = ' 
    <html> 
        <body> 
            Image Out Put sadadasdasdasdasdasd
        </body> 
    </html>
    '; 

    $img = imagecreate("300", "600"); 
    imagecolorallocate($img,0,0,0); 
    $c2 = imagecolorallocate($img,70,70,70); 
    imageline($img,0,0,300,600,$c2); 
    imageline($img,300,0,0,600,$c2); 

    $white = imagecolorallocate($img, 255, 255, 255); 
    imagettftext($img, 9, 0, 1, 1, $white, "arial.ttf", $html_code); 

    header('Content-type: image/jpeg'); 
    imagejpeg($img); 

?>

site url:

http://almogas.com/temp/phpimage.php?id=3

I get lot of unknown characters. How can I fix this behavior?

///////////////////////////////////////////////

edit

now its working after delete the spaces

but there is way to add background image? thank you

有帮助吗?

解决方案

You have unwanted chars before <?. remove all blank lines and spaces before <? your image is otherwise correct.

<? must be the first character first line in your script. you can avoid ?> if you use it it must be the last line last chars.

其他提示

Please make sure that your file start without spaces or extra chars. It is important that it starts with

<?

You can avoid the closing

?>

So that you're sure to not send any other unwanted output to the client.

  • Make sure you have the GD library enabled on your server.
  • You should use png instead of jpg as png produces better text quality
  • You should use imagedestroy after imagejpeg or imagepng to clean up memory
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top