简体   繁体   中英

Increase php font size

I am trying to create an image by using PHP. This image is supposed to have text on it. I am only able to get the text displayed by using imagestring function, but this function only allows font size of upto 5. As it turns out, this is too small or tiny of a font for me. Then, I learned you can have text as big as you want on your image by using imagettftext function. This function doesn't work for me at all. I found this code in another stackoverflow question while I was researching on this issue.

<?php
$image = imagecreate(400,300);
$blue = imagecolorallocate($image, 0, 0, 255);
$white = ImageColorAllocate($image, 255,255,255);

if(!isset($_GET['size'])) $_GET['size'] = 44;
if(!isset($_GET['text'])) $_GET['text'] = "Hello, world!";

// Set the enviroment variable for GD
putenv('GDFONTPATH=' . realpath('.'));

// Name the font to be used (note the lack of the .ttf extension)
$font = 'arial.ttf';

imagettftext($image, $_GET['size'], 15, 50, 200, $white, $font, $_GET['text']);
header("content-type: image/png");
imagepng($image);
imagedestroy($image);
?> 

I see funny faces and weird characters on the browser when I call it up. What is wrong this code and what other options available for increasing font size in PHP?

UPDATE After much research on this, I've tried all the suggestions and answers that were given online for this issue, but still it won't display image. I added ./ in front of the font type string. I even uploaded a Microsoft font type from my computer to the server and still ended up with the same result.

UPDATE I did forget to mention that I actually did transfer font type arial.ttf from my computer to the server into the folder where all of my php files resides.

From the docs

fontfile The path to the TrueType font you wish to use.

Depending on which version of the GD library PHP is using, when fontfile does not begin with a leading / then .ttf will be appended to the filename and the library will attempt to search for that filename along a library-defined font path.

Remove the .ttf or add an absolute path

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM