PHP库对象错误 [英] PHP library object error
问题描述
我在imagick
php库中遇到问题.
I am having an issue with imagick
php library.
我正在文件系统中进行递归搜索,并查找任何pdf
文件.
I am doing a recursive search in my file system and look for any pdf
files.
$it = new RecursiveDirectoryIterator("/test/project");
$display = Array ('pdf');
foreach(new RecursiveIteratorIterator($it) as $file){
if (in_array(strtolower(array_pop(explode('.', $file))), $display))
{
if(file_exists($file)){
echo $file; //this would echo /test/project/test1.pdf
$im = new Imagick($file);
$im->setImageFormat("jpg");
file_put_contents('test.txt', $im);
}
}
}
但是,我收到一个错误提示
However, I am getting an error saying
Fatal error: Uncaught exception 'ImagickException' with message 'Can not process empty Imagick object' in /test.php:57
Stack trace:
#0 /test.php(57): Imagick->setimageformat('jpg')
#1 {main}
thrown in /test.php on line 57
line 57 is $im->setImageFormat("jpg");
但是,如果我将我的$im = new Imagick($file)
替换为$im = new Imagick('/test/project/test1.pdf'),
,该错误消失了.
However, if I replace my $im = new Imagick($file)
with $im = new Imagick('/test/project/test1.pdf'),
the error is gone.
我不知道为什么会这样.有人可以给我提示这个问题吗?非常感谢
I don't know why this is happening. Can someone give me a hint for this issue? Thanks so much
推荐答案
@pozs指出
注1:您的$ file变量是一个对象SplFileInfo,但是您是 总是像字符串一样使用它.
Note 1: Your $file variable is an object SplFileInfo, but you are using it always like a string.
这是一个代码段,可让您将文件名作为字符串获取,并具有获取文件扩展名的优化方法:
Here's a code snippet which gets you the filename as string and has an optimized method to get the file extension:
<?php
$display = array ('pdf');
$directoryIterator = new RecursiveDirectoryIterator('./test/project');
// The key of the iterator is a string with the filename
foreach (new RecursiveIteratorIterator($directoryIterator) as $fileName => $file) {
// Optimized method to get the file extension
$fileExtension = pathinfo($fileName, PATHINFO_EXTENSION);
if (in_array(strtolower($fileExtension), $display)) {
if(file_exists($fileName)){
echo "{$fileName}\n";
// Just do what you want with Imagick here
}
}
这篇关于PHP库对象错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!