Jump to:

7939 Posts in 1472 Topics by 944 members

DataObjectManager Module

SilverStripe Forums » DataObjectManager Module » DataObjectManager Image Dimension Validation

Discuss the DataObjectManager module, and the related ImageGallery module.

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

Page: 1
Go to End
Author Topic: 678 Views
  • chrisabey
    Avatar
    Community Member
    11 Posts

    DataObjectManager Image Dimension Validation Link to this post

    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

  • chrisabey
    Avatar
    Community Member
    11 Posts

    Re: DataObjectManager Image Dimension Validation Link to this post

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

  • UncleCheese
    Avatar
    4085 Posts

    Re: DataObjectManager Image Dimension Validation Link to this post

    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

  • chrisabey
    Avatar
    Community Member
    11 Posts

    Re: DataObjectManager Image Dimension Validation Link to this post

    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

  • UncleCheese
    Avatar
    4085 Posts

    Re: DataObjectManager Image Dimension Validation Link to this post

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

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

  • chrisabey
    Avatar
    Community Member
    11 Posts

    Re: DataObjectManager Image Dimension Validation Link to this post

    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

  • chrisabey
    Avatar
    Community Member
    11 Posts

    Re: DataObjectManager Image Dimension Validation Link to this post

    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();
       }

    678 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.