The url-encoded space is represented by the string "% 20" so you can use str_replace to replace each instance of the character "" with the character "% 20"
echo str_replace(' ', '%20', 'http://domain.com/folder/blue sky.png');
displays
http://domain.com/folder/blue%20sky.png
Additional Information
Also, I never used it myself, but I would look at the urlencode php function, if I were you, this might contain useful information
Note: Url encode converts all characters that are not standard for the string (so you can only use urlencode in the string, which is your image name)
Hipny source share