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.

DataObjectManager Module /

Discuss the DataObjectManager module, and the related ImageGallery module.

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

[ImageGallery] do not increase the dimensions of images when resampling to NormalSize


Go to End


4 Posts   1824 Views

Avatar
codem

Community Member, 6 Posts

24 June 2010 at 1:40pm

Edited: 24/06/2010 1:42pm

Hi

I've used the ImageGallery module in a site where content is driven by a client. We had an issue with content managers uploading images that were smaller than "NormalSize" with the result that the resampled images were larger than the original image... resulting in poor quality "downsampled" images.

I couldn't find a solution to this via Google or config option, so came up with a simple solution to use SetSize, not SetWidth/SetHeight if the original image is smaller than NormalSize in the required dimension.

Code ( - is removed code, + is added code from my version control diff)

-                                       if($item->Image()->Landscape())
-                                               $normalImg = $item->Image()->SetWidth($this->NormalSize);
-                                       else
-                                               $normalImg = $item->Image()->SetHeight($this->NormalSize);
-
+                                       $normalImg = $this->normalImg($item);

and added a normalImg() method to ImageGalleryPage (to retain the default behaviour)

+       protected function normalImg($item) {
+               if($item->Image()->Landscape()) {
+                       return $item->Image()->SetWidth($this->NormalSize);
+               } else {
+                       return $item->Image()->SetHeight($this->NormalSize);
+               }
+       }

and then override this new method in my own page that extends ImageGalleryPage:

+       /**
+        * normalImg() override ImageGalleryPage normalImg handling
+        * @note if the image is less than the configured normal width set in the admin, then use the original image size
+        * @param $item the gallery image
+        */
+       protected function normalImg($item) {
+               $width = $item->Image()->getWidth();
+               $height = $item->Image()->getHeight();
+               if($item->Image()->Landscape()) {
+                       if($width < $this->NormalSize) {
+                               return $item->Image()->SetSize($width, $height);
+                       } else {
+                               return $item->Image()->SetWidth($this->NormalSize);
+                       }
+               } else {
+                       if($height < $this->NormalSize) {
+                               return $item->Image()->SetSize($width, $height);
+                       } else {
+                               return $item->Image()->SetHeight($this->NormalSize);
+                       }
+               }
+       }

Maybe this could be merged back into the core module code? A configuration option "do not upsize/downsample images" would be useful as well.

Code could be optimised if required but it works.

Thanks
James

Edit - should be some tabs in that code but the forum seems to be removing them

Avatar
UncleCheese

Forum Moderator, 4102 Posts

24 June 2010 at 3:35pm

Yeah, I've implemented solutions like this in the past, and I'll definitely look this over, but I think it comes down to a conversation you need to have with your client, too. A content management system isn't magic -- the old "garbage in, garbage out" adage still applies, and users need to understand that not just any random 220px image they scrape off their desktop is good enough for prime time. It's your company website, not a myspace page. Think it through!

I used to try to chase down every possible dumb thing a client could do with a CMS, but then I conceded that they're the owners of the site, and they need to take responsibility for its content. As developers, the onus is on us to educate the client, and train them in the application, but we can't child-proof everything.

Avatar
TotalNet

Community Member, 181 Posts

24 June 2010 at 4:16pm

Edited: 24/06/2010 4:17pm

"It's your company website, not a myspace page"

Classic. I will be using that one.

That one comment has made my day, thanks Uncle Cheese :)

EDIT: and what a good tone to have for my 100th post!

Avatar
codem

Community Member, 6 Posts

25 June 2010 at 2:38pm

I understand what you're saying, yes it's definitely not a myspace page and yes I have thought it through etc etc ;)

The issue is that if you set a "NormalSize" of say 800 pixels and someone uploads a 1000x1000 image then it's going to resample the image down correctly to 800 pixels. On the flip side if they upload an image of 450x450 pixels with perfectly good text rendering (for instance a bitmap of a map, architectural drawings) then the ImageGallery is going to increase the size by about 40% and downsample it in the process, resulting in illegible text and poor quality.
Why would a client upload a smaller image ? Maybe because they don't have access to larger images and they need to mix 1000x1000 with 450x450 images in the same gallery but with a max size of 800.

Of course there are limits, for instance if some uploads a 50x50 avatar to a 800x800 gallery then expect pain, but in other sensible cases like the one above, the image gallery really shouldn't upsize the image to the point of illegibility.

I can certainly "educate" a client but at the end of the day they can still do what they want. My solution gives a safety net such that the client won't call and ask "why has the image gone fuzzy?"