Gd Library

Discussion in 'PHP' started by Kakish, Jun 27, 2009.

  1. #1
    Greetings,

    I need the code for gd library text on images plz, such as:

    http://www.imagegenerator.net/create/bush_door/

    :p
    thanks :D
     
    Kakish, Jun 27, 2009 IP
  2. Louis11

    Louis11 Active Member

    Messages:
    783
    Likes Received:
    26
    Best Answers:
    0
    Trophy Points:
    70
    #2
    If you haven't already, read the documentation for GD. It provides a ton of useful information about how you would accomplish this:

    http://us.php.net/manual/en/book.image.php

    One of the functions you want to take a look at is imagefttext(), which basically allows you to put text on an image.

    http://us.php.net/manual/en/function.imagefttext.php

    Basically, what you'll want to do is use one of the imagecreate functions to grab your background image, and then use imagefttext() to place your desired text over the image.

    Otherwise, if you just want someone to provide you the script, here you go:
    
    <?php
    $image = ImageCreateFromPNG("base.png");
    $color = imagecolorallocate($image, 0xFF, 0xFF, 0xFF);
    $colorShadow = imagecolorallocate($image, 0x66, 0x66, 0x66);
    $font = 'arial.ttf';
    $fontSize = "10";
    $fontRotation = "0";
    $str = "YOUR TEXT!!!!!";
    
    /* Shadow */
    ImageTTFText($image, $fontSize, $fontRotation, 7, 22, $colorShadow, $font, $str);
    
    /* Top Level */
    ImageTTFText($image, $fontSize, $fontRotation, 5, 20, $color, $font, $str);
    
    header("Content-Type: image/PNG");
    ImagePng ($image);
    imagedestroy($image);
    ?>
    
    PHP:
    Just as a side note, I didn't write that code. Found it on this site:
    http://scriptplayground.com/tutorials/php/Text-On-Image/

    Google is your friend ;)
     
    Louis11, Jun 28, 2009 IP
  3. Kakish

    Kakish Peon

    Messages:
    19
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    i did that and had a bunch of errors:


    Warning: imagecreatefrompng() [function.imagecreatefrompng]: 'base.png' is not a valid PNG file in /home/www/kakish.agilityhoster.com/english/gd.php on line 2

    Warning: imagecolorallocate(): supplied argument is not a valid Image resource in /home/www/kakish.agilityhoster.com/english/gd.php on line 3

    Warning: imagecolorallocate(): supplied argument is not a valid Image resource in /home/www/kakish.agilityhoster.com/english/gd.php on line 4

    Warning: imagettftext() expects parameter 1 to be resource, boolean given in /home/www/kakish.agilityhoster.com/english/gd.php on line 11

    Warning: imagettftext() expects parameter 1 to be resource, boolean given in /home/www/kakish.agilityhoster.com/english/gd.php on line 14

    Warning: Cannot modify header information - headers already sent by (output started at /home/www/kakish.agilityhoster.com/english/gd.php:2) in /home/www/kakish.agilityhoster.com/english/gd.php on line 16

    Warning: imagepng(): supplied argument is not a valid Image resource in /home/www/kakish.agilityhoster.com/english/gd.php on line 17

    Warning: imagedestroy(): supplied argument is not a valid Image resource in /home/www/kakish.agilityhoster.com/english/gd.php on line 18
     
    Kakish, Jul 1, 2009 IP
  4. AlilG

    AlilG Peon

    Messages:
    23
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    fix 2nd line first!
    other errors will be disappear!

    
    Warning: imagecreatefrompng() [function.imagecreatefrompng]: 'base.png' is not a valid PNG file in /home/www/kakish.agilityhoster.com/english/gd.php on line 2
    
    Code (markup):
    it says 'base.png' is not a valid PNG file.
    try another normal PNG image!

    if your source image is jpeg, change this function from
    imagecreatefrompng()
    to
    imagecreatefromjpeg()
     
    AlilG, Jul 1, 2009 IP
  5. vaibhavcoder

    vaibhavcoder Peon

    Messages:
    12
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Hi alilg,

    Do you have any code which will generate thumbnail on run time with a image.

    Let me know if you are having one.

    Noddy
     
    vaibhavcoder, Jul 1, 2009 IP
  6. Kakish

    Kakish Peon

    Messages:
    19
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Thanks i did that and lost some errors but i still have some:

    Warning: imagettftext() [function.imagettftext]: Could not find/open font in /home/www/kakish.agilityhoster.com/english/gd.php on line 11

    Warning: imagettftext() [function.imagettftext]: Could not find/open font in /home/www/kakish.agilityhoster.com/english/gd.php on line 14

    Warning: Cannot modify header information - headers already sent by (output started at /home/www/kakish.agilityhoster.com/english/gd.php:11) in /home/www/kakish.agilityhoster.com/english/gd.php on line 16
    ‰PNG  ��� IHDR��ô��ô���D´H��IDATxœíÚAJÛiÇá7Ö ¢mЕàªË =„[….=C{…@ÁKxDð=†MC‚F2‹€Óa6ÃTýð<«ð_½«/~:Ùl6cÅ’1Æz½¾¿¿||�ü9^¿~ýîÝ»ýýý_?N¶ã¾X,æóùóÐðâ„¢L&oÞ¼9::úûËf³ùñãÇb±Øá³�ø}GGGoß¾ÝþÞ[.—– `±X,—Ëíï½ù|¾Û×�ðR~þü¹=°ï¯×ë]?€—ñøø¸Z­÷vý�^Ã’jµcw€”‡‡‡1ƾ(yzzÊ f›ìÆ ÈY H¹w€ g€ Ã¥dÜ‚œe�‚”;@rRî�AÊ H¹w€ g€ Ã¥¤Ü‚”;@rRî�AÊ H¹)w€ Ã¥¤Ü‚”;@rRî�AÊ H¹)w€ Ã¥¤Ü‚”;@rRî�AÊ H¹)w€ ãä,¤Ü‚”;@rRî�AÊ H¹)w€ ãä,¤Ü‚”;@rRî�AÊ ȸ9�)w€ Ã¥¤Ü‚Œ;@³ @rRî�AÊ ȸ9�)w€ Ã¥¤Ü‚Œ;@³ @rRî�AÊ H¹)w€ ãä,¤Ü‚”;@rRî�AÊ H¹)w€ ãä,¤Ü‚”;@rRî�AÊ H¹)w€ Ã¥¤Ü‚”;@rRî�AÊ H¹)w€ Ã¥¤Ü‚”;@rRî�AÆ ÈY H¹)w€ Ã¥¤Ü‚”;@qr–Rî�AÊ H¹w€ g€ Ã¥¤Ü‚”;@qr–Rî�AÆ ÈY H¹w€ g€ Ã¥dÜ‚œe�‚”;@qr–Rî�AÆ ÈY H¹w€ g€ Ã¥dÜ‚œe�‚”;@qr–Rî�AÆ ȸ¹¹)w€ ãä,¤Ü‚Œ;@qrsRî�AÆ ȸ¹¹)w€ ãä,¤Ü‚Œ;@qrsRî�AÆ ȸ¹¹)w€ ãä,¤Ü‚Œ;@qrsRî�AÆ ȸ¹¹)w€ ãä,¤Ü‚Œ;@qrsRî�AÆ ÈY H¹w€ ã俤܂Œ;@³ @r2î�AÆ ÈÍ H¹w€ g€ Ã¥dÜ‚œe�‚”;@qr–Rî�AÆ ÈY H¹w€ g€ Ã¥dÜ‚œe�‚”;@qr–Rî�AÆ ÈY H¹)w€ Ã¥dÜ‚œe�‚”;@rRî�AÆ ÈY H¹w€ g€ Ã¥¤Ü‚”;@qr–Rî�AÆ ÈY H¹)w€ Ã¥dÜ‚œe�‚”;@rRî�AÆ ÈY H¹)w€ Ã¥dÜ‚œe�‚”;@rRî�AÆ ÈY H¹)w€ Ã¥¤Ü‚”;@qr–Rî�AÊ H¹)w€ Ã¥dÜ‚œe�‚”;@rRî�AÊ H¹w€ g€ Ã¥¤Ü‚”;@rRî�AÊ H¹w€ g€ Ã¥¤Ü‚”;@rRî�AÊ H¹w€ g€ Ã¥¤Ü‚”;@rRî�AÊ H¹)w€ Ã¥¤Ü‚”;@qr–Rî�AÊ H¹)w€ Ã¥¤Ü‚”;@rRî�AÊ H¹)w€ Ã¥¤Ü‚”;@rRî�AÊ H¹)w€ Ã¥¤Ü‚”;@qr–2î�AÃŽ2�AÆ ÈY H¹)w€ Ã¥¤Ü‚”;@rRî�AÊ ȸ9�)w€ Ã¥¤Ü‚”;@rRî�AÊ H¹)w€ ãä,¤Ü‚”;@r2î�AÃŽ2�AÊ H¹)w€ ãä,¤Ü‚Œ;@³ @r2î�AÃŽ2�AÊ ȸ9�)w€ ãä,¤Ü‚Œ;@³ @r2î�AÃŽ2�AÊ ȸ9�)w€ ãä,¤Ü‚Œ;@qrsRî�AÆ ÈY H¹w€ g€ Ã¥dÜ‚œe�‚”;@q2î�Anî�AÊ ȸ9�)w€ ãä,¤Ü‚Œ;@³ @r2î�AÃŽ2�AÊ ȸ9�)w€ Ã¥¤Ü‚Œ;@³ @r2î�AÃŽ2�AÊ H¹)w€ ãä,¤Ü‚”;@rRî�AÊ ȸ9�)w€ Ã¥¤Ü‚”;@rRî�AÊ H¹)w€ Ã¥¤Ü‚”;@qr–Rî�AÊ H¹)w€”ããã¡ÜbÞ¿?Å’;@ÌǏ‡³ @Ìùùùcïôôt×/àeœœœ\\\Å’1ö¦Óé®ÀËøöíÛÁÁÁãÕl6›Ïç···»~�¿åëׯ_¾|ÙþÞc\__O§ÓíØðÇ988ËœN§×××Ï_&ÏPýþýûÕÕÕÝÝÝŽÞÀÿñéÓ§›››>üãëæëõz6›}þüùììlGà?9;;»¼¼œÍfëõzó/ÿ Ðó›G-»q¤M����IEND®B`‚
     
    Kakish, Jul 1, 2009 IP
  7. AlilG

    AlilG Peon

    Messages:
    23
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    
    Warning: imagettftext() [function.imagettftext]: Could not find/open font in /home/www/kakish.agilityhoster.com/english/gd.php on line 11
    
    Code (markup):
    well it says it can not find your FONT LOCATION!
    give him the full path of your font!
    for example
    
    imagettftext($im, 20, 0, 11, 21, $grey, '/home/user/public_html/gallery/ARIAL.ttf', $text);
    
    PHP:
    full patch!
    '/home/user/public_html/gallery/ARIAL.ttf'

    not only the font name ARIAL.ttf
     
    AlilG, Jul 1, 2009 IP
  8. Kakish

    Kakish Peon

    Messages:
    19
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Thanks alot! all worked well but how can i make an extension like gd.php?text=example so that my text is editable? and how can i change text location?

    Thanks alot i really appreciate it! :)
     
    Kakish, Jul 1, 2009 IP
  9. GreenWithEnvy

    GreenWithEnvy Active Member

    Messages:
    125
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    53
    #9
    If you're going to learn PHP, you need to be analyzing scripts that you use, not simply throw them in a server and then complain about errors.

    If you read the errors you received above, you clearly did not have a valid png file as an input, nor did you have the valid font file.

    As a post above mentioned, google will solve the majority of your problems. It's not hard to google this stuff and find it.

    For example...
    google "php gd script put text on image"
    and a little down the list you find this tutorial that explains a lot and shows you exactly what you want:
    http://rmb-scripting.com/tutorials.php?tutorial&tid=164

    Hope this helps
     
    GreenWithEnvy, Jul 1, 2009 IP
  10. Kakish

    Kakish Peon

    Messages:
    19
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Well tahnks i will try them now..just to let u know I am 14 yo so i find this kinda hard! :)
     
    Kakish, Jul 1, 2009 IP
  11. Kakish

    Kakish Peon

    Messages:
    19
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #11
    i used this but it doesnt seem to work
    Error: this server connot creat this image!

    <?php

    // clean up the input

    if(empty($_GET['text'])) fatal_error('Error: No text specified.') ;



    $text = html_entity_decode($_GET['text']).' ' ;

    $text = floatval($text);

    $text = number_format($text, 1, '.', '') . '%';



    if(empty($text))

    fatal_error('Error: Text not properly formatted.') ;



    // customizable variables

    $font_file = 'ambient.ttf';

    $font_size = 23 ; // font size in pts

    $font_color = '#ffffff' ;

    $image_file = 'jump_empty.png';



    // x and y for the bottom right of the text

    // so it expands like right aligned text

    $x_finalpos = 227;

    $y_finalpos = 103;





    // trust me for now...in PNG out PNG

    $mime_type = 'image/png' ;

    $extension = '.png' ;

    $s_end_buffer_size = 4096 ;



    // check for GD support

    if(!function_exists('ImageCreate'))

    fatal_error('Error: Server does not support PHP image generation') ;



    // check font availability;

    if(!is_readable($font_file)) {

    fatal_error('Error: The server is missing the specified font.') ;

    }



    // create and measure the text

    $font_rgb = hex_to_rgb($font_color) ;

    $box = @ImageTTFBBox($font_size,0,$font_file,$text) ;



    $text_width = abs($box[2]-$box[0]);

    $text_height = abs($box[5]-$box[3]);



    $image = imagecreatefrompng($image_file);



    if(!$image || !$box)

    {

    fatal_error('Error: The server could not create this image.') ;

    }



    // allocate colors and measure final text position

    $font_color = ImageColorAllocate($image,$font_rgb['red'],$font_rgb['green'],$font_rgb['blue']) ;



    $image_width = imagesx($image);



    $put_text_x = $image_width - $text_width - ($image_width - $x_finalpos);

    $put_text_y = $y_finalpos;



    // Write the text

    imagettftext($image, $font_size, 0, $put_text_x, $put_text_y, $font_color, $font_file, $text);





    header('Content-type: ' . $mime_type) ;

    ImagePNG($image) ;



    ImageDestroy($image) ;

    exit ;





    /*

    attempt to create an image containing the error message given.

    if this works, the image is sent to the browser. if not, an error

    is logged, and passed back to the browser as a 500 code instead.

    */

    function fatal_error($message)

    {

    // send an image

    if(function_exists('ImageCreate'))

    {

    $width = ImageFontWidth(5) * strlen($message) + 10 ;

    $height = ImageFontHeight(5) + 10 ;

    if($image = ImageCreate($width,$height))

    {

    $background = ImageColorAllocate($image,255,255,255) ;

    $text_color = ImageColorAllocate($image,0,0,0) ;

    ImageString($image,5,5,5,$message,$text_color) ;

    header('Content-type: image/png') ;

    ImagePNG($image) ;

    ImageDestroy($image) ;

    exit ;

    }

    }



    // send 500 code

    header("HTTP/1.0 500 Internal Server Error") ;

    print($message) ;

    exit ;

    }





    /*

    decode an HTML hex-code into an array of R,G, and B values.

    accepts these formats: (case insensitive) #ffffff, ffffff, #fff, fff

    */

    function hex_to_rgb($hex) {

    // remove '#'

    if(substr($hex,0,1) == '#')

    $hex = substr($hex,1) ;



    // expand short form ('fff') color to long form ('ffffff')

    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)

    fatal_error('Error: Invalid color "'.$hex.'"') ;



    // convert from hexidecimal number systems

    $rgb['red'] = hexdec(substr($hex,0,2)) ;

    $rgb['green'] = hexdec(substr($hex,2,2)) ;

    $rgb['blue'] = hexdec(substr($hex,4,2)) ;



    return $rgb ;

    }

    ?>
     
    Kakish, Jul 1, 2009 IP