php加载 GD库后 路径写的也正确 还是无法显示图片? 到底是啥原因 ? 求答案~
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php加载 GD库后 路径写的也正确 还是无法显示图片? 到底是啥原因 ? 求答案~相关的知识,希望对你有一定的参考价值。
参考技术A 图片是你用函数画出来的吗?把下面的代码运行一下,就知道你的GD库好了没
<?php
$image = imagecreatetruecolor(200,100);
//设置背景颜色
$bgcolor = imagecolorallocate($image,0,0,0);
//设置字体颜色
$textcolor = imagecolorallocate($image,255,255,255);
//把字符串写在图像左上角
imagestring($image,20,15,10,"Hello world!",$textcolor);
//输出图像
header("Content-type: image/jpeg");
imagejpeg($image);
?>
即使在激活 GD 库后,imagettftext() 也无法正常工作
【中文标题】即使在激活 GD 库后,imagettftext() 也无法正常工作【英文标题】:imagettftext() is Not Working even after activating GD library 【发布时间】:2021-11-20 18:26:45 【问题描述】:我可以通过 imagemerage 成功看到图像结果,但 imagettftext 无法正常工作,请帮我解决这个问题。
header('Content-type: image/jpeg');
$dest = imagecreatefromjpeg('ITI_card.jpg');
$filename = 'students_img/' . $student_photo;
$source = imagecreatefromjpeg($filename);
list($width, $height) = getimagesize($filename);
$newwidth = 230;
$newheight = 275;
$thumb = imagecreatetruecolor($newwidth, $newheight);
imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
$color = imagecolorallocate($dest, 0x00, 0x00, 0x00);
$font_path = 'arial.ttf';
imagettftext($dest, 25, 0, 266, 182, $color, $font_path, $name);
imagettftext($dest, 25, 0, 266, 232, $color, $font_path, $fathername);
imagettftext($dest, 25, 0, 266, 276, $color, $font_path, $gender);
imagettftext($dest, 25, 0, 266, 322, $color, $font_path, $branch);
imagettftext($dest, 25, 0, 266, 367, $color, $font_path, $academic_year);
imagettftext($dest, 25, 0, 266, 412, $color, $font_path, $roll_no);
imagettftext($dest, 25, 0, 266, 460, $color, $font_path, $dob);
imagettftext($dest, 25, 0, 266, 501, $color, $font_path, $address);
imagettftext($dest, 25, 0, 266, 532, $color, $font_path, $address1);
imagettftext($dest, 25, 0, 266, 595, $color, $font_path, $contact);
imagecopymerge($dest, $thumb, 761, 160, 0, 0, 230, 275, 100);
imagejpeg($dest);
【问题讨论】:
【参考方案1】:只要确定
-
字体文件 (arial.ttf) 存在于您的服务器中
变量 $student_photo、$name 等已正确设置。
那么应该没有问题。
请尝试以下代码:(1.jpg是示例图片,2.jpg是绿色图片。我已将文字更改为红色以使其更清晰)
<?php
header('Content-type: image/jpeg');
$dest = imagecreatefromjpeg('1.jpg');
$filename = '2.jpg';
$source = imagecreatefromjpeg($filename);
list($width, $height) = getimagesize($filename);
$newwidth = 230;
$newheight = 275;
$thumb = imagecreatetruecolor($newwidth, $newheight);
imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
$color = imagecolorallocate($dest, 0xFF, 0x00, 0x00);
$font_path = 'arial.ttf';
$test="TESTING";
imagettftext($dest, 25, 0, 266, 182, $color, $font_path, $test);
imagecopymerge($dest, $thumb, 761, 160, 0, 0, 230, 275, 100);
imagejpeg($dest);
?>
一个现场工作的例子是在
http://www.createchhk.com/SO/testSO21Nov2021.php
【讨论】:
以上是关于php加载 GD库后 路径写的也正确 还是无法显示图片? 到底是啥原因 ? 求答案~的主要内容,如果未能解决你的问题,请参考以下文章
即使在激活 GD 库后,imagettftext() 也无法正常工作
PHP 警告:PHP 启动:无法加载动态库 '/usr/lib/php/20151012/gd.so' - /usr/lib/php/20151012/gd.so:
Composer 安装“PHP 警告:PHP 启动:无法加载动态库 'gd2'”