草庐IT

php - imagesx() 期望参数 1 为资源, bool 值给定

coder 2024-04-17 原文

我正在使用图像缩略图库(Laravel 的 resizer bundle ),它从目录中获取 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): Laravel\Error::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): Laravel\Routing\Controller->response('crawl_next_item', Array)
#9 /home/dev/public_html/laravel/routing/controller.php(165): Laravel\Routing\Controller->execute('crawl_next_item', Array)
#10 /home/dev/public_html/laravel/routing/route.php(153): Laravel\Routing\Controller::call('crawl.cl@(:1)', Array)
#11 /home/dev/public_html/laravel/routing/route.php(124): Laravel\Routing\Route->response()
#12 /home/dev/public_html/laravel/laravel.php(165): Laravel\Routing\Route->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。

在 resizer.php 的第 196 行,$this->image 被设置为 false。

如果你不能让它工作,请在 Github 上创建一个问题,这样我就可以跟进:-)

关于php - imagesx() 期望参数 1 为资源, bool 值给定,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12470251/

有关php - imagesx() 期望参数 1 为资源, bool 值给定的更多相关文章

  1. ruby-on-rails - 如何在 ruby​​ 中使用两个参数异步运行 exe? - 2

    exe应该在我打开页面时运行。异步进程需要运行。有什么方法可以在ruby​​中使用两个参数异步运行exe吗?我已经尝试过ruby​​命令-system()、exec()但它正在等待过程完成。我需要用参数启动exe,无需等待进程完成是否有任何ruby​​gems会支持我的问题? 最佳答案 您可以使用Process.spawn和Process.wait2:pid=Process.spawn'your.exe','--option'#Later...pid,status=Process.wait2pid您的程序将作为解释器的子进程执行。除

  2. ruby - RSpec - 使用测试替身作为 block 参数 - 2

    我有一些Ruby代码,如下所示:Something.createdo|x|x.foo=barend我想编写一个测试,它使用double代替block参数x,这样我就可以调用:x_double.should_receive(:foo).with("whatever").这可能吗? 最佳答案 specify'something'dox=doublex.should_receive(:foo=).with("whatever")Something.should_receive(:create).and_yield(x)#callthere

  3. ruby - 如何在 Ruby 中拆分参数字符串 Bash 样式? - 2

    我正在为一个项目制作一个简单的shell,我希望像在Bash中一样解析参数字符串。foobar"helloworld"fooz应该变成:["foo","bar","helloworld","fooz"]等等。到目前为止,我一直在使用CSV::parse_line,将列分隔符设置为""和.compact输出。问题是我现在必须选择是要支持单引号还是双引号。CSV不支持超过一个分隔符。Python有一个名为shlex的模块:>>>shlex.split("Test'helloworld'foo")['Test','helloworld','foo']>>>shlex.split('Test"

  4. ruby - 检查方法参数的类型 - 2

    我不确定传递给方法的对象的类型是否正确。我可能会将一个字符串传递给一个只能处理整数的函数。某种运行时保证怎么样?我看不到比以下更好的选择:defsomeFixNumMangler(input)raise"wrongtype:integerrequired"unlessinput.class==FixNumother_stuffend有更好的选择吗? 最佳答案 使用Kernel#Integer在使用之前转换输入的方法。当无法以任何合理的方式将输入转换为整数时,它将引发ArgumentError。defmy_method(number)

  5. ruby-on-rails - 在默认方法参数中使用 .reverse_merge 或 .merge - 2

    两者都可以defsetup(options={})options.reverse_merge:size=>25,:velocity=>10end和defsetup(options={}){:size=>25,:velocity=>10}.merge(options)end在方法的参数中分配默认值。问题是:哪个更好?您更愿意使用哪一个?在性能、代码可读性或其他方面有什么不同吗?编辑:我无意中添加了bang(!)...并不是要询问nobang方法与bang方法之间的区别 最佳答案 我倾向于使用reverse_merge方法:option

  6. ruby - 定义方法参数的条件 - 2

    我有一个只接受一个参数的方法:defmy_method(number)end如果使用number调用方法,我该如何引发错误??通常,我如何定义方法参数的条件?比如我想在调用的时候报错:my_method(1) 最佳答案 您可以添加guard在函数的开头,如果参数无效则引发异常。例如:defmy_method(number)failArgumentError,"Inputshouldbegreaterthanorequalto2"ifnumbereputse.messageend#=>Inputshouldbegreaterthano

  7. ruby - 安装 Ruby 时遇到问题(无法下载资源 "readline--patch") - 2

    当我尝试安装Ruby时遇到此错误。我试过查看this和this但无济于事➜~brewinstallrubyWarning:YouareusingOSX10.12.Wedonotprovidesupportforthispre-releaseversion.Youmayencounterbuildfailuresorotherbreakages.Pleasecreatepull-requestsinsteadoffilingissues.==>Installingdependenciesforruby:readline,libyaml,makedepend==>Installingrub

  8. ruby - rails 3 redirect_to 将参数传递给命名路由 - 2

    我没有找到太多关于如何执行此操作的信息,尽管有很多关于如何使用像这样的redirect_to将参数传递给重定向的建议:action=>'something',:controller=>'something'在我的应用程序中,我在路由文件中有以下内容match'profile'=>'User#show'我的表演Action是这样的defshow@user=User.find(params[:user])@title=@user.first_nameend重定向发生在同一个用户Controller中,就像这样defregister@title="Registration"@user=Use

  9. ruby - 字符串文字中的转义状态作为 `String#tr` 的参数 - 2

    对于作为String#tr参数的单引号字符串文字中反斜杠的转义状态,我觉得有些神秘。你能解释一下下面三个例子之间的对比吗?我特别不明白第二个。为了避免复杂化,我在这里使用了'd',在双引号中转义时不会改变含义("\d"="d")。'\\'.tr('\\','x')#=>"x"'\\'.tr('\\d','x')#=>"\\"'\\'.tr('\\\d','x')#=>"x" 最佳答案 在tr中转义tr的第一个参数非常类似于正则表达式中的括号字符分组。您可以在表达式的开头使用^来否定匹配(替换任何不匹配的内容)并使用例如a-f来匹配一

  10. ruby-on-rails - 如何生成传递一些自定义参数的 `link_to` URL? - 2

    我正在使用RubyonRails3.0.9,我想生成一个传递一些自定义参数的link_toURL。也就是说,有一个articles_path(www.my_web_site_name.com/articles)我想生成如下内容:link_to'Samplelinktitle',...#HereIshouldimplementthecode#=>'http://www.my_web_site_name.com/articles?param1=value1¶m2=value2&...我如何编写link_to语句“alàRubyonRailsWay”以实现该目的?如果我想通过传递一些

随机推荐