This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.
erodriguez
In order to save size on my host, I want to resize the image uploaded before to save but I don't know how to do that
Any suggestion or a link?
On the other hand, I want to remove my prior image before upload. This is my code
function onAvatarUpdate() {
$model = Auth::getUser();
if(!Input::hasFile('avatar')) {
return;
}
// remove prior avatar
if($model->avatar !== null) {
$model->avatar->delete();
}
// update avatar
$model->avatar = Input::file('avatar');
// save
$model->save();
}
Is it ok?
Last updated
daftspunky
Hey, looks good. Try this
// Resize options
$width = 100;
$height = 100;
$options = [];
$file = Input::file('avatar');
$resizedFile = temp_path(uniqid().$file->getClientOriginalName());
Resizer::open($file->getRealPath())
->resize($width, $height, $options)
->save($resizedFile)
;
// update avatar
$model->avatar = $resizedFile;
// save
$model->save();
// cleanup
File::delete($resizedFile);
I haven't tested it but it could be the right direction. Good luck!
erodriguez
It doesn't work :(
I get the next error The avatar must be an image.
The image is created in temporary path location but after set the $resizedFile into $model->avatar, it is not able to save the model.
erodriguez
I could resolve the issue, this is my code
$model = Auth::getUser();
if(!Input::hasFile('avatar')) {
return;
}
// remove prior avatar
if($model->avatar !== null) {
$model->avatar->delete();
}
// save
$model->avatar = Input::file('avatar');
$model->save();
// resize
Resizer::open($model->avatar->getLocalPath())
->resize(800, 800)
->save($model->avatar->getLocalPath());
1-5 of 5