我需要以编程方式检查用户在我的应用程序上选择作为他的墙纸的图像是否损坏或损坏......基本上我提供给用户选择自己的图像作为墙纸的选项。现在,当图像加载时,我只想检查它是否以某种方式损坏了......
发布于 2012-03-30 15:45:52
相反,如果您正在寻找PHP解决方案而不是javascript解决方案(潜在的复制品不提供),您可以在PHP中使用GD的getimagesize(),并查看它返回了什么。当提供的图像格式无效时,它将返回false并抛出错误。
发布于 2016-07-16 07:24:14
这是一个PHP脚本,你可以在一个充满图像的目录上运行,它将根据imagecreatefrom***()测试记录哪些文件被损坏。它可以只记录坏文件,也可以采取措施删除它们。
https://github.com/e-ht/literate-happiness
您还可以将其插入到数据库中,以对可能已存储的图像路径执行操作。
下面是它使用的函数的主要部分:
$loopdir = new DirectoryIterator($dir_to_scan);
foreach($loopdir as $fileinfo) {
if(!$fileinfo->isDot()) {
$file = $fileinfo->getFilename();
$file_path = $dir_to_scan . '/' . $file;
$mime_type = mime_content_type($file_path);
switch($mime_type) {
case "image/jpg":
case "image/jpeg":
$im = imagecreatefromjpeg($file_path);
break;
case "image/png":
$im = imagecreatefrompng($file_path);
break;
case "image/gif":
$im = imagecreatefromgif($file_path);
break;
}
if($im) {
$good_count++;
}
elseif(!$im) {
$bad_count++;
}
}
}发布于 2015-04-03 04:25:44
这似乎对我很有效。
<?php
$ext = strtolower(pathinfo($image_file, PATHINFO_EXTENSION));
if ($ext === 'jpg') {
$ext = 'jpeg';
}
$function = 'imagecreatefrom' . $ext;
if (function_exists($function) && @$function($image_file) === FALSE) {
echo 'bad img file: ' . $image_file . ' ' . $function;
}
?>https://stackoverflow.com/questions/9938483
复制相似问题