Adding ability to custom format filename of generated thumbnails #1754

wants to merge 1 commit into


None yet
5 participants

dongilbert commented Dec 13, 2012

This PR adds a new $format parameter to the createThumbs method that allows you to specify the filename format to use when generating and saving thumbnails. It must contain the following three strings, which will be replaced when saving a thumbnail:

 * {filename} - replaced with original files name
 * {width} - replaced with width of generated thumbnail
 * {height} - replaced with height of generated thumbnail

// The default format
$format = '{filename}_{width}x{height}';

// Alternative format (that I use)
$format = '{width}x{height}_{filename}';

If any one of the three are missing, an InvalidArgumentException is thrown.


dongilbert commented Dec 13, 2012

I'm not convinced this is the best implementation, but the idea is definitely needed. If someone could come up with a better way to format the $format or to run a fancy preg_replace on it instead of my simple str_replace, let me know. I'll integrate it and update the PR.


pasamio commented Dec 13, 2012

I disagree with enforcing that you have to include all them. I can think of a few cases where I might not require the other two parameters:

  • If I'm dealing with squares and I know I'm always dealing with squares then one of the dimensions is redundant. Digital Blasphemy also only include a single number to identify their various image zips so it isn't an unlikely case.
  • To the extreme, the thumbnail directory may be a local specific to an individual image or path. Consider converting an avatar for a user into multiple formats and sticking that in a directory matching the required sizes. This was something that occurred at eBay for the DataHub where the source file was ignored entirely and thrown away once the thumbnails were generated (which were only files in a directory)

I feel it would be reasonable to at least enforce either the width or the height is required. You could scan the array to check that the selected on is unique though that's bordering a bit much. That does rule out a case where the user just wants to quickly resize and output a single thumb as well as the original though I'm not sure if that is a use case we care about - in which case requiring any of them at all is unnecessary. Fundamentally I feel if the user wants to instruct the system to overwrite the same file more than once then this isn't something that we necessarily need to protect against.


dongilbert commented Dec 13, 2012

Your comments make sense @pasamio - maybe instead of enforcing the min standard, we just provide clear documentation of what is available to use in the filename, and let them at it.

I was implementing the generateThumbs method on my own script just yesterday. Take a look at @phproberto's comment at the bottom of this gist ( for how I eventually went about using the method.

At the beginning I was wondering about the best way to use the generateThumbs method since saving to a file wasn't entirely clear, but after reading Roberto's comment and figuring out the implementation, I actually prefer doing it the way that he does. You can easily come up with a naming scheme that is pretty easy to use and doesn't require modifications to the method.

So - I'm not sure if the method really requires this change. Its fairly flexible the way it is. Maybe adding a predefined array with some options that are already set would help to prevent writing what I had to do above and make the method a lot easier to use / implement.


dongilbert commented Dec 13, 2012

@phproberto's comment there is basically a duplication of the createThumbs method. If this PR had already been implemented, you could have saved yourself the time by specifying the format when callingcreateThumbs.

dongilbert referenced this pull request Dec 13, 2012


JImage Docs #1753


phproberto commented Dec 13, 2012

The reason for having a generateThumbs method is to customize the way that thumbs are saved. In fact I originally only created the createThumbs method and someone rightly suggested this (I think it was @realityking ).

I understand what you are trying @dongilbert . I also thought in implementing it. But as @drmmr763 I finally decided that the library should only contain a standard method.

Any naming method will not probably fit the requirements of two different developers. So if someone needs a different one it can post-process the generateThumbs output or (the best way for me) extend the JImage class and create a customized createThumbs method.

Thanks for your work with this library Donald!


dongilbert commented Dec 13, 2012

Some good points there @phproberto.

The createThumbs method does a good job in it's own right (great work there) - it does most of what any developer using the library would desire. I actually only recently discovered it, having been implementing my own code to accomplish the same thing in the past. I think the most common use of overriding the method would be to change the format of the filename. While I agree that the platform should contain a standard method, don't you think it would be reasonable for it to support a simple override?

Also, after thinking more on it, I don't think modifying the format via a parameter in the createThumbs method is the best way to approach this. Maybe a protected var that can be overwritten by extending classes would be better?

class JImage
    protected $thumbFilenameFormat = 'STANDARD FORMAT';

Or, maybe abstract the filename creation to a createThumbFilename method?

But at that point, you're needing to extend the class, and you might as well override the createThumbs method.

I think I just talked myself out of the PR. :(

I'd be interested in seeing a method to handle it. It seems like a lot of extra steps to extend the class just so you can specify a custom naming method. If we had a method that did it by just passing a naming scheme, that would be simpler I think.


dongilbert commented Dec 13, 2012

So a setThumbFilenameFormat($format) method?

Yes. I'm thinking it could work chained in before using toFile? Something like:

$thumbs = $imgObj->generateThumbs(array('500','400')); $format = array('pretext', 'posttext', 'ext'); $setThumbFilenameFormat($format); foreach($thumbs as $thumb) { $thumb->toFile($path); }

disclaimer: I put very little thought into the design of this so I may be totally off in what I'm thinking. I'd trust just anyone else's expertise over my own here!


phproberto commented Dec 13, 2012

Think in most probable cases:


or K2 style:


I think that both deserve to extend the JImage class as you already have to define sizes correspondecies.

Having in mind the examples if I had to choose a solution I would probably use something simple like:

$thumbSizes = array( 
    'XS'  => '100x100',
    'S'   => '150x150',
    'M'   => '200x200',
    'L'   => '250x250',
    'XL'  => '300x300',
    'XXL' => '350x350'
$thumbs = createThumbs($thumbSizes);

This way the createThumbs method will replace the suffixes with the array KEYS. This method will work also for the mini, smal, etc. example.


eddieajau commented Mar 16, 2013

@dongilbert is this worth merging or shall we fix it in the FW?


dongilbert commented Mar 16, 2013

I'll put something together for the FW.

dongilbert closed this Mar 16, 2013

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment