Jump to:

17452 Posts in 4473 Topics by 1971 members

Archive

SilverStripe Forums » Archive » Automatic Resizing of images uploaded on frontend.

Our old forums are still available as a read-only archive.

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

Page: 1
Go to End
Author Topic: 2600 Views
  • dio5
    Avatar
    Community Member
    501 Posts

    Automatic Resizing of images uploaded on frontend. Link to this post

    Hi,

    I let users upload images through the frontend and I want them to be resized immediately if they are bigger than 800x800.

    Now I thought I could use some of SS's methods like 'generateResizedImage()' but I'm a bit stuck with that.

    I get the original from the File-table based on an ID.
    So I have a 'image' dataobject. Now I want to replace the original with the resized.
    What I have already is this:

    $photo = DataObject::get_by_id("File", $tip->PhotoID);
                
    $width = $photo->getWidth();
    $height = $photo->getHeight();
    if($width > $height && $width > 800)
    {
    $newWidth = 800;
    $ratio = $width/$newWidth;
       $newHeight = $height/$ratio
    }
    if($width < $height && $height > 800)
    {
       $newHeight = 800;
       $ratio = $height/$newHeight;
       $newWidth = $width/$ratio;
    }
    if($newWidth && $newHeight)
    {
       $resized = $photo->generateResizedImage($newWidth, $newHeight);
    // do some stuff with the resized.. replace original with resized..?
    }

    Now there seems to be a parameter missing in the generateResizedImage(???, $width, $height), but there' s nothing about it in the docu:
    http://doc.silverstripe.com/assets/classes/default/Image.html#generateResizedImage

    I suppose it is $gd, but what is that $gd?

    If someone could hint me how to resize the original image using SS that would be great. Otherwise I'm just going to use some plain php...

  • dio5
    Avatar
    Community Member
    501 Posts

    Re: Automatic Resizing of images uploaded on frontend. Link to this post

    Ok, another approach:

    if($newWidth && $newHeight)
    {
    $GDImage = new GD("../" . $photo->Filename);
       $GDImage->resize($newWidth, $newHeight);
                   
    }

    Am I going in the right direction?

  • dio5
    Avatar
    Community Member
    501 Posts

    Re: Automatic Resizing of images uploaded on frontend. Link to this post

    Forget it, I solved it

  • fordy
    Avatar
    Community Member
    46 Posts

    Re: Automatic Resizing of images uploaded on frontend. Link to this post

    Hey dio5,

    could you post you solution for resizing on front-end upload?

    Cheers

    Ed

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