我目前正在使用imagecreatefromjpeg()
向图像添加文本。我让它处理我的所有文件都在一个文件夹中,但是我使用的是 CodeIgniter,所以我的文件分布在不同的文件夹中。
实现此目的的解决方法是什么?
我想把我所有需要的文件放在我的视图中,但这不起作用。
查看错误
遇到 PHP 错误
严重性:警告
消息: imagettftext(): 找不到/打开字体
文件名:视图/优惠券.php
行号:30
查看代码
<?php
//Report any errors
ini_set("display_errors", "1");
error_reporting(E_ALL);
//Set the Content Type
// header('Content-type: image/jpeg');
// Create Image From Existing File
$jpg_image = imagecreatefromjpeg('http://localhost:8888/game/rcw/assets/couponWrite.jpg');
// Allocate A Color For The Text
$white = imagecolorallocate($jpg_image, 155, 155, 155);
// Set Path to Font File
$font_path = 'http://localhost:8888/game/rcw/application/views/Arial.ttf';
// Set Text to Be Printed On Image
$text = "IGL" . rand(55555,99999);
// Print Text On Image
imagettftext($jpg_image, 25, 0, 75, 300, $white, $font_path, $text);
// Send Image to Browser
imagejpeg($jpg_image);
// Clear Memory
imagedestroy($jpg_image);
?>
您所要做的就是使用图像文件的路径访问图像文件,无论是相对于处理它的 PHP 文件的路径,还是更好的是绝对路径。例如,假设您的代码位于控制器中的文件application/controllers/myImageProcessor.php
中,并且您想加载存储在application/input-files
中的文件,在其上写入一些内容并将修改后的版本存储在public/images
中。您可以像这样生成文件的路径:
// input path is '../input-files/'
$inputPath = dirname(__FILE__).'/input-files/'; // this is an absolute path
$inputName = 'original.jpg';
// output path is '../../public/images/'
$outputPath = dirname(dirname(__FILE__)).'/public/images/';
$outputName = 'modified.jpg';
// load image from file
$img = imagecreatefromjpeg($inputPath.$inputName);
// ...
// write on $img here...
// ...
// save it to other file
imagejpeg($img, $outputPath.$outputName, 95);
将所有文件存储在单个目录中不是一个好主意。此外,不要将代码与 HTML/JS/CSS 混合使用,并且永远不要将生成的文件或从外部源检索的文件(例如由用户上传)放在同一个目录中的源文件。