4

Given the following code:

$car= new Car();
$car->name = Input::get('name');
$car->photo = Input::file('photo');
$car->save();

I need to crop the photo (with offset) before saving it. I tried using the ImageResizer plugin but couldn't figure out how to integrate its API with the above code.

halfer
  • 19,824
  • 17
  • 99
  • 186
B Faley
  • 17,120
  • 43
  • 133
  • 223

1 Answers1

4

Yes you can resize image using that plugin but you even don't need it as internally it also use OctoberCMS built-in Resize function.

First you need to save it on disk and then resize it in-place.

for this you can use October Cms's in-built Resizer https://octobercms.com/docs/api/october/rain/database/attach/resizer

You can also crop image if you need just read https://octobercms.com/docs/api/october/rain/database/attach/resizer#crop doc and you are good to go. There are lot more options if you need.

<?php namespace hardiksatasiya/...somethig;

use October\Rain\Database\Attach\Resizer;

// ...

$car= new Car();
$car->name = Input::get('name');
$car->photo = Input::file('photo');
$car->save();

// code to resize image
$width = 100;
$height = 100;
$options = []; // or ['mode' => 'crop']

Resizer::open($car->photo->getLocalPath()) // create from real path
          ->resize($width, $height, $options)
          ->save($car->photo->getLocalPath());

This code will open saved Image, Resize it and save it in same place.

If You get any problem please comment.

Hardik Satasiya
  • 9,547
  • 3
  • 22
  • 40
  • Hi. When I try to crop the photo `Resizer::open($car->photo)->crop(10, 10, 100, 100)->save($car->photo->getLocalPath());` I get this error: `Call to undefined method October\Rain\Database\QueryBuilder::guessExtension()`. Removing this line of code fixes the error. Did you test this code and it worked? – B Faley Apr 29 '18 at 03:56
  • I didnt check it but if this also gives you error i will check and correct it after testing, for now I see In resize there is different instance of file, modified code `Resizer::open($car->photo->getLocalPath())` just check it if it works or not and let me know. – Hardik Satasiya Apr 29 '18 at 07:31
  • sorry for posting without testing I was in little hurry so.. but i will test it when i get free. – Hardik Satasiya Apr 29 '18 at 08:00
  • Thanks. I will test it and get back to you as soon as possible. – B Faley Apr 29 '18 at 10:14
  • Thanks. This is saving the image twice, once the original image and then the resized one. Do you think it's possible to crop and save at once? – B Faley Apr 29 '18 at 14:41
  • i will check it and let you know, it should override old one but i will check it. – Hardik Satasiya Apr 29 '18 at 15:28