Question

I'm trying to create an image from google's php appengine that contains some text rendered using the imagettftext php function. I have to especify a font file name and it's path to the function but i don't know how to access my static fonts directory from php script as scripts are stored separately from static files when i deploy the application.

This is the relevant code from my php script:

header("Content-type: image/png");
$image = imagecreatetruecolor($imgWidth, $imgHeight);
imagealphablending($image, false);
$transparency = imagecolorallocatealpha($image, 0, 0, 0, 127);
$black = imagecolorallocatealpha($image, 0,0,0, 0);
$font = './font/Allerta-Regular.ttf';
imagefill($image, 0, 0, $transparency);
imagesavealpha($image, true);
imagealphablending($image, true);
imagettftext($image, $fontSize, $angle, 100, 100, $black, $font, "Test text");
imagepng($image);
imagedestroy($image);

The directory structure is something like this:
- root
    - js
    - css
    - font
    - img
Where php script is in the root folder and font files are in font folder.

The code above works on my local PC but does not work when i deploy to the server as imagettftext can't find the font and a error message is raised. Using URL full path does not work neither as imagettftext needs a local file path.

I've tried $font = '/font/Allerta-Regular.ttf' (without the initial dot) also but no luck.

I know that i can use "Google Cloud Storage" service but i don't want to hire it and duplicate files as this fonts are used from my js files also.

How can i specify the font in PHP app engine to use with imagettftext?

Thanks

Was it helpful?

Solution

Finally solved. As app.yaml configuration page says:

For efficiency, App Engine stores and serves static files separately from application files. Static files are not available in the application's file system. If you have data files that need to be read by the application code, the data files must be application files, and must not be matched by a static file pattern.

So i created a new "scriptfont" folder and defined a new entry in app.yaml file to say that the fonts will be loaded from the script, and not served to the browser:

- url: /scriptfont/(.*)
  script: /scriptfont/\1

Now, all files inside this folder can be used by php scripts.

OTHER TIPS

I did not have to declare anything in the app.yaml to handle the font file requests. My requirement was to use custom fonts to produce a barcoded label. The font files don't have to be served tot he world and only used by the app.

function label()
{
    ....
    $fbarcode = './resources/ttf/FRE3OF9X.TTF';
    imagettftext($im, 75, 0, 50, 170, $black, $fbarcode, "*{$p['id']}*");
    ....

    header('Content-Type: image/png');
    imagepng($im);
    imagedestroy($im);
}

The font file is located in the codebase under /resources. This works both on local environment as well as on gae. Again I did not have to define anything in app.yaml.

I have solved to treat the font folder when appling cool-php-captcha in GAE using php runtime follow the solution from Alex Alarcon i.e.:

- url: /(.+\.(otf|ttf|woff))$
  script: \1

For python solution its handler should treat remain as default i.e.

- url: /(.*\.ttf)
  mime_type: font/opentype
  static_files: \1
  upload: (.*\.ttf)$
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top