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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

General Questions /

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

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

Solved: Reusing image file from Image.SetWidth


Go to End


4 Posts   1541 Views

Avatar
StuM

Community Member, 59 Posts

22 July 2010 at 9:56pm

Edited: 22/07/2010 10:11pm

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

Community Member, 59 Posts

22 July 2010 at 10:10pm

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

Avatar
delmi

6 Posts

4 August 2010 at 9:25am

Edited: 04/08/2010 9:27am

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

6 Posts

4 August 2010 at 10:03pm

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