Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

PHP and Graphics

Applying Text to Image with Transparency by Karl Blessing
Posted: 27 Apr 05

Most people know how to throw some text onto an image, but what about not only putting the text on the image, but also controlling the transparency of that image. Here is a simple code that does just that, edit the variables as needed.


Header ("Content-type: image/png");

    //The copy of the image that will act as the bottom layer
    $src_img = imagecreatefromjpeg('path to the image');
    //The copy of the image that will act as the transparent top layer
    $cpy_img = imagecreatefromjpeg('same path to the image');

    //stores the image's dimensions
    $img_width = imagesx($src_img);
    $img_height = imagesy($src_img);

    //The color of the font you wish to draw 0 to 255 for each value to make a truecolor result
    $red = 0;
    $green = 0;
    $blue = 0;
    //allocates the font color based on the RGB value
    $font_color = imagecolorallocate($src_img, $red, $green, $blue);
    //Sets the font file, this can be just about any true type font you upload to the server
    $font_file = "/home/kb244/arial.ttf";
    //Sets the font size, based on point sizes, such as 8, 10, 14, 16, so on
    $font_size = 28;
    //Sets the angle of the font
    $angle = 0;
    //finds the boundary of the text box, this part is setup with the assumption of 0 degree angle
    //an angle other than zero the next three lines may not be accurate
    $textdimension = imagettfbbox($font_size,$angle, $font_file, "Karl Blessing Photography");
    $textheight = $textdimension[3] - $textdimension[5];
    $textwidth = $textdimension[2] - $textdimension[0];

    $position = "center"; //choices are top, bottom, center
    if (strcmp($position, "top") == 0)
        $y_start = $textheight;
    elseif (strcmp($position, "bottom") == 0)
        $y_start = $new_h - ($textheight / 2);
        $y_start = ($new_h / 2) - ($textheight / 2);
    //Sets the horizontal starting position to allow for a horizontally centered text
    $x_start = ($new_w - ($line_width[2] - $line_width[0])) / 2;
    $transparency = 50; //set this reverse what you want, if you want 25% transparency on text, set this to 75
    //applies the text onto the bottom layer
    imagettftext($src_img, $font_size, $angle, $x_start, $y_start, $black, $font_file, "Karl Blessing Photography");
    //merges the top copy over the altered bottom copy causing the illusion of transparent text
    imagecopymerge( $src_img, $cpy_img, 0 , 0 , 0, 0, $new_w, $new_h, $tran);
    //Caches the image for later use so you dont hog the processor
    imagejpeg($src_img,$wm_cache.$gallery."_".$file.".jpg", 80);
    //Sends the new data back to the browser for viewing
    //Clears the images from memory

it is recomended you cache the images for later use so you dont overuse the processor from people visiting and viewing the image on number of hits. To help this, place this line right above the imagejpeg line near the bottom


imagejpeg($src_img,'path and file name to cache folder', 80);

Adjust the last number to the compression level desired ( from 0 to 100 , 100 being best quality, 0 being smallest file size ) Make sure the destination folder where you are caching the image is set to be writable, and modify the code to check for the presense of this file so that if it does exist simply send the data from it to the browser such as this method:


if ((file_exists('path to file'))
    $dst_img = imagecreatefromjpeg('path to file');

Back to PHP FAQ Index
Back to PHP Forum

My Archive

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close