Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Issue with font and jpg, possibly with font 1

Status
Not open for further replies.

hovercraft

Technical User
Jun 19, 2006
236
US
Greetings,

I am using faq434-5838 by kb244 titled, "Applying Text to Image with transparency".

I uploaded the file = CENTURY.TTF for my font. It's in the same directory as my .php.

I'm trying to display some text over a graphic of a t-shirt. The image displays but not the text. I'm wondering if the font actually has to be installed on the server and not just simply uploaded. The site is hosted by a hosting company so I don't know if I can install fonts on their server (or if this even makes sense).

Here is the code.
Code:
<?php

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

    //The copy of the image that will act as the bottom layer
    $src_img = imagecreatefromjpeg('imgs/teebl.jpg');
    //The copy of the image that will act as the transparent top layer
    $cpy_img = imagecreatefromjpeg('imgs/teebl.jpg');

    //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 = 255;
    $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 = "CENTURY.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);
    else
        $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 = 75; //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
    imagejpeg($src_img,"",80);
    
    //Clears the images from memory
    imagedestroy($cpy_img);
    imagedestroy($src_img);
?>

Is there a problem with the code or is it the font?

Thanks in advance,
Hovercraft
 
Fonts do not have to be installed on the system for PHP to use them. If the font file resides in the same directory on the web server as the script, PHP should be able to find it.

However, if the web server on which you are running PHP is using a case-sensitive filesystem (such as a typically-case-sensitive filesystem on a Unix-like OS versus Win32, the supported filesystems of which are all case-insensitive), then capitalization of the filename could be tripping you up.



Want the best answers? Ask the best questions! TANSTAAFL!
 
Hi

sleipnir214 said:
If the font file resides in the same directory on the web server as the script, PHP should be able to find it.
Yes, that is how I used to think to. But only until I hit a problem on a free host. There I had to use absolute path.

Feherke.
 
I'll have to check with my host (which isn't free) to see about the font.

There seems to be an issue with the line:
Code:
 //Caches the image for later use so you dont hog the processor
    imagejpeg($src_img,$wm_cache.$gallery."_".$file.".jpg", 80); [\code]

I commented it out and the shirt appears but if I leave it in I get the "red x box" in place of the image.

hmmmmm.....
 
They did say I have to create a "fonts" directory and then use absolute path. But when I did that I got an error "could not find font" or something along those lines. So I put it back the way I had it.

 
Okay, that's probably not going to work. The function, to the best of my knowledge, requires a filesystem absolute path, not a web absolute path.



Want the best answers? Ask the best questions! TANSTAAFL!
 
I asked my host again. They have a webchat support app. It's 24x7 but the answer I just received from them contradicts the previous response.
This time they tell me to use "public_html/fonts/CENTURY.TTF" So I'm going to try that...
 
I now get this error:
Code:
<b>Warning</b>:  imagettfbbox(): Could not find/open font in <b>/home/teelogic/public_html/shirt.php</b> on line <b>34</b><br />
when I use "public_html/fonts/century.ttf"
 
Imagine you were issuing a [tt]cd[/tt] command on the command line to change from the directory where your script resides to the directory where your font file resides.

Would the command:

[tt]cd public_html/fonts[/tt]

get you there?

Does the directory which contains your script have a directory named "public_html"? Does that directory contain a directory named "fonts"? Does that directory contain a True-Type font file named "century.ttf"? Are all those directory names exact, including the upper- and lower-case of the letters in all the names?




Want the best answers? Ask the best questions! TANSTAAFL!
 
Hi

"public_html/fonts/CENTURY.TTF" is not an absolute path, but you have it now from the error message :
Code:
$font_file = "/home/teelogic/public_html/CENTURY.TTF";

Feherke.
 
Thank you all over your replies. I appologize for not being able to check back sooner. As odd as it sounds, my world is on fire around me...literally. Forest fires are encrouching on my city and things have been weird...and ashy.
I don't think the path ended up being the problem, I'm still not sure what IS the issue but I think the problem lay within Internet Explorer 7. This code works in that it displays the shirt with text. It's not transparent but that's not a big deal.

Code:
<?php
$insert = imagecreatefromjpeg("tee.jpg"); 
$text="SAY I WON'T!";
$font="fonts/arial.ttf";

//choose textcolor (black)
$col=ImageColorAllocate ($insert, 0, 255, 255);

//check width of the text
$bbox=imagettfbbox (12, 0, $font, $text);
$xcorr=0-$bbox[6];
$mase=$bbox[2]+$xcorr;

//check width of the image
$width=imagesx($insert);

//calculate x coordinates for text
$new=($width-$mase)/2;

//write text
imagettftext ($insert, 12, 0, $new, 200, $col, $font, $text);

//output picture
imagegif($insert,"",100); 

?>
This works. I got this from php.com under the "imagecreatefromjpeg" function.

I've tried different fonts. They all work, including CENTURY.TTF. Odds my bodkins.

Now I need to try and center the text with a wordwrap effect. I'll look into this and post the complete code if I can come up with anything.

Thanks again for all your help(s).

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top