imagesx() 期望参数 1 是资源,给定布尔值



我正在使用一个图像缩略图库(Laravel的调整器包),它从目录中获取jpg并使用imagesx()将该图像保存为不同的大小。这在大多数情况下都没有错误。

问题:但是,有时当有一批图像要处理时,我会收到以下错误。在调试时,我做了

....
print_r($this->image);
imagesx($this->image);
....

PHP 输出

Resource id #64Resource id #67Resource id #73Resource id #76Resource id #82Resource id #85Resource id #91Resource id #94Resource id #100Resource id #103Resource id #109Resource id #112Resource id #118Resource id #121Resource id #127Resource id #130Resource id #136Resource id #139Resource id #145Resource id #148Resource id #154Resource id #157Resource id #163Resource id #166
Unhandled Exception
Message:
imagesx() expects parameter 1 to be resource, boolean given
Location:
/home/dev/public_html/bundles/resizer/resizer.php on line 69
Stack Trace:
#0 /home/dev/public_html/laravel/laravel.php(40): LaravelError::native(2, 'imagesx() expec...', '/home/dev/publi...', 69)
#1 [internal function]: Laravel{closure}(2, 'imagesx() expec...', '/home/dev/publi...', 69, Array)
#2 /home/dev/public_html/bundles/resizer/resizer.php(69): imagesx(false)
#3 /home/dev/public_html/bundles/resizer/resizer.php(81): Resizer->__construct('/home/photos/pu...')
#4 /home/dev/public_html/application/controllers/crawl/cl.php(369): Resizer::open('/home/photos/pu...')
#5 /home/dev/public_html/application/controllers/crawl/cl.php(315): Crawl_CL_Controller->save_photos(Array, 2516533, '2012-09-17 18:4...')
#6 [internal function]: Crawl_CL_Controller->action_crawl_next_item()
#7 /home/dev/public_html/laravel/routing/controller.php(325): call_user_func_array(Array, Array)
#8 /home/dev/public_html/laravel/routing/controller.php(285): LaravelRoutingController->response('crawl_next_item', Array)
#9 /home/dev/public_html/laravel/routing/controller.php(165): LaravelRoutingController->execute('crawl_next_item', Array)
#10 /home/dev/public_html/laravel/routing/route.php(153): LaravelRoutingController::call('crawl.cl@(:1)', Array)
#11 /home/dev/public_html/laravel/routing/route.php(124): LaravelRoutingRoute->response()
#12 /home/dev/public_html/laravel/laravel.php(165): LaravelRoutingRoute->call()
#13 /home/dev/public_html/public/index.php(34): require('/home/dev/publi...')
#14 {main}

结合上面的堆栈跟踪,您可以看到$this->image在其Resource id #166之后立即false

知道是什么原因造成的吗?我不明白为什么有时会false.谢谢!

发生这种情况是因为该文件的 mime 类型无法识别或不是 jpg/jpeg/png/gif。

$this>图像在 resize.php 的第 196 行设置为 false。

如果您无法让它工作,请在 Github 上创建一个问题,以便我可以跟进:-)

而不是使用 imagesx() 来获取图像的宽度imagesy() 来获取图像的高度您可以使用 list($width, $height, $type, $attr) = getimagesize($imagesource);

最新更新