Insert image without knowing its id

Permalink
I've created a page for each of our employees, and I'm only missing the picture for each of them.

What I'd like to do is have C5 insert the appropriate image for each employee automatically.

I found this snippet somewhere on the forums:

$imageFile = File::getByID(123);
$relpath = $imageFile->getRelativePath();


which could solve my problem.. but I have no idea how to get the ID, when I only have the name of the picture to go on. I could find the ID manually in the file manager, but that would kinda defeat the purpose :)

In short - how do I get the ID of an image, when I know only the filename of the picture?

Any pointes would be greatly appreciated.

Thanks

 
JohntheFish replied on at Permalink Reply
JohntheFish
If your employees are each set up as a c5 user, you can add a user attribute to associate a file with each user. The attributes still need to be set for file association and you will still need to display the image from php.

If you do not want to write code to display the attribute/image, this is the sort of thing that is really easy using my Magic Data addon.

Magic data can also find and show files by name (as long as the name is unique), so as long as the image file names map exactly 1:1 to employee names (or page names), you should be able to have a common snippet of magic data included on every page that works it all out for you.
enlil replied on at Permalink Reply
enlil
@buntho
This is similar to a how-to i wrote on using Magic Data to integrate an appropriate State flag on every corresponding "State" page.

http://www.concrete5.org/documentation/how-tos/designers/integrate-...

You will find 100 other uses for Magic data once you have it, if this is the route you choose to go !!

EDIT: Being you simply need to pull the users avatar, use of Magic Data's "AVATAR" symbol would make this as simple as a small string of symbols in an html block!!
buntho replied on at Permalink Reply
Hi enlil,

Thanks for the reply. It does seem clever, but it still requires me to enter data on each page.

I'm looking for a more automated solution :-)
buntho replied on at Permalink Reply
Hi John,

I have no issues writing a bit of code (or more specifically - adopting and fiddling around with someone elses code).

Magic Data looks very interesting and I'll be sure to keep it in mind.

The image names map exactly to the page names, so it should be no problem at all.

I'm guessing the add-on can also get attributes for specific users and write them on the same page?
JohntheFish replied on at Permalink Reply
JohntheFish
In Magic Data it would be something like (for a 250 pixel image):
[% PAGE PAGE_NAME AS_FILE FILE_THUMBNAIL 250 %]

Or maybe:
[% PAGE PAGE_NAME . '.jpg' AS_FILE FILE_THUMBNAIL 250 %]

A robust solution may also need a message for failed matching:
[% PAGE PAGE_NAME AS_FILE ZERO_AS_THEN_END "no mugshot" FILE_THUMBNAIL 250 %]

There is also a symbol FILE_CROP_THUMBNAIL for square cropped images.

Internally, Magic Data looks directly at the FileVersions table in the database to backtrack a file name to a file ID.

For user attributes, a similar cross-matching of names
[% PAGE PAGE_NAME AS_USER DISPLAY_ATTRIBUTE "address" %]
[% PAGE PAGE_NAME AS_USER EMAIL AS_LINK %]
[% PAGE PAGE_NAME AS_USER AVATAR %]

If you needed a bunch of related information, you can optimise it by saving the user to memory:
[% PAGE PAGE_NAME AS_USER SAVE 'user_on_page' %]
[% 'user_on_page' RECALL DISPLAY_ATTRIBUTE "address" %]
[% 'user_on_page' RECALL EMAIL AS_LINK %]
[% 'user_on_page' RECALL AVATAR %]

So you could have a stack with the above sort of code identical on each page in your user directory. Maybe propagated by page type defaults.

As originally noted, you could use a page attribute rather than name to link the parts together, so avoiding the need for an exact name match. With a slightly more complex magic data expression, you could have it look for a page attribute first, then try the name match as a fallback, thus gaining the best of both.

The symbols documentation is online at
http://www.c5magic.co.uk/add-ons/magic-data/symbols-documantation/...

For the future, I have Uber List just submitted to the PRB for review. Its a Magic Data driven general purpose list that uses stacks and more Magic Data to template each list item. It would be great for a staff mugshot gallery.

http://www.concrete5.org/marketplace/addons/uber-list/...
(link will only work for PRB members until approved)
buntho replied on at Permalink Reply
Hey John,

I'm a bit sorry, that you typed all that. I did manage to make a solution:

<?php
$files = array();
      $fs = FileSet::getByName('Ansatte');
      $fileList = new FileList();            
      $fileList->filterBySet($fs);
      $files = $fileList->get();
      //Find medarbejdernavn ud fra URL
      $employee_name = explode("/",$_SERVER['REQUEST_URI']);   
      //Ret navn til fornavn_efternavn.jpg
      $navn = strtolower(str_ireplace("-", "_", $employee_name[3]).'.jpg');
      foreach ($files as $file)
      {
         $file_name = strtolower($file->getFileName());
         if($file_name == $navn)
         {


Probably not the prettiest code in the world, but it works, as long as the url matches the image filenames, it should work just fine.

In the long run, Magic Data would be better, but for now this will have to do :-)
JohntheFish replied on at Permalink Reply
JohntheFish
I actually started out doing something like that with added caching for speed before I decided it would be easier (and for long lists much more efficient) to interrogate the database directly.