Hi all,
First the disclaimer, I do not consider myself a professional programmer in any sense, more the stubborn trial-and-error kind of guy. Anyhow, this filename thing was really bugging me, mainly for SEO purposes.
So I dug around some in the code. I usually try to avoid hacks as they can cause trouble at upgrades, but if you want to try the below it did what I wanted it to do for me. As usual, make back-ups of the files below before starting in case you mess up and need to revert.
So, enough with the disclaimers, here it goes (my K2 version by the way 2.4.1).
The basis for the whole thing is that I do not use the field Image Credits (found in K2 Item Editor -> Image) for anything, so decided to use that for file naming purposes. Also I do not have the lightbox function activated so I have not tested if that works with this hack.
1. in administrator/components/com_k2/models/item.php
Line 207, change to:
$handle->file_new_name_body = $row->image_credits;
2. components/com_k2/models/item.php
Starting on line 110, add the line:
$myfilename = $item->image_credits;
Then there are the following 16 lines below that which need to be modified to:
if (JFile::exists(JPATH_SITE.DS.'media'.DS.'k2'.DS.'items'.DS.'cache'.DS.$myfilename.'_XS.jpg')) $item->imageXSmall = JURI::root().'media/k2/items/cache/'.$myfilename.'_XS.jpg'; if (JFile::exists(JPATH_SITE.DS.'media'.DS.'k2'.DS.'items'.DS.'cache'.DS.$myfilename.'_S.jpg')) $item->imageSmall = JURI::root().'media/k2/items/cache/'.$myfilename.'_S.jpg'; if (JFile::exists(JPATH_SITE.DS.'media'.DS.'k2'.DS.'items'.DS.'cache'.DS.$myfilename.'_M.jpg')) $item->imageMedium = JURI::root().'media/k2/items/cache/'.$myfilename.'_M.jpg'; if (JFile::exists(JPATH_SITE.DS.'media'.DS.'k2'.DS.'items'.DS.'cache'.DS.$myfilename.'_L.jpg')) $item->imageLarge = JURI::root().'media/k2/items/cache/'.$myfilename.'_L.jpg'; if (JFile::exists(JPATH_SITE.DS.'media'.DS.'k2'.DS.'items'.DS.'cache'.DS.$myfilename.'_XL.jpg')) $item->imageXLarge = JURI::root().'media/k2/items/cache/'.$myfilename.'_XL.jpg'; if (JFile::exists(JPATH_SITE.DS.'media'.DS.'k2'.DS.'items'.DS.'cache'.DS.$myfilename.'_Generic.jpg')) $item->imageGeneric = JURI::root().'media/k2/items/cache/'.$myfilename.'_Generic.jpg';
3. When uploading a new file in the K2 Editor under Items -> Image in the backend, just make sure to put your filename in the "Item image credits" field (without any .jpg), for example: my_pretty_seo_image
Note: When I started this work I had not yet uploaded any images, so during testing I went in via FTP and erased all image files under media/k2/items/cache/, so I do not know how it works with overwriting existing images.
Now I am a bit mentally exhausted and in need of lunch so I might have forgotten some modification I made, but I hope the above is of some help to you.
/Markus