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.

DataObjectManager Module /

Discuss the DataObjectManager module, and the related ImageGallery module.

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

DataObjectManager Image Dimension Validation


Reply


7 Posts   792 Views

Avatar
chrisabey

Community Member, 11 Posts

2 September 2011 at 1:19am

I was wondering if anyone knows how to check if an image being uploaded with the DataObjectManager/Uploadify modules is of the correct dimensions and only successfully upload it if it is, otherwise reject it and alert the user?

I have a banner image on my page which randomly selects a handful of images from a folder and displays them using the jquery cycle plugin. However, for it to work correctly, the images need to be a specific dimension. It is a photography website so the photos have to be resized in photoshop beforehand as resizing/resampling/cropping the images in the controller or template does not give me the result I want.

I would really appreciate it if anyone in the forums could help me out.

Cheers - Chris

Avatar
chrisabey

Community Member, 11 Posts

6 September 2011 at 7:31pm

Doesn't anyone know how to do this? - Chris

Avatar
UncleCheese

Forum Moderator, 4096 Posts

7 September 2011 at 3:15am

I would use a validate() function on your DataObject and check $this->YourImage()->getWidth() and $this->YourImage()->getHeight(). If it doesn't fit the spec, throw a validation error.

--------------------
SilverStripe tips, tutorials, screencasts and more: http://www.leftandmain.com

Avatar
chrisabey

Community Member, 11 Posts

8 September 2011 at 9:05pm

Thanks for getting back to me UncleCheese..... How do I get it to throw a validation error? I've tried the testing for the width and height with the following code:

   public function validate() {
      if($this->myImage()->getWidth() != 930 || $this->myImage()->getHeight() != 330){
          /*Throw Validation Error*/
      }

But it doesn't like that!.... any help would be appreciated

- Chris

Avatar
UncleCheese

Forum Moderator, 4096 Posts

9 September 2011 at 2:06am

Return a ValidationResult http://api.silverstripe.org/2.4/sapphire/core/ValidationResult.html

--------------------
SilverStripe tips, tutorials, screencasts and more: http://www.leftandmain.com

Avatar
chrisabey

Community Member, 11 Posts

9 September 2011 at 2:12am

Hi UncleCheese.... I sort of got it working using the onBeforeWrite() function

public function onBeforeWrite() {
      
      if(($this->myImage()->getWidth() != "930") && ($this->myImage()->getHeight() != "330")){

         exit();         
      }
      parent::onBeforeWrite();
   }

Now this works as I can upload an image with the correct dimensions successfully but uploading a small image returns a blank popup so I assume I'm heading in the right direction..... still lost regarding displaying an error message and closing the popup.

- Chris

Avatar
chrisabey

Community Member, 11 Posts

9 September 2011 at 3:30am

Right - Got it working! Here is the working code in case anyone wants to use it

public function onBeforeWrite() {
      
      if(($this->myImage()->getWidth() != "930") && ($this->myImage()->getHeight() != "330")){
      die ('<script type="text/javascript">alert("Sorry! Those Image Dimensions Are Invalid. Please Try Again");history.back();</script>');      
      }
      parent::onBeforeWrite();
   }