|
Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon. |
|
View previous topic :: View next topic |
Author |
Message |
Spinacia Smarty n00b
Joined: 01 Nov 2017 Posts: 1
|
Posted: Wed Nov 01, 2017 7:36 am Post subject: Seeking clever way for code to work with GIF and JPG |
|
|
Hi,
The following code is rather simple and self explanatory:
Code: | $config = cmsms()->GetConfig();
$files = glob($config['image_uploads_path'].'/home/home*.jpg');
$num_images = count($files);
$month = date('m');
$changes = (int)(($month-1));
$myimage = $changes % $num_images + 1;
$smarty->assign('homeautoimage', "$myimage"); |
The code is now obviously set only for JPG files. I would like to modify it to simultaneously work with both GIF and JPG files.
I am looking for a clever way for this code to first look for and load a GIF file, and if the GIF does not exist then load the corresponding JPG file.
For example, if the home5.gif file exists then the code should load it. If the home5.gif file does not exist then the code should load the home5.jpg file.
Using {if file_exists(...)} {/if} is valid but was hoping for an optimally fast and efficient way...
All suggestions are most welcome.
Thanks. |
|
Back to top |
|
AnrDaemon Administrator
Joined: 03 Dec 2012 Posts: 1785
|
Posted: Wed Nov 01, 2017 1:59 pm Post subject: |
|
|
http://php.net/glob
This is not a Smarty question. |
|
Back to top |
|
bsmither Smarty Elite
Joined: 20 Dec 2011 Posts: 322 Location: West Coast
|
Posted: Wed Nov 01, 2017 6:48 pm Post subject: |
|
|
This can certainly be done in the PHP core code, but you are asking for a Smarty template code solution. So, I assume making changes to the core code is not possible. Therefore, the target file has already been determined in the core code and cannot be changed.
But this may be interesting: Code: | {$globbed_files = glob("{* real path to the image directory *}{$home_image|basename:".jpg"}{literal}{.jpg,.gif}{/literal}",GLOB_BRACE)}
<img src="{* HTML path to the image subdirectory *}{$globbed_files[0]|basename}"> | The above represents the idea, and may not be actual workable code.
Is it optimal and efficient? Hardly. Is it clever? That depends on what you have already tried, among other considerations.
Can you guarantee that the desired file will be at index zero? Considering gif is alphabetically before jpg, and the glob() function defaults to a sorted list, you could get gif before jpg.
The next potential obstacle is when there is no gif file, and there is no jpg file as a backup. Then, $globbed_files will be empty. Smarty has a default modifier where you can specify a known-to-exist filename as a second backup. |
|
Back to top |
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|