write text to transparent image

Discussion in 'PHP' started by thosecars82, Dec 14, 2009.

  1. #1
    Hello

    Does anybody know why this code is only working locally on xampp but not on remote server where I just see a black rectangle instead of the word "phone"?
    You can check it at http://www.arreglaordenador.com/numberimage4.php

    
    <?php
     $fontDir = $rootPath . "fonts\\";
    header("Content-type: image/png"); //Picture Format
    header("Expires: Mon, 01 Jul 2003 00:00:00 GMT"); // Past date
    header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); // Consitnuously modified
    header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
    header("Pragma: no-cache"); // NO CACHE
    
    /*image generation code*/
    //create Image of size 350px x 75px
    $bg = imagecreatetruecolor(350, 75);
    
    //This will make it transparent
    imagesavealpha($bg, true);
    
    $trans_colour = imagecolorallocatealpha($bg, 0, 0, 0, 127);
    imagefill($bg, 0, 0, $trans_colour);
    
    //Text to be written
    $helloworld = isset($_GET['text']) ? $_GET['text'] : "phone";
    
    // White text
    $white = imagecolorallocate($bg, 255, 255, 255);
    // Grey Text
    $grey = imagecolorallocate($bg, 128, 128, 128);
    // Black Text
    $black = imagecolorallocate($bg, 0,0,0);
    
    $font = 'arial.ttf'; //path to font you want to use
    $fontsize = 20; //size of font
    
    //Writes text to the image using fonts using FreeType 2
    imagettftext($bg, $fontsize, 0, 20, 20, $grey, $fontDir.$font, $helloworld);
    
    //Create image
    imagepng($bg);
    
    //destroy image
    ImageDestroy($bg);
    ?>
    
    
    Code (markup):
    Thank you very much.
     
    thosecars82, Dec 14, 2009 IP
  2. szalinski

    szalinski Peon

    Messages:
    341
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #2
    possibly the php GD extension isn't enabled on your server, but is on your localhost?
     
    szalinski, Dec 14, 2009 IP
  3. Bohra

    Bohra Prominent Member

    Messages:
    12,573
    Likes Received:
    537
    Best Answers:
    0
    Trophy Points:
    310
    #3
    put this at the top

    error_reporting(E_ALL);
    ini_set("display_errors", 1);

    it will output errors if any
     
    Bohra, Dec 15, 2009 IP
  4. thosecars82

    thosecars82 Member

    Messages:
    81
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    43
    #4
    I do not know why the transparency of this code works fine locally but not in the remote server. Well, the transparency works fine in the local server as long as you do not use internet explorer but firefox or other browser. Well I think the reason is that the sentence
    
    imagesavealpha($bg, true);
    
    Code (markup):
    might not be working in the remote server but I do not know why.

    The whole code is this:
    
    <?php
    $fontDir = "fonts/";
    /*image generation code*/
    //create Image of size 350px x 75px
    $bg = imagecreatetruecolor(350, 75);
    
    //This will make it transparent
    imagesavealpha($bg, true);
    
    $trans_colour = imagecolorallocatealpha($bg, 0, 0, 0, 127);
    imagefill($bg, 0, 0, $trans_colour);
    
    //Text to be written
    $helloworld = isset($_GET['text']) ? $_GET['text'] : "phone";
    
    $red = imagecolorallocate($bg, 255, 0, 0);
    
    $font = 'arial.ttf'; //path to font you want to use
    $fontsize = 20; //size of font
    
    //Writes text to the image using fonts using FreeType 2
    imagettftext($bg, $fontsize, 0, 20, 20, $red, $fontDir.$font, $helloworld);
    header("Content-type: image/png"); //Picture Format
    //Create image
    imagepng($bg);
    
    //destroy image
    ImageDestroy($bg);
    ?>
    
    Code (markup):
    Nonetheless, now I have an alternative and different code which does successfully what I wanted not only locally but also in the remote server and seems to work with all the browsers even with internet explorer. The code comes here:
    
    <?php
     // Create a 55x30 image
     $im = imagecreate(105, 20);
      $transp = imagecolorallocatealpha($im, 0, 0, 0, 127);
     $textColor = imagecolorallocate($im, 0, 0, 0);
     imagestring($im, 4, 0, 2,"phone", $textColor);// Save the image
      header("Content-type: image/png");
     imagepng($im);
     imagedestroy($im);
     ?>  
    
    Code (markup):
    Cheers!
     
    thosecars82, Dec 15, 2009 IP
  5. HuggyStudios

    HuggyStudios Well-Known Member

    Messages:
    724
    Likes Received:
    20
    Best Answers:
    26
    Trophy Points:
    165
    #5
    It´s most likely because the libary extension is setup different, have a look in your php info and see if that particular function has been enabled.

    Glen
     
    HuggyStudios, Dec 15, 2009 IP
  6. thosecars82

    thosecars82 Member

    Messages:
    81
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    43
    #6
    Hello
    Question, how do I know if I have that particular function enabled just by looking at the phpinfo() information. I just took a glance at it and it said this:

    Configure Command './configure' '--prefix=/usr/local/php5' '--with-mysql=/usr' '--with-gd' '--with-gzip' '--with-zlib' '--with-kerberos' '--enable-ftp' '--enable-gd-native-ttf' '--with-freetype-dir=/usr' '--with-jpeg-dir=/usr' '--with-xpm-dir=/usr' '--with-png-dir=/usr' '--with-imagick' '--enable-bcmath' '--with-xml' '--with-dom' '--with-gettext' '--enable-mbstring' '--with-curl' '--with-imap' '--enable-calendar' '--enable-mhash' '--enable-openssl' '--with-openssl=/usr' '--enable-exif' '--with-mhash=/usr' '--enable-mcrypt' '--with-mcrypt=/usr' '--with-imap-ssl' '--enable-xslt' '--with-dom-xslt=/usr' '--with-xslt-sablot=/usr' '--with-config-file-path=/home/sys/php5' '--with-pgsql=/usr' '--with-pdo-mysql' '--with-pdo-pgqsl' '--with-mysqli' '--with-pear' '--with-iconv' '--with-xsl=/usr' '--enable-fastcgi'

    GD Support enabled
    GD Version bundled (2.0.34 compatible)
    FreeType Support enabled
    FreeType Linkage with freetype
    FreeType Version 2.1.7
    GIF Read Support enabled
    GIF Create Support enabled
    JPG Support enabled
    PNG Support enabled
    WBMP Support enabled
    XPM Support enabled
    XBM Support enabled

    Is there any problem with that? Should I look at other part of the info from the phpinfo()?
    Thanks
     
    thosecars82, Dec 15, 2009 IP
  7. thosecars82

    thosecars82 Member

    Messages:
    81
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    43
    #7
    thosecars82, Dec 15, 2009 IP
  8. HuggyStudios

    HuggyStudios Well-Known Member

    Messages:
    724
    Likes Received:
    20
    Best Answers:
    26
    Trophy Points:
    165
    #8
    Have a look at this,

    php.net/manual/en/function.function-exists.php

    Check if the function is actually enabled on your system.

    Glen
     
    HuggyStudios, Dec 15, 2009 IP
  9. thosecars82

    thosecars82 Member

    Messages:
    81
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    43
    #9
    It seems it is actually enabled. Actually you can check it.
    I wrote this
    
    <?php
    if (function_exists('imagesavealpha')) {
        echo "IMAP functions are available.<br />\n";
    } else {
        echo "IMAP functions are not available.<br />\n";
    }
    ?> 
    
    
    Code (markup):
    in http://www.arreglaordenador.com/functionexists.php
    and it returns
    IMAP functions are available.
     
    thosecars82, Dec 15, 2009 IP
  10. shiva9675

    shiva9675 Peon

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    the issue in IE may be due to IE6 not supporting png images.. also for imagettf you need to have the fonts in the right place on the server too.. do you have the font in place ?
     
    shiva9675, Dec 15, 2009 IP
  11. thosecars82

    thosecars82 Member

    Messages:
    81
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    43
    #11
    With the first code I placed, yes the problem might be around not getting access to the fonts. But witth the second, that was not the problem.

    I do not know why because I did not change anything but the code
    
    <?php
    $fontDir = "fonts/";
    /*image generation code*/
    //create Image of size 350px x 75px
    $bg = imagecreatetruecolor(350, 75);
    
    //This will make it transparent
    imagesavealpha($bg, true);
    
    $trans_colour = imagecolorallocatealpha($bg, 0, 0, 0, 127);
    imagefill($bg, 0, 0, $trans_colour);
    
    //Text to be written
    $helloworld = isset($_GET['text']) ? $_GET['text'] : "phone";
    
    $red = imagecolorallocate($bg, 255, 0, 0);
    
    $font = 'arial.ttf'; //path to font you want to use
    $fontsize = 20; //size of font
    
    //Writes text to the image using fonts using FreeType 2
    imagettftext($bg, $fontsize, 0, 20, 20, $red, $fontDir.$font, $helloworld);
    header("Content-type: image/png"); //Picture Format
    //Create image
    imagepng($bg);
    
    //destroy image
    ImageDestroy($bg);
    ?>
    
    Code (markup):
    seems to be working now in the remote server
    www.arreglaordenador.com/numberimage4.php
    I mean, it is not showing a black rectangle anymore because transparency is working.

    To your answer, the font is there. Anyways, I think the problem was that for some reason transparency was not working well in the server, that is to say, the function imagesavealpha($bg, true); was not doing what was supposed to do. Otherwise I would not have seen a black rectangle when I run the code in the server. Actually, when I run the code in local server then I did not see that black rectangle because this sentence imagesavealpha($bg, true); made it transparent. But now it seems it is also working in the remote server.
    Thanks though.
     
    thosecars82, Dec 16, 2009 IP