Jump to:

10844 Posts in 2513 Topics by 1792 members

All other Modules

SilverStripe Forums » All other Modules » DataObjectManager - resize image at creation by input value

Discuss all other Modules here.

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

Page: 1 2
Go to End
Author Topic: 3049 Views
  • Allisone
    Avatar
    Community Member
    27 Posts

    DataObjectManager - resize image at creation by input value Link to this post

    Is there a nice way to resize the image that I upload ?

    I would like to use an input field (next to Name and describtion), where I can set the width, so that after the image got uploaded, and after I filled the image input fields (in the following dialog), it automatically gets resized to that width and saved as the main image (not thumbnail or so).

    Thanks in advance.

  • UncleCheese
    Avatar
    4085 Posts

    Re: DataObjectManager - resize image at creation by input value Link to this post

    Of course.. let's say you put two properties on your ImageObject "ImageWidth" and "ImageHeight".. you could then write a function on the page containing that image:

    function ResizedImage()
    {
    return $this->Image()->CroppedImage($this->ImageWidth, $this->ImageHeight);
    }

    and on your template:

    <img src="$ResizedImage.URL" alt="" />

  • Allisone
    Avatar
    Community Member
    27 Posts

    Re: DataObjectManager - resize image at creation by input value Link to this post

    If I understand you correctly, you are suggesting to always resize the image (on demand) when it is used somewhere... ?

    I thought there would be a way to make this happen once, right after the image(/images) have been uploaded, in this multi-page dialog (Editing file 1 of X), that comes after pressing the upload button, or directly in the "Add Images"-dialog (before you click on the blue upload button)...

    For now, I have written a function that lets me check from within the template if an uploaded image is wider than what I want. If so, I set a css width value, so that the browser can reduce the width. But since I exactly know, how big the width must be for all images that I upload ( < 700px ), it would make more sence to simply reduce the file size on uploading, wouldn't it... ?

    For those who stumble across this post, wondering how I did it, here is my code...

       <% control ReferenzenBild.Bild %>         
                <% if isWiderThan(700) %>
                   <img src="$URL" style="width: 740px" />
                <% else %>         
                   <img src="$URL"/>   
                <% end_if %>                        
             <% end_control %>

    ReferenzenBild is what you call 'Resource', // Source class in your ResourcePage
    Bild is what you call 'Attachment' => 'File', in your Resource
    Instead of File I use ImageExtended, a class that extends Image containing my isWiderThan function
    With "your" I mean what I read in http://doc.silverstripe.com/doku.php?id=modules:dataobjectmanager

       public function isWiderThan($width = 740){
          return ($this->getWidth() > $width);
       }

    By the way, it's so good, that you programmed this module, I was looking for something like that since my first contact with silverstripe (half a year).
    Thank you !!!

  • UncleCheese
    Avatar
    4085 Posts

    Re: DataObjectManager - resize image at creation by input value Link to this post

    Thanks for the compliment! Glad it's working for you.

    As a matter of best practice, Silverstripe does not resize on upload. The original image that you upload is always kept in tact. When you resize the image on the template, it duplicates the image and resamples it, so that it can retrieve it easily the next time. If that ever changes, it will cut a new version, but it will never alter the integrity of your original image.

  • Allisone
    Avatar
    Community Member
    27 Posts

    Re: DataObjectManager - resize image at creation by input value Link to this post

    I'm not quite sure I understand, do you mean it remembers my resizing (the return $this->Image()->CroppedImage($this->ImageWidth, $this->ImageHeight); ) so that next time or each next time he loads, he won't resize again, but take the image, that was resized the very first time, only ?
    because I don't want him to resize each time... I know its quite fast, I programmed a Banner editor that does a really mega mega mega huge amount of work (and it's animated too) and all work is done in 1 second, but still if it's possible you never want to do the same work twice (or have the same operation twice).

  • UncleCheese
    Avatar
    4085 Posts

    Re: DataObjectManager - resize image at creation by input value Link to this post

    That's correct. It caches the resampled image in the _resampled folder and provided the dimensions never change on your template, it will never resize again. Once you make a change, however, the first time the page loads, it will cut a new image to the _resampled folder with the new dimensions.

  • Allisone
    Avatar
    Community Member
    27 Posts

    Re: DataObjectManager - resize image at creation by input value Link to this post

    Thank you very very much for this imformation

  • Howard
    Avatar
    Community Member
    215 Posts

    Re: DataObjectManager - resize image at creation by input value Link to this post

    Hi guys,

    I realise that it is normal practice to not destroy the integrity of the source image but I am building a site at the moment that has a lot of admins all uploading photos from events and you just know that there will be those who upload the 3mb images directly off their camera. I'm not keen on having all that space used (hundreds of photos) on the server so is there a way to do what he is originally asking? ie. upload, if larger than x resize to 800x600 and save.

    3049 Views
Page: 1 2
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.