Closed piaaaac closed 4 years ago
The browser may cache images not by "name" (whatever you mean with that), but by its URL. So, you should have an unique URL for every image, and play with HTTP headers to tell the browser to cache the returned image.
Thanks for your answer. By "name" I mean the filename I see when I download the generated image.
you should have an unique URL for every image, and play with HTTP headers to tell the browser to cache the returned image
Could you point me in the right direction to do that?
By "name" I mean the filename I see when I download the generated image.
You could try to use the Content-Disposition
header, something like this (before sending the image to the client):
header('Content-Disposition: inline; filename="wonderful-image-name.jpg"');
Could you point me in the right direction to do that?
Sure! See here :wink:
😅 really?
You could try to use the Content-Disposition header, something like this (before sending the image to the client)
Thanks
Is there a way to add a header to the one sent by $image->show()
or do I have to send the image manually?
// from src/Gd/Image.php
public function show($format, array $options = array())
{
header('Content-type: ' . $this->getMimeType($format));
// additional 'Content-Disposition' header here <<<<
$this->saveOrOutput($format, $options);
return $this;
}
Or, looking at the code of saveOrOutput()
I wonder if there's a way to add the filename (that would correspond to the 3rd parameter) when it's executed by $image->show()
, which was my original intention.
private function saveOrOutput($format, array $options, $filename = null)
{
$format = $this->normalizeFormat($format);
// ...
That would overcomplicated the code... which disposition, inline or attachment? What if the file name is not available, or the original file has a jpg extension but you are serving a png file... The easiest solution is to add headers on your own
I solved like follows:
1 - Save the file using Imagine:
$image->save($outputPath, ['jpeg_quality' => $q]);
2 - Serve the image using a separate function that reads the file again and sends it with the Content-Disposition
header you suggested:
// Function
function serveJpegFromFile ($path) {
$parts = explode("/", $path);
$filename = array_pop($parts);
$img = imagecreatefromjpeg($path);
header("Content-type: image/jpeg");
header("Content-Disposition: inline; filename=$filename");
imagejpeg($img);
}
// Serve
serveJpegFromFile ($outputPath);
Still wondering, isn't this a pretty common case? It would be handy to specify the filename directly in $image->show(…)
.
Anyway, thanks for your help!
What I want to accomplish is very simple: add an image name to the served image. I made a simple script named
thumbs.php
that uses Imagine to create the thumbnail with a unique name into athumbs
folder and serves it using $image->show('jpg'). If the thumb has already been generated the file is used directly from the thumbs folder.thumbs.php
looks like thisRight now all the images are called thumbs.jpg and I think the browser can't cache them. Is this possible? I browsed the docs carefully and didn't find anything.
Additional info