Halnex
Halnex

Reputation: 4526

Intervention Image: Encoding format (png?v=73d79a89bded&a) is not supported

I am using Laravel 5 Embed package to grab the meta data of external links. I then use Intervention Image package to manipulate the default image of the link and save it on the disk.

All works well, until I try to submit a link to a StackOverflow question. Then I get this error:

NotSupportedException in AbstractEncoder.php line 149:

Encoding format (png?v=73d79a89bded&a) is not supported.

in AbstractEncoder.php line 149

at AbstractEncoder->process(object(Image), 'png?v=73d79a89bded&a', null) > in AbstractDriver.php line 77

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

at Image->encode('png?v=73d79a89bded&a', null) in Image.php line 139

at Image->save('C:\xampp\htdocs\r2\public/images/rwSuGpEB.png?v=73d79a89bded&a') in PostsController.php line 70

How do I deal with this in Laravel and Intervention package?

How do you remove ?v=73d79a89bded&a from the basename()?

This is create() method in PostsController

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');
}

Upvotes: 0

Views: 2681

Answers (2)

samlev
samlev

Reputation: 5942

It looks like you're accepting a URL, so you should use parse_url first:

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

I'll also note that you should probably be using DIRECTORY_SEPARATOR for paths.

Upvotes: 1

Eric J.
Eric J.

Reputation: 150108

There is a query string ?v=73d79a89bded&a at the end if your image name. That query string is being interpreted, incorrectly, as part of the image's file extension.

Remove that query string before further processing.

UPDATE

Assuming $extension holds the unwanted query string

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

Upvotes: 3

Related Questions