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're retiring the forums!

The SilverStripe forums have passed their heyday. They'll stick around, but will be read only. We'd encourage you to get involved in the community via the following channels instead:

Data Model Questions /

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

[Solved]Return an image object from filename

Go to End

5 Posts   4189 Views


Community Member, 148 Posts

7 April 2009 at 7:26pm

Edited: 08/04/2009 4:56pm

I have a site where I have a lot of product, all the information (and images) is exported from the company accounting system. All images are in the format [productcode].jpg .

In my SS site I import all the product objects directly into the database to make it easy on myself. In the front end I need to link the images to the product data objects....It's working fine, but I need to resize the image as I bring it in. at the moment I've got:

######### in Product.php ############

class Product extends DataObject
      function FindProductImage() { 
      $filename = 'assets/product-images/' . $this->ProductCode . '.jpg'; 
      $default_filename = 'themes/allentrading/images/no-product-image.png';
      if (Director::fileExists($filename)) { 
         $findimagepath = $filename; 
      }  else { 
         $findimagepath = $default_filename; 
      $data = array( 
         'ProductImagePath' => $smartimagepath 
      return $this->customise($data);        

In the template I call:

       <% control FindProductImage%> 
        <img src="$ProductImagePath" > 
        <% end_control %>

The problem is the images are all different sizes so I need to resize them in the template. What I ideally need to do is return the actual image object and then resize it (in the function)...not sure how to do that...or can I somehow do it in the template??

any help appreciated!


Community Member, 712 Posts

7 April 2009 at 8:34pm


The static method File::find($filename) should do the trick.

It will look for the filename in the database, so your asset files need to be synced with your database. This happens normally whenever you visit AssetAdmin and can also be manually kicked off with the call: FileSystem::sync().

Eg, if you have automated the image export/import process, call FileSystem::sync() after a batch. This should update your database. Then you can do:

function FindProductImage() { 
	return File::find('assets/product-images/' . $this->ProductCode . '.jpg');

and in your template:

<% if FindProductImage %>
<% else %>
	<img src='themes/allentrading/images/no-product-image.png' title='Not Found' />
<% end_if %>


Community Member, 148 Posts

7 April 2009 at 8:52pm

Great! that worked like a charm...thanks!

incidentally, is there any advantage to creating specific image sizes in the function, rather than doing a resize on the template??


Community Member, 712 Posts

7 April 2009 at 8:58pm

Edited: 07/04/2009 9:00pm

No problem.

I believe (but don't quote me) that the resulting template will refer to the cached resampled image anyway, so aside from the overhead of resizing the image the first time it is called, it should be exactly the same.

edit: by resulting template, I mean the ssviewer template generated from your .ss templates. The result should be equivalent


Community Member, 126 Posts

18 April 2009 at 1:49am

thought it's worth mentioning:
i just had a similar problem, but i was uploading the images through dataobjectmanager, but using 'File' instead of 'Image' in the $has_one relation, as i wanted to upload video files (flv) as well as images (jpg).
but as the File class doesn't define 'SetWidth' or similar functions, i couldn't resize the images in my templates.
i was looking for some sort of 'casting' my File object to an Image object, but didn't get far (oop primer).
this is what i ended up with:

	function myImagefile() {
		$file = DataObject::get_one('File','ID = '.$this->MediafileID);	
		$img = new Image_Cached($file->Filename);
		$img->ID = $file->ID; //image resize function requires an id
		return $img;

maybe it's of use for someone with the same problem.
if there is a more elegant way of doing this, i'd be happy to hear about it!