Kanishka perera
Kanishka perera

Reputation: 1

Is there any method to crop image in laravel open-admin?

NotSupportedException In AbstractEncoder.php line 200 : Encoding format (tmp) is not supported.

Previously, I uploaded images in Laravel Open-Admin for my specified storage path. It worked correctly. but later I have to crop images when uploading. Then I followed the documentation of open admin https://open-admin.org/docs/en/model-form-upload . it says to install the intervention/image package and use crop() to crop the image. I code like this,

$form->image('cover_photo', __('Cover photo'))->crop(800,800,50,50);

but it didn't work. All the time showed errors like this

 NotSupportedException In AbstractEncoder.php line 200 :
  Encoding format (tmp) is not supported.

this is the full form function of my controller in open-admin

protected function form()
    {
        $form = new Form(new Book());

        $form->text('name', __('Name'));
        $form->ckeditor('description', __('Description'));       
        $form->image('cover_photo', __('Cover photo'))->crop(800,800,50,50);
        return $form;
    }

also, I change the code in several ways to solve this error,

$form->image('cover_photo', __('Cover photo'))->crop(800,800,50,50)->encode('jpg');
$form->image('cover_photo', __('Cover photo'))->crop(800,800,50,50)->encode('jpg',90);
$form->image('cover_photo', __('Cover photo'))->crop(800,800,50,50)->encode('jpg',75);

I checked the code both GD and imagick libraries, but error was the same. i checked the AbstractEncoder.php file which error says about.

[2023-04-08 08:59:29] local.ERROR: Encoding format (tmp) is not supported. {"userId":1,"exception":"[object] (Intervention\\Image\\Exception\\NotSupportedException(code: 0): Encoding format (tmp) is not supported. at F:\\Office\\Admin2\\vendor\\intervention\\image\\src\\Intervention\\Image\\AbstractEncoder.php:200)
[stacktrace]
#0 F:\\Office\\Admin2\\vendor\\intervention\\image\\src\\Intervention\\Image\\AbstractDriver.php(79): Intervention\\Image\\AbstractEncoder->process(Object(Intervention\\Image\\Image), 'tmp', NULL)
#1 F:\\Office\\Admin2\\vendor\\intervention\\image\\src\\Intervention\\Image\\Image.php(121): Intervention\\Image\\AbstractDriver->encode(Object(Intervention\\Image\\Image), 'tmp', NULL)
#2 F:\\Office\\Admin2\\vendor\\intervention\\image\\src\\Intervention\\Image\\Image.php(146): Intervention\\Image\\Image->encode('tmp', NULL)
#3 F:\\Office\\Admin2\\vendor\\open-admin-org\\open-admin\\src\\Form\\Field\\Traits\\ImageField.php(53): Intervention\\Image\\Image->save('C:\\\\wamp64\\\\tmp\\\\p...')
#4 F:\\Office\\Admin2\\vendor\\open-admin-org\\open-admin\\src\\Form\\Field\\Image.php(53): OpenAdmin\\Admin\\Form\\Field\\Image->callInterventionMethods('C:\\\\wamp64\\\\tmp\\\\p...')
#5 F:\\Office\\Admin2\\vendor\\open-admin-org\\open-admin\\src\\Form.php(913): OpenAdmin\\Admin\\Form\\Field\\Image->prepare(Object(Illuminate\\Http\\UploadedFile))
#6 F:\\Office\\Admin2\\vendor\\open-admin-org\\open-admin\\src\\Form.php(358): OpenAdmin\\Admin\\Form->prepareInsert(Array)

I think these code lines affected for error.

if some one could give me a solution for this, it would be great. thank you

I want to crop an upload image in Laravel open-admin

Upvotes: 0

Views: 413

Answers (1)

Marcin Orlowski
Marcin Orlowski

Reputation: 75645

Seems .tmp filename suffix confuses the package. Try to correct this:

protected function form()
{
    $form = new Form(new Book());

    $form->text('name', __('Name'));
    $form->ckeditor('description', __('Description'));
    $form->image('cover_photo', __('Cover photo'))
        ->crop(800, 800, 50, 50)
        ->move('public/cover_photos') // Set your desired folder
        ->uniqueName()
        ->saving(function (File $file) {
            return $file->guessExtension();
        });

    return $form;
}

Upvotes: 0

Related Questions