我正在使用MySQL数据库保存一些字体。我需要使用自定义字体系列文本创建图像。我正在使用SplTempFileObject处理setFont方法,但这是imagick所说的:
Fatal error: Uncaught exception 'ImagickDrawException' with message 'The given font is not found in the ImageMagick configuration and the file ($_SERVER['DOCUMENT_ROOT]php:/temp) is not accessible'
Run Code Online (Sandbox Code Playgroud)
(当然$ _SERVER ['DOCUMENT_ROOT']是实际值,我只是替换了它):)
任何解决方案还是我必须在文件系统中保存字体?
这是代码:
$image = new \Imagick();
$draw = new \ImagickDraw();
$temp = new \SplTempFileObject();
$temp->fwrite($font->getFile()->getContent());
$image->newImage(550, 50, "black");
$draw->setFont($temp);
Run Code Online (Sandbox Code Playgroud)
$ font是来自数据库的数据。我认为问题是因为ImagickDraw :: setFont()相对于DOC ROOT搜索字体。
此问题是由SplTempFileObject引起的,该SplTempFileObject默认使用最多2MB的内存中缓存。
\SplTempFileObject->getFileName()
将返回php://temp
\SplTempFileObject->getFileInfo()
将返回一个空对象/文件指针
\SplTempFileObject->getRealPath()
将始终返回一个空字符串!
如果您想让PHP在文件系统中写入实际文件,则必须提供内存限制0
作为构造参数,但SplFileInfo
除此以外,您将无法获得有效的,路径名或文件名。php://temp/maxmemory:0
是:很奇怪,而且与用于创建临时文件的“简单界面”相去甚远。
有关详细信息,请查看http://php.net/manual/spltempfileobject.construct.php
回去,使用旧的tempfile()
或tempnam()
。我的解决方案:
代替
$file = new \SplTempFileObject();
我用
$file = new \SplFileObject(tempnam(sys_get_temp_dir(), rand()), 'w+');