Convert text to image?

后端 未结 5 1574
旧巷少年郎
旧巷少年郎 2020-12-08 17:58

I have seen in a couple of cases when sites generate image based on text/data input. How that can be achieved with PHP?

相关标签:
5条回答
  • 2020-12-08 18:19

    The PHP GD extension allows text to be overlaid on an image.

    In fact, you don't need an image in the first place, you can generate an image containing just the text.

    I've used it for buttons.

    0 讨论(0)
  • 2020-12-08 18:34

    Another option: try imagick

    0 讨论(0)
  • 2020-12-08 18:39

    Using gd or other such libraries (or libraries built on top of gd).

    0 讨论(0)
  • 2020-12-08 18:41

    Working Solution:

    ( at first, you need to ensure, that the hosting has enabled GD library: in any php file, execute phpinfo(); and in output check if GD library is enabled) .

    Solution 1 (auto-sized output):

    TextToImage_my( $text='Helloooo! my unicode words:  ǩ Ƥ Ў  ض ط  Ⴓ ');
        // ==== other parameters can be used too, see the function arguments below
    

    function code: text-to-image.php


    Solution 2 (manual-sized output):

    (needs to have manual width&height of output, longer strings are cut out)..

    <?php
    $text = "YOUR  texttttttttttttttt";
    
    $my_img = imagecreate( 200, 80 );                             //width & height
    $background  = imagecolorallocate( $my_img, 0,   0,   255 );
    $text_colour = imagecolorallocate( $my_img, 255, 255, 0 );
    $line_colour = imagecolorallocate( $my_img, 128, 255, 0 );
    imagestring( $my_img, 4, 30, 25, $text, $text_colour );
    imagesetthickness ( $my_img, 5 );
    imageline( $my_img, 30, 45, 165, 45, $line_colour );
    
    header( "Content-type: image/png" );
    imagepng( $my_img );
    imagecolordeallocate( $line_color );
    imagecolordeallocate( $text_color );
    imagecolordeallocate( $background );
    imagedestroy( $my_img );
    ?> 
    
    0 讨论(0)
  • 2020-12-08 18:46

    I believe libGD is one of the most popular alternatives for generating images (and it has bindings for most languages used in web development).

    See the documentation on PHP.net. I guess you are especially interested in imagettftext.

    0 讨论(0)
提交回复
热议问题