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.

Customising the CMS /

Image resize on upload


Reply


5 Posts   2569 Views

Avatar
Ben_W

Community Member, 80 Posts

3 April 2009 at 2:13pm

hi, I would like to resize image when user upload a image through cms. I have done my readings and tried to find relative post in the forum. The following is what i have found, but none of them actually solve my problem.

http://doc.silverstripe.com/doku.php?id=imageupload
http://doc.silverstripe.com/doku.php?id=gd
http://doc.silverstripe.com/doku.php?id=simpleimagefield
http://doc.silverstripe.com/doku.php?id=image&s=setwidth

I follow the first article as it is very close to what I want, 'http://doc.silverstripe.com/doku.php?id=imageupload'. Yet something is off, and my script does not work. The following is what I have written.

class AboutUsPage extends Page {
   static $db = array(
      'Headline' => 'Varchar(255)'
   );
   static $has_one = array(
      'AboutUsImage' => 'AboutUsPage_CustomeImage'
   );
   
   
   function getCMSFields() {
      $fields = parent::getCMSFields();
      
      
      $fields->addFieldToTab('Root.Content.Main', new TextField('Headline'), 'Content');
      
      $fields->addFieldToTab("Root.Content.Images", new ImageField('AboutUsImage','Image'));
      
      return $fields;
   }
}

class AboutUsPage_Controller extends Page_Controller {

}

class AboutUsPage_CustomeImage extends Image {
   function genertateCustomeImage($gd){
      return $gd->resizeByWidth(259);
   }

}

Could someone point me to the right direction please?

Thank you for taking your time to read this post!

Avatar
Ben_W

Community Member, 80 Posts

3 April 2009 at 2:21pm

I forgot to mention that, by doing so, the database is all linked correctly, I have record in File table that points to 'AboutUs' table, that's all good, only that image uploaded is not resized. what needs to be called in the template? I have tried following but it failed.

<img src="$AboutUsImage.CustomeImage.URL" />

Avatar
Carbon Crayon

Community Member, 598 Posts

3 April 2009 at 9:17pm

Hi Silverfish

Try something like this;

<% control AboutUsImage %>
<% control CustomeImage %>
<img src="$URL" />
<% end_control %>
<% end_control %>

or

<% control AboutUsImage %>
<img src="$CustomeImage.URL" />
<% end_control %>

Avatar
Ben_W

Community Member, 80 Posts

6 April 2009 at 12:17pm

Hi aram

Thank you for the reply! It makes sense. However, it does not seem to work for me.
With the first method, it seems that <% control CustomeImage %> does not exist, if I remove this tag and end tag, the AboutUsImage shows.

With second method, nothing is in the src.

I have double checked the file permission on the server and give 777 to the assets/ and its sub directories. Images appears to be stored in the 'File' table, and has ClassName of 'AboutUsPage_CustomeImage', but the image is in its original size.

Avatar
Ben_W

Community Member, 80 Posts

6 April 2009 at 12:54pm

Hi aram,

Quick correction. It worked.
The reason why it did not work in the first place is 'typo', I am very embarrassed, and sorry to waste anybody's time, especially aram.

I revisited image class and thinking it should work, the 'defineMethods' should pick up my custom function, and then I noticed my misspelling, it is shown in my previous post, 'genertate'.

It is always at the back of my head, it should work, I had followed everything in the documentation, somehow I fear it would be something silly, and indeed, it was one of these days. Sorry guys. ^_^