javascript 检测图像是否损坏或损坏

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9938483/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 08:17:46  来源:igfitidea点击:

Detecting whether an image is corrupted or broken

phpjavascriptpythonimage

提问by Dilletante

I need to programmatically check whether the image that the user has selected as his wallpaper on my app is broken or corrupted....... basically I provide user with the option to choose his own image as wallpaper. Now when the images loads, I just want to keep a check on whether it is somehow corrupt or not.......

我需要以编程方式检查用户在我的应用程序上选择作为他的墙纸的图像是否损坏或损坏......基本上我为用户提供了选择他自己的图像作为墙纸的选项。现在当图像加载时,我只想检查它是否以某种方式损坏......

回答by objectified

If instead you are looking for a PHP solution instead of a javascript solution (which the potential duplicates do not provide), you can use GD's getimagesize()in PHP and see what it returns. It will return false and throw an error when the provided image format is not valid.

相反,如果您正在寻找 PHP 解决方案而不是 javascript 解决方案(潜在重复项不提供),您可以在 PHP 中使用 GD 的getimagesize()并查看它返回的内容。当提供的图像格式无效时,它将返回 false 并抛出错误。

回答by objectified

Here is a PHP CLI script you can run on a directory full of images and it will log which files are corrupted based on an imagecreatefrom***()test. It can just log the bad files or take action and delete them.

这是一个 PHP CLI 脚本,您可以在一个充满图像的目录上运行它,它会根据imagecreatefrom***()测试记录哪些文件已损坏。它可以只记录坏文件或采取行动并删除它们。

https://github.com/e-ht/literate-happiness

https://github.com/e-ht/literate-happiness

You can also plug it in to a database to take action on image paths that you may have stored.

您还可以将其插入数据库以对您可能已存储的图像路径执行操作。

Here is the meat of the function it uses:

这是它使用的函数的主要内容:

$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++;
        }
    }
}

回答by mikeytown2

This seems to work for me.

这似乎对我有用。

<?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;
  }
?>