Image.php中被零除的错误

时间:2016-04-18 作者:brandozz

在服务器上查看错误日志时,我不断看到错误:

PHP Warning:  Division by zero in /public_html/wp-content/themes/sass-wordpress-bootstrap-master/image.php on line 95
以下是发生错误的代码:

$imgmeta = wp_get_attachment_metadata( $id );

                    // Convert the shutter speed retrieve from database to fraction
                          if ((1 / $imgmeta[\'image_meta\'][\'shutter_speed\']) > 1)
                          {
                             if ((number_format((1 / $imgmeta[\'image_meta\'][\'shutter_speed\']), 1)) == 1.3
                             or number_format((1 / $imgmeta[\'image_meta\'][\'shutter_speed\']), 1) == 1.5
                             or number_format((1 / $imgmeta[\'image_meta\'][\'shutter_speed\']), 1) == 1.6
                             or number_format((1 / $imgmeta[\'image_meta\'][\'shutter_speed\']), 1) == 2.5){
                                $pshutter = "1/" . number_format((1 / $imgmeta[\'image_meta\'][\'shutter_speed\']), 1, \'.\', \'\') . " second";
                             }
                             else{
                               $pshutter = "1/" . number_format((1 / $imgmeta[\'image_meta\'][\'shutter_speed\']), 0, \'.\', \'\') . " second";
                             }
                          }
                          else{
                             $pshutter = $imgmeta[\'image_meta\'][\'shutter_speed\'] . " seconds";
                           }
我真的不知道什么时候会叫它。机器人是否会触发此错误?

3 个回复
最合适的回答,由SO网友:Mark Kaplun 整理而成

快门速度是wordpress在上传图像时试图从图像中提取的exif数据之一。如果图像不包含该信息,它将为零,因此您的除零错误。在代码之前的某个地方,您应该检查图像是否需要转到那里。如果不是照片,那么可能不是。

SO网友:Bruno Cantuaria

即使您确信所有图像都声明了“shutter\\u speed”且大于0(尽管您的错误报告说有些图像没有),验证它也是一种很好的做法。

如果出现以下情况,请尝试包含新的父级:

if ( isset($imgmeta[\'image_meta\'][\'shutter_speed\']) && (intval($imgmeta[\'image_meta\'][\'shutter_speed\'])>0) ) {
   ...
} else {
   $pshutter = \'unknow\';
}

SO网友:brandozz

该错误是在图像附件页上生成的,因此这很可能是一个机器人。但在快门速度转换为分数之前,我添加了一个检查:

$imgmeta = wp_get_attachment_metadata( $id );
                        $imgShutter = $imgmeta[\'image_meta\'][\'shutter_speed\'];
                        // Convert the shutter speed retrieve from database to fraction

                        if ($imgShutter > 0) {

                          if ((1 / $imgmeta[\'image_meta\'][\'shutter_speed\']) > 1)
                          {
                             if ((number_format((1 / $imgmeta[\'image_meta\'][\'shutter_speed\']), 1)) == 1.3
                             or number_format((1 / $imgmeta[\'image_meta\'][\'shutter_speed\']), 1) == 1.5
                             or number_format((1 / $imgmeta[\'image_meta\'][\'shutter_speed\']), 1) == 1.6
                             or number_format((1 / $imgmeta[\'image_meta\'][\'shutter_speed\']), 1) == 2.5){
                                $pshutter = "1/" . number_format((1 / $imgmeta[\'image_meta\'][\'shutter_speed\']), 1, \'.\', \'\') . " second";
                             }
                             else{
                               $pshutter = "1/" . number_format((1 / $imgmeta[\'image_meta\'][\'shutter_speed\']), 0, \'.\', \'\') . " second";
                             }
                          }
                          else{
                             $pshutter = $imgmeta[\'image_meta\'][\'shutter_speed\'] . " seconds";
                           }
                        }