Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Customising the CMS /

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

Resize image after upload


Go to End


4 Posts   1218 Views

Avatar
Johnny9

Community Member, 35 Posts

23 April 2015 at 6:14pm

Edited: 23/04/2015 8:12pm

Hi,
I let users upload images to my website, but sometimes they upload huge resolution images.
So I want to resize these images after upload.
Just for beggining I use this code:

$original = File::get()->byID($file->ID);
$file720 = $original->SetRatioSize(720,720);
$file720->write();	

But this code creates resized copy of image.
How to make resized image as original image?
Thanks to all :)

Avatar
Johnny9

Community Member, 35 Posts

23 April 2015 at 10:16pm

No help?

Avatar
Devlin

Community Member, 344 Posts

24 April 2015 at 8:46pm

Edited: 24/04/2015 8:48pm

You could extend onAfterUpload() and quietly replace the original with the resized one. Something like:

class NotTestedResizeImageOnAfterUploadExtension  extends DataExtension {
	public function onAfterUpload() {
		$imgObj = $this->owner;
		$resizedImg = $imgObj->SetRatioSize(720,720);
		file_put_contents($imgObj->getFullPath(), file_get_contents($resizedImg->getFullPath());
	}
}

Avatar
Johnny9

Community Member, 35 Posts

24 April 2015 at 10:17pm

Great idea, will gonna try it :) Thanks!