Skip to content

Instantly share code, notes, and snippets.

@bugzbrown
Created February 23, 2020 14:55
Show Gist options
  • Save bugzbrown/2fadb8b460f3217a54992467b310528e to your computer and use it in GitHub Desktop.
Save bugzbrown/2fadb8b460f3217a54992467b310528e to your computer and use it in GitHub Desktop.
Resize Image on Save octoberCMS
use October\Rain\Database\Attach\Resizer;
class ClassName extends Model{
public $attachOne = [
'img' => 'System\Models\File',
];
public function afterSave(){
$imgPath = $this->img->getLocalPath();
$width = 800;
$height = 800;
$options = []; // or ['mode' => 'crop']
Resizer::open($imgPath)
->resize($width, $height, $options)
->save($imgPath);
}
}
@bugzbrown
Copy link
Author

This can be used in any model of octoberCMS that has an image - it is used to ensure that the file is of a maximum size (width and height).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment