Search code examples
laravelimage-resizingoctobercms

OctoberCMS: Cropping the original image on upload


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.


Solution

  • 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.