【问题标题】:Unable to init from given URL, Intervention Image, Laravel queued job无法从给定的 URL、干预图像、Laravel 排队作业初始化
【发布时间】:2019-07-01 21:55:43
【问题描述】:

我已排队作业,它保存来自给定 URL 的图像(从远程获取它们并保存在本地)并且一切正常,但是,如果我将此代码放入排队作业中,则会出现错误。

local.ERROR: Unable to init from given URL (URL that exists and I can access via browser)...

我可以访问这些 URL,allow_url_fopen 已启用,并且此代码无需排队作业即可运行。

try {
    $img = Image::make($photo->url);
    $img->save($path.$filenameBig); // Save the big image
} catch (NotReadableException $e) {
    var_dump($e->getMessage());
}

这个$photo->urlhttp,我可以在错误中看到它(它不是空的什么的)。我可以使用浏览器访问实际图像。我正在像这样导入Image

config/app中使用别名:

'Image' => Intervention\Image\ImageManagerStatic::class,

在提供者中(同一文件):

Intervention\Image\ImageServiceProvider::class

然后使用它:

use Image;

堆栈跟踪

ErrorException: file_get_contents(): https:// wrapper is disabled in the server configuration by allow_url_fopen=0 in /home/myes8adq/app/Libraries/PropertyLibrary.php:156
Stack trace:
0 [internal function]: Illuminate\Foundation\Bootstrap\HandleExceptions-handleError(2, 'file_get_conten...', '/home/myes8adq/...', 156, Array)
#1 /home/myes8adq/app/Libraries/PropertyLibrary.php(156): file_get_contents('https://dummyim...')
#2 /home/myes8adq/app/Libraries/PropertyLibrary.php(30): App\Libraries\PropertyLibrary->importOneChanged(Object(stdClass))
#3 /home/myes8adq/app/Jobs/ImportPropertiesAndNomenclaturesJob.php(48): App\Libraries\PropertyLibrary->importAllChanged()
#4 [internal function]: App\Jobs\ImportPropertiesAndNomenclaturesJob->handle()
#5 /home/myes8adq/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(32): call_user_func_array(Array, Array)
#6 /home/myes8adq/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(90): Illuminate\Container\BoundMethod::Illuminate\Container\{closure}()
#7 /home/myes8adq/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(34): Illuminate\Container\BoundMethod::callBoundMethod(Object(Illuminate\Foundation\Application), Array, Object(Closure))
#8 /home/myes8adq/vendor/laravel/framework/src/Illuminate/Container/Container.php(576): Illuminate\Container\BoundMethod::call(Object(Illuminate\Foundation\Application), Array, Array, NULL)
#9 /home/myes8adq/vendor/laravel/framework/src/Illuminate/Bus/Dispatcher.php(94): Illuminate\Container\Container->call(Array)
#10 /home/myes8adq/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(128): Illuminate\Bus\Dispatcher->Illuminate\Bus\{closure}(Object(App\Jobs\ImportPropertiesAndNomenclaturesJob))
#11 /home/myes8adq/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(104): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(App\Jobs\ImportPropertiesAndNomenclaturesJob))
#12 /home/myes8adq/vendor/laravel/framework/src/Illuminate/Bus/Dispatcher.php(98): Illuminate\Pipeline\Pipeline->then(Object(Closure))
#13 /home/myes8adq/vendor/laravel/framework/src/Illuminate/Queue/CallQueuedHandler.php(49): Illuminate\Bus\Dispatcher->dispatchNow(Object(App\Jobs\ImportPropertiesAndNomenclaturesJob), false)
#14 /home/myes8adq/vendor/laravel/framework/src/Illuminate/Queue/Jobs/Job.php(88): Illuminate\Queue\CallQueuedHandler->call(Object(Illuminate\Queue\Jobs\RedisJob), Array)
#15 /home/myes8adq/vendor/laravel/framework/src/Illuminate/Queue/Worker.php(327): Illuminate\Queue\Jobs\Job->fire()
#16 /home/myes8adq/vendor/laravel/framework/src/Illuminate/Queue/Worker.php(277): Illuminate\Queue\Worker->process('redis', Object(Illuminate\Queue\Jobs\RedisJob), Object(Illuminate\Queue\WorkerOptions))
#17 /home/myes8adq/vendor/laravel/framework/src/Illuminate/Queue/Worker.php(230): Illuminate\Queue\Worker->runJob(Object(Illuminate\Queue\Jobs\RedisJob), 'redis', Object(Illuminate\Queue\WorkerOptions))
#18 /home/myes8adq/vendor/laravel/framework/src/Illuminate/Queue/Console/WorkCommand.php(102): Illuminate\Queue\Worker->runNextJob('redis', 'default', Object(Illuminate\Queue\WorkerOptions))
#19 /home/myes8adq/vendor/laravel/framework/src/Illuminate/Queue/Console/WorkCommand.php(86): Illuminate\Queue\Console\WorkCommand->runWorker('redis', 'default')
#20 [internal function]: Illuminate\Queue\Console\WorkCommand->handle()
#21 /home/myes8adq/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(32): call_user_func_array(Array, Array)
#22 /home/myes8adq/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(90): Illuminate\Container\BoundMethod::Illuminate\Container\{closure}()
#23 /home/myes8adq/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(34): Illuminate\Container\BoundMethod::callBoundMethod(Object(Illuminate\Foundation\Application), Array, Object(Closure))
#24 /home/myes8adq/vendor/laravel/framework/src/Illuminate/Container/Container.php(576): Illuminate\Container\BoundMethod::call(Object(Illuminate\Foundation\Application), Array, Array, NULL)
#25 /home/myes8adq/vendor/laravel/framework/src/Illuminate/Console/Command.php(183): Illuminate\Container\Container->call(Array)
#26 /home/myes8adq/vendor/symfony/console/Command/Command.php(255): Illuminate\Console\Command->execute(Object(Symfony\Component\Console\Input\ArgvInput), Object(Illuminate\Console\OutputStyle))
#27 /home/myes8adq/vendor/laravel/framework/src/Illuminate/Console/Command.php(170): Symfony\Component\Console\Command\Command->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Illuminate\Console\OutputStyle))
#28 /home/myes8adq/vendor/symfony/console/Application.php(917): Illuminate\Console\Command->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#29 /home/myes8adq/vendor/symfony/console/Application.php(269): Symfony\Component\Console\Application->doRunCommand(Object(Illuminate\Queue\Console\WorkCommand), Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#30 /home/myes8adq/vendor/symfony/console/Application.php(145): Symfony\Component\Console\Application->doRun(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#31 /home/myes8adq/vendor/laravel/framework/src/Illuminate/Console/Application.php(90): Symfony\Component\Console\Application->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#32 /home/myes8adq/vendor/laravel/framework/src/Illuminate/Foundation/Console/Kernel.php(133): Illuminate\Console\Application->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#33 /home/myes8adq/artisan(37): Illuminate\Foundation\Console\Kernel->handle(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#34 {main}

【问题讨论】:

  • 如果它只在队列中失败,那么它一定是您的队列工作人员的问题。更新代码后您是否重新启动了它们?你能file_get_contents()这个网址吗?
  • 是的,我甚至更改了一些在线服务的 URL 以生成假图像。结果是一样的。
  • 可以分享图片链接吗?
  • 您能否提供队列工作者的完整错误(带有堆栈跟踪)?
  • 重要的是,作为 CLI 应用程序运行的队列工作器使用另一个 php.ini 而不是您的网络服务器。

标签: laravel laravel-5 intervention


【解决方案1】:

使用 allow_url_fopen=On 运行主管

command=php -d allow_url_fopen=On ...

【讨论】:

  • 共享主机
  • 什么 php -r 'echo ini_get("allow_url_fopen");'演出?
  • 它以某种方式加载了全局服务器 php.ini,我无法更改它,因为它是共享主机,但这些设置位于我帐户的本地 php.ini 文件(允许的位置)中。现在,问题是如何在队列作业的配置中设置本地 php.ini 文件。
  • 您是否尝试在队列代码中设置它? ini_set("allow_url_fopen", 1);在 $img = Image::make($photo->url); 之前
  • php -c path/to/correct/php.ini 有意义吗?使用 cURL 下载图片?
【解决方案2】:

如果您在共享主机上并且无法在队列工作人员中使用 allow_url_fopen,那么您可以通过 cURL 或 GuzzleHTTP 在本地下载图像,然后在本地文件上使用 Image::make()

例如

$url = "http://www.example.com/img.jpg";
if (!ini_get('allow_url_fopen') && function_exists('curl_version')) {

    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    $content = curl_exec($curl);
    curl_close($curl);

} else if (ini_get('allow_url_fopen')) {
    $content = file_get_contents($url);
} else {
    echo 'No dice.';
}

$i= Image::make($content);

【讨论】:

  • 谢谢,这将是我会尝试的最后一个解决方案,但我想这是更多的工作和更多的延迟。我认为,必须有另一种方式,其他人如何在共享主机上做到这一点,因为在这种类型的主机中,这个选项在任何地方都被全局禁用。
猜你喜欢
  • 2020-04-30
  • 1970-01-01
  • 2015-11-03
  • 2019-02-08
  • 1970-01-01
  • 1970-01-01
  • 2014-07-29
  • 2015-12-26
  • 2023-03-20
相关资源
最近更新 更多