dpj96988 2012-09-18 12:28
浏览 97
已采纳

如何确保JPEG图像有效并且可以由PHP处理?

I have this JPEG that has been giving problems to the function imagesx($this->image) from the Resizer library that I am using. I am able to view the image using the browser, and when attempting to resize, I get the error:

imagesx() expects parameter 1 to be resource, boolean given

It is OK for me to not handle this file if it is going to throw an error. How can I use PHP to check whether this image can be handled properly by PHP's image functions?


Code that calls the Library

// Download the photo
$img_content = file_get_contents($url);
if($img_content !== FALSE) {
    file_put_contents($img_documentroot . $img_subpath . $img_filename . '_tmp.jpg',
     $img_content);
}

echo $url . '<br>';
echo $img_documentroot . $img_subpath . $img_filename . '_tmp.jpg<br>';
ob_flush();
flush();

// Resize photo
Resizer::open( $img_documentroot . $img_subpath . $img_filename . '_tmp.jpg' )
->resize(300, 300, 'landscape' )
->save($img_documentroot . $img_subpath . $img_filename . '.jpg' , 90 );

// Thumbnail photo
Resizer::open( $img_documentroot . $img_subpath . $img_filename . '_tmp.jpg' )
->resize(100, 100, 'crop' )
->save($img_documentroot . $img_subpath . $img_filename . '.jpg' , 90 );

Output

I also echoed out the full path of the image being resized.

http://www.ApartmentsInAllstonMA.com/Images/Apts/132847_kn1.jpg
/home/photos/public_html/2012/0917/2516539_7_tmp.jpg
resource(127) of type (gd)
resource(130) of type (gd)
http://www.ApartmentsInMedford.com/Images/Apts/132847_lv2.jpg
/home/photos/public_html/2012/0917/2516539_11_tmp.jpg
resource(163) of type (gd)
resource(166) of type (gd)
http://www.AllstonApartmentX.com/images/agents/61.jpg
/home/photos/public_html/2012/0917/2516539_12_tmp.jpg
bool(false)

UPDATE

This is the code snippet that is causing the library to return a false value.

private function open_image( $file )
{

    // If $file isn't an array, we'll turn it into one
    if ( !is_array($file) ) {
        $file = array(
            'type'      => File::mime( strtolower(File::extension($file)) ),
            'tmp_name'  => $file
        );
    }

    $mime = $file['type'];
    $file_path = $file['tmp_name'];

    switch ( $mime )
    {
        case 'image/pjpeg': // IE6
        case File::mime('jpg'): $img = @imagecreatefromjpeg( $file_path );  break;
        case File::mime('gif'): $img = @imagecreatefromgif( $file_path );   break;
        case File::mime('png'): $img = @imagecreatefrompng( $file_path );   break;
        default:                $img = false;                               break;
    }

    return $img;
}
  • 写回答

2条回答 默认 最新

  • dounie5475 2012-09-18 12:33
    关注

    Checking for the mime type is a very good method to ensure the validity of JPEG image. Here is how you can do this.

    $finfo = finfo_open(FILEINFO_MIME_TYPE); // return mime type
    $type = finfo_file($finfo, $filename) . "
    ";  
    if($type == "image/jpeg") { 
         //Valid JPEG Image
    }
    finfo_close($finfo);
    

    According to your errors, you are sending boolean value instead of image resource.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据