【问题标题】:Intervention Image: Encoding format (png?v=73d79a89bded&a) is not supported干预图像:不支持编码格式 (png?v=73d79a89bded&a)
【发布时间】:2015-10-21 21:07:59
【问题描述】:

我正在使用Laravel 5Embed包来抓取外部链接的元数据。然后我使用Intervention Image 包来操作链接的默认图像并将其保存在磁盘上。

一切正常,直到我尝试提交指向StackOverflow 问题的链接。然后我得到这个错误

AbstractEncoder.php 第 149 行中的 NotSupportedException:

不支持编码格式 (png?v=73d79a89bded&a)。

在 AbstractEncoder.php 第 149 行

在 AbstractEncoder->process(object(Image), 'png?v=73d79a89bded&a', null) > 在 AbstractDriver.php 第 77 行

在 AbstractDriver->encode(object(Image), 'png?v=73d79a89bded&a', null) in > Image.php 第 119 行

在 Image.php 第 139 行中的 Image->encode('png?v=73d79a89bded&a', null)

在 PostsController.php 第 70 行中的 Image->save('C:\xampp\htdocs\r2\public/images/rwSuGpEB.png?v=73d79a89bded&a')

我如何在Laravel 和干预包中处理这个问题?

如何从basename() 中删除?v=73d79a89bded&a

这是PostsController中的create()方法

public function store(PostRequest $request)
{
    if (Input::has('link')) {
        $input['link'] = Input::get('link');
        $info = Embed::create($input['link']);

        if ($info->image == null) {
            $embed_data = ['text' => $info->description];
        } else if ($info->description == null) {
            $embed_data = ['text' => ''];
        } else {
            $extension = pathinfo($info->image, PATHINFO_EXTENSION);

            $newName = public_path() . '/images/' . str_random(8) . ".{$extension}";

            if (File::exists($newName)) {
                $imageToken = substr(sha1(mt_rand()), 0, 5);
                $newName = public_path() . '/images/' . str_random(8) . '-' . $imageToken . ".{$extension}";
            }
            // This is line 70
            $image = Image::make($info->image)->fit(70, 70)->save($newName);
            $embed_data = ['text' => $info->description, 'image' => basename($newName)];
        }

        Auth::user()->posts()->create(array_merge($request->all(), $embed_data));

        return redirect('/subreddit');
    }
    Auth::user()->posts()->create($request->all());

    return redirect('/subreddit');
}

【问题讨论】:

    标签: php image laravel laravel-5 intervention


    【解决方案1】:

    如果您的图像名称,末尾有一个查询字符串?v=73d79a89bded&a。该查询字符串被错误地解释为图像文件扩展名的一部分。

    在进一步处理之前删除该查询字符串。

    更新

    假设 $extension 包含不需要的查询字符串

    $orig = pathinfo($info->image, PATHINFO_EXTENSION);
    $extension = substr($orig, 0, strpos($orig, '?'));
    

    【讨论】:

    • 我不是故意粗鲁,但我已经知道了。问题是,我怎样才能删除它?我可以使用basename() 来获取文件名+扩展名。
    • 抱歉,您的问题的原始版本并不清楚。在这种情况下,您可以大大缩短它。请参阅我的更新答案。
    • 是的,对此我深表歉意。之后我更新了我的问题。谢谢你,解决了。
    【解决方案2】:

    您似乎正在接受一个 URL,因此您应该先使用parse_url

    $parts = parse_url($input['link']);
    $extension = pathinfo($parts['path'], PATHINFO_EXTENSION);
    

    我还要注意,您可能应该使用 DIRECTORY_SEPARATOR 作为路径。

    【讨论】:

      猜你喜欢
      • 2015-02-20
      • 2021-12-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多