Jump to:

5513 Posts in 1732 Topics by 1218 members

Customising the CMS

SilverStripe Forums » Customising the CMS » Image resize on upload

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

Page: 1
Go to End
Author Topic: 2497 Views
  • Ben_W
    Avatar
    Community Member
    80 Posts

    Image resize on upload Link to this post

    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!

  • Ben_W
    Avatar
    Community Member
    80 Posts

    Re: Image resize on upload Link to this post

    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" />

  • Aram
    Avatar
    Community Member
    598 Posts

    Re: Image resize on upload Link to this post

    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 %>

  • Ben_W
    Avatar
    Community Member
    80 Posts

    Re: Image resize on upload Link to this post

    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.

  • Ben_W
    Avatar
    Community Member
    80 Posts

    Re: Image resize on upload Link to this post

    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. ^_^

    2497 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.