Jump to:

23361 Posts in 18124 Topics by 2862 members

General Questions

SilverStripe Forums » General Questions » Solved: Reusing image file from Image.SetWidth

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

Page: 1
Go to End
Author Topic: 930 Views
  • StuM
    Avatar
    Community Member
    56 Posts

    Solved: Reusing image file from Image.SetWidth Link to this post

    Hi All,

    I'm creating a basic gallery for a site, where the admin of the site just uploads images into a specific folder, and they're displayed on a page. I don't the site owner to have to do anything else.

    I've attempted using the Image.SetWidth to generate thumbnails, that works okay and a resized image is saved in assets which is fine. I then expect it to reuse it, but it doesn't, it regenerates the thumbnail everytime the page is viewed. The test server I'm using is slowing down to a crawl every time I view the page, the final site will be on shared hosting as well, and I don't want this page to interrupt other clients. Is there a way to tell it to look for an already resized version before regenerating thumbnails?

  • StuM
    Avatar
    Community Member
    56 Posts

    Re: Solved: Reusing image file from Image.SetWidth Link to this post

    doh, I read through the source code, it was because '?flush=1' was in the URL

  • delmi
    Avatar
    6 Posts

    Re: Solved: Reusing image file from Image.SetWidth Link to this post

    Hi StuM,
    I'm very new to SS and I have implemented a basic image gallery similiar to your.
    It works well and I would like to share my code with you. I don't know if my code is ok.. mabe could be better..

    Apartments.php

    <?php
    class ApartmentsPage extends Page {
       static $db = array();
       static $has_one = array();

       function Galleria() {
          $object = new stdClass();
          $path="../assets/Appartamenti/".$this->Title."/";
          if ($handle = opendir($path)){
             $path = substr($path,3);
             $i=0;
             while(false !== ($file = readdir($handle))) {
                if(strpos($file,"jpg")!=false){            
                   $object->$i = File::find($path.$file);
                   $i++;
                }
             }
          }   
          return $object;
       }
       

    }

    class ApartmentsPage_Controller extends Page_Controller {

    }
    ?>

    ApartmentsPage.ss

    <% control Galleria %>
       <span style="float: left;padding: 3px;margin-bottom: 3px;"><a href="$URL">
       <% control CroppedImage(100,100) %>
          <img src="$URL" />
       <% end_control %>
       </a></span>
    <% end_control %>

    I'm "reading" all jpg files inside Assets/Appartamenti/<Page Title>/ , creating the object of the reading result and passing it to the control on the ss template.
    Don't know if is ok or not... but it works..

  • delmi
    Avatar
    6 Posts

    Re: Solved: Reusing image file from Image.SetWidth Link to this post

    Just learned something new and definitely better:

    return DataObject::get("Image", "ClassName <> 'Folder' AND ParentID = (SELECT ID FROM File WHERE ClassName = 'Folder' AND Name = '".$this->Cartella_Immagini."')");

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