Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

encoding URLs incorrectly for characters "(" and ")" #34

Closed
nbl7 opened this issue Mar 14, 2018 · 4 comments
Closed

encoding URLs incorrectly for characters "(" and ")" #34

nbl7 opened this issue Mar 14, 2018 · 4 comments

Comments

@nbl7
Copy link

nbl7 commented Mar 14, 2018

I am having an issue with the URL encode.
If I have an image with a filename like this

some-name (1).png

the library generate a URL like this

some-name+%281%29.png

and this give me a 404 error in Imgix.

Does somebody know about this bug?

Thank you
nbl7

@jayeb
Copy link
Contributor

jayeb commented Mar 14, 2018

@nbl7 What version of the library are you using? In version 2.1.0 all paths are being encoded with rawurlencode(), which should encode spaces as %20 rather than as +, which I think is the error you're seeing.

@nbl7
Copy link
Author

nbl7 commented Mar 15, 2018

Hello @jayeb, thanks for your response.
I was using 1.2.1 version. Now I switched to 2.1.0 but I still see the parentheses encoded like before

Original image:
https://giannichiarini.com/smedia/gchiarini/bs-6309-rmn-re-bmu-marble-2884-jpg (1).jpeg

URL generated
https://nebula-7.imgix.net/https%3A%2F%2Fgiannichiarini.com%2Fsmedia%2Fgchiarini%2Fbs-6309-rmn-re-bmu-marble-2884-jpg%20%281%29.jpeg?auto=compress%2Cformat&ixlib=php-2.1.0&q=70&s=c011bdc152a8429724e44fd09ae07778

Thank you
nbl7

@jayeb
Copy link
Contributor

jayeb commented Mar 15, 2018

Ah, okay. When passing a fully-qualified URL into the createURL() method as the $path argument, the library is expecting that your input to be a legal, well-encoded URL. The example you're using here is not properly encoded -- the space character before (1).jpeg is not a legal URL characters and need to be encoded.

Unfortunately, PHP doesn't provide an easy function for encoding only illegal characters in a given URL -- the go-to rawurlencode() function encodes every non-alphanumeric character, which is definitely not what we want. There may be a third-party library out there that does this well, but here's a somewhat simplistic version of what you'd want to do:

function encodeFullURL($url)
  return preg_replace_callback("/([^\w\-\/\:@\(\\)\[\]!\?&~=])/", function ($match) {
    return rawurlencode($match[0]);
  }, $url);
}

If you run your example path through that function, you'll end up with this properly-encoded URL:

https://giannichiarini.com/smedia/gchiarini/bs-6309-rmn-re-bmu-marble-2884-jpg%20(1).jpeg

And now that this path is a legal URL, you can pass it through createURL() as a path, and you should get a properly-signed URL:

https://nebula-7.imgix.net/https%3A%2F%2Fgiannichiarini.com%2Fsmedia%2Fgchiarini%2Fbs-6309-rmn-re-bmu-marble-2884-jpg%2520%281%29.jpeg?auto=compress%2Cformat&ixlib=php-2.1.0&q=70&s=40e9f622e75fb725e550dc08ef99287a

Hope this helps! We prefer to use our GitHub issues for bug reports and feature requests rather than individual implementation and configuration problems, so I'm gonna close this ticket now. However, if you have any follow-up questions or additional issues please write in to support@imgix.com -- I'll make sure to scoop up any tickets I see from you right as they come in.

@jayeb jayeb closed this as completed Mar 15, 2018
@nbl7
Copy link
Author

nbl7 commented Mar 15, 2018

Thank you so much for your help @jayeb.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants