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.

General Questions

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

Solved: Reusing image file from Image.SetWidth


Reply

4 Posts   951 Views

Avatar
StuM

22 July 2010 at 9:56pm (Last edited: 22 July 2010 10:11pm), Community Member, 56 Posts

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?

Avatar
StuM

22 July 2010 at 10:10pm Community Member, 56 Posts

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

Avatar
delmi

4 August 2010 at 9:25am (Last edited: 4 August 2010 9:27am), 6 Posts

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

Avatar
delmi

4 August 2010 at 10:03pm 6 Posts

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."')");