I have taken over a CodeIgniter PHP site that dynamically generates resized images and thumbnails.
Here is an example of an img tag in the site that generates a 100px by 100px thumbnail:
<img src="/media/image/ImageName.jpg/100/100" />
This is generated by using the helper:
echo img('media/image/ImageName.jpg/100/100');
There is some logic in the image function in the media controller, which first checks a cache folder and then serves the cached image if it exists.
It still seems that this is not an elegant approach and looking at the way other PHP frameworks like SilverStripe do it, it would be better to do something like this:
echo img_link('ImageName.jpg', 100, 100);
This new img_link function in a helper would check for the cached image first and write the link directly to the image file or generate the new image and pass the direct link to the html.
My thoughts are that if PHP is serving every img using this type of code
$this->output->set_content_type('jpeg')->set_output(file_get_contents($file_path));
it will always be inefficient.
Thoughts anyone?
I agree, I have done exactly what you are proposing (writing the link directly if it is already cached) and it works fine.
The only time I can think you would get caught out is if your html is cached and you have deleted the cached image, so the direct link to the imagefile is now broken.
In my case I have never had to remove the cached images, so have not struck that yet.
The only other case where I can see it being a problem is if you have private images. In that case php would need to check who has permission first before displaying the image, you couldn’t just serve up the cached file as a directlink, as people could share that direct link, and the security would no longer apply.
If you do need to do that, and you are able to install modules on apache (assuming you are using apache), there is a module “x-sendfile” which will let you handover the file delivery process back to apache, instead of via php. This is good for big files where php could timeout, etc