是否有一个 ImageMagick 函数可以判断文件类型是否受支持?
Is there an ImageMagick function that figures out if filetype is supported or not?
我有一组文件路径,需要 ImageMagick 运行 通过。
但是有些文件是 ImageMagick 不支持的文件,会中止循环并给我 "Uncaught exception" 错误。我只想跳过这些文件并转到下一个文件,但找不到任何相关信息。
这是我的循环:
// I'd like this section to be inclosed in a function that decides
// if Imagemagick should skip the file or do the thumbnail process
$imagick = new Imagick();
$imagick->readImage($wpdm_uploads_folder . $file);
$filename = ABSPATH.'wp-content/uploads/thumbs/';
if ( ! is_wp_error( $imagick ) ) {
if($new_file['extension'] == "psd"){
$imagick->setIteratorIndex(0);
}
$imagick->thumbnailImage(200, 0);
$imagick->writeImage($filename . $new_file['filename'] . '.png');
}
$imagick->clear();
$imagick->destroy();
Imagick::pingImage is your friend for getting information about a possible image. Ping image just looks at the image's header and meta-info, and doesn't load any actual image-data. Checkout article "to Ping, or not to Ping."
<?php
$images = array(
'built-in' => 'rose:',
'valid' => '/tmp/image.png',
'invalid' => '/tmp/server.sock',
);
$wand = new Imagick();
foreach( $images as $type => $path ) {
try {
$okay = $wand->pingImage($path);
} catch (ImagickException $error) {
$okay = FALSE;
}
if($okay) {
print $path . ' is an image' . PHP_EOL;
print $path . ' has a format of ' . $wand->getImageFormat() . PHP_EOL;
} else {
print $path . ' is NOT an image' . PHP_EOL;
}
}
输出...
rose: is an image
rose: has a format of PPM
/tmp/out.png is an image
/tmp/out.png has a format of PNG
/tmp/server.sock is NOT an image
编辑
如果您想在处理文件之前了解系统ImageMagick库支持的图像格式。 Imagick::queryFormats 可以生成内置列表、格式和类型。
对于命令行方法,我的第一个建议是使用
identify -ping -format "%m\n" filename
然后检查 return 值并在 <stdout>
上输出魔法文件类型。 (这可能与@emcconville 与 PHP 使用的方法完全相同)。
但是,我还没有在各种不同的文件格式上对此进行测试(还)。
我有一组文件路径,需要 ImageMagick 运行 通过。
但是有些文件是 ImageMagick 不支持的文件,会中止循环并给我 "Uncaught exception" 错误。我只想跳过这些文件并转到下一个文件,但找不到任何相关信息。
这是我的循环:
// I'd like this section to be inclosed in a function that decides
// if Imagemagick should skip the file or do the thumbnail process
$imagick = new Imagick();
$imagick->readImage($wpdm_uploads_folder . $file);
$filename = ABSPATH.'wp-content/uploads/thumbs/';
if ( ! is_wp_error( $imagick ) ) {
if($new_file['extension'] == "psd"){
$imagick->setIteratorIndex(0);
}
$imagick->thumbnailImage(200, 0);
$imagick->writeImage($filename . $new_file['filename'] . '.png');
}
$imagick->clear();
$imagick->destroy();
Imagick::pingImage is your friend for getting information about a possible image. Ping image just looks at the image's header and meta-info, and doesn't load any actual image-data. Checkout article "to Ping, or not to Ping."
<?php
$images = array(
'built-in' => 'rose:',
'valid' => '/tmp/image.png',
'invalid' => '/tmp/server.sock',
);
$wand = new Imagick();
foreach( $images as $type => $path ) {
try {
$okay = $wand->pingImage($path);
} catch (ImagickException $error) {
$okay = FALSE;
}
if($okay) {
print $path . ' is an image' . PHP_EOL;
print $path . ' has a format of ' . $wand->getImageFormat() . PHP_EOL;
} else {
print $path . ' is NOT an image' . PHP_EOL;
}
}
输出...
rose: is an image
rose: has a format of PPM
/tmp/out.png is an image
/tmp/out.png has a format of PNG
/tmp/server.sock is NOT an image
编辑
如果您想在处理文件之前了解系统ImageMagick库支持的图像格式。 Imagick::queryFormats 可以生成内置列表、格式和类型。
对于命令行方法,我的第一个建议是使用
identify -ping -format "%m\n" filename
然后检查 return 值并在 <stdout>
上输出魔法文件类型。 (这可能与@emcconville 与 PHP 使用的方法完全相同)。
但是,我还没有在各种不同的文件格式上对此进行测试(还)。