使用PHP在图像上编写文本时出现字体问题

时间:2019-07-31 11:23:36

标签: php

当我从本地主机上的计算机调用字体时,代码正在工作。例如: $font = "C:\Windows\Fonts\arial.ttf";

但是当我把这种arial字体放在外面或将代码上传到服务器上时,它是行不通的。

我的代码看起来像这样。

$img = imagecreatefromjpeg('main.jpg');
$black = imagecolorallocate($img, 0, 0, 0);
$white = imagecolorallocate($img, 255, 255, 255);
$txt = "My name is Swapnil raja and I have created this image dynamically for featured image.";
$txtt = "Last Date";
$font = "arial.ttf"; //this font is available on the same place, where this code is

// THE IMAGE SIZE
$width = imagesx($img);
$height = imagesy($img);

// THE TEXT SIZE
$text_size = imagettfbbox(24, 0, $font, $txt);
$text_width = max([$text_size[2], $text_size[4]]) - min([$text_size[0], $text_size[6]]);
$text_height = max([$text_size[5], $text_size[7]]) - min([$text_size[1], $text_size[3]]);

// CENTERING THE TEXT BLOCK

$centerX = CEIL(($width - $text_width) / 2);
$centerX = $centerX<0 ? 0 : $centerX;
$centerX = CEIL(($height - $text_height) / 2);
$centerY = $centerX<0 ? 0 : $centerX;


imagettftext($img, 24, 0, $centerX-200, $centerY, $black, $font, wordwrap($txt));
imagettftext($img, 24, 0, $centerX-120, $centerY-240, $white, $font, wordwrap($txtt));


// OUTPUT IMAGE
header('Content-type: image/jpeg');
imagepng($img);
imagedestroy($png_image);

在此先感谢您的帮助

0 个答案:

没有答案
相关问题