glide
glide copied to clipboard
Image names - rawurlencode() or not?
My application allows users to upload images, and also to choose the image subfolder and filename.
One such image is called foo%20bar.jpg.
Here's a simplified version of the code I'm using. These are symfony/httpfoundation requests and responses. $this->glideServer() simply calls ServerFactory::create() with appropriate parameters.
$params = $request->query->all();
$server = $this->glideServer($media_file->folder());
$path = $server->makeImage($media_file->filename(), $params);
return new Response($server->getCache()->read($path));
The problem is that Server::makeImage() calls Server::getSourcePath(), which in turn calls rawurldecode() on the path. This converts the filename to foo bar.jpg - which does not exist.
Now, ServerFactory::getSourcePath() also checks whether the path contains $this->baseUrl as a prefix (and strips it off). It does this before calling rawurldecode().
This means that for the code to work, I must rawurlencode() both the folder and the file names before passing them to the library.
IMHO, URL-decoding does not belong here at all, and it ought to be removed from https://github.com/thephpleague/glide/blob/1.3.0/src/Server.php#L154
But if these functions really do require urlencoded data, then this ought to be clearly documented.
FYI, my workaround for this bug is to rawurlencode() each segment of the file path before asking glide to create the image.
// Yes, the file *does* contain a % character in the name...
$file = 'path to/folder/foo%20bar.jpg';
// Encode each segment of the filename
$file = implode('/', array_map('rawurlencode', explode('/', $file)));
// Generate image
$path = $server->makeImage($file, $params);