7911 Posts in 1354 Topics by 930 members
DataObjectManager Module
SilverStripe Forums » DataObjectManager Module » DataObjectManager Image Dimension Validation
Discuss the DataObjectManager module, and the related ImageGallery module.
Moderators: martimiz, UncleCheese, Howard, Sean, Ryan M., biapar, Willr, Ingo, swaiba, simon_w
|
Page:
1
|
Go to End | |
| Author | Topic: | 534 Views |
-
DataObjectManager Image Dimension Validation

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
-
Re: DataObjectManager Image Dimension Validation

6 September 2011 at 7:31pm
Doesn't anyone know how to do this? - Chris
-
Re: DataObjectManager Image Dimension Validation

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 -
Re: DataObjectManager Image Dimension Validation

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
-
Re: DataObjectManager Image Dimension Validation

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 -
Re: DataObjectManager Image Dimension Validation

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
-
Re: DataObjectManager Image Dimension Validation

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();
}
| 534 Views | ||
|
Page:
1
|
Go to Top |

