I have a script that resizes images and then save the new images.
I can't save the new image if the original image didn't have 777 permissions.
I know that 777 is risky, so when using different permissions such as 775 or 755 it does nothing.
EDIT:
I want to be able to save the files using my script no matter what the permission of the IMAGE is/was.
It all because your server is badly configured, which means httpd runs as different user than owns all the files and this requires write
permission set for others
. The solution would be to fix the server configuration so these user ids match. But it's not trivial if you are not familiar with the server administration. Other (but this is not really a solution) would be to to put these two users into one group, so that way instead of giving everyone write access you "limit" it to your group only. But this is not a way to go though. Alternatively, if you are the only user on the server you may set httpd to run on your userid/groupid instead of its own. But, again, this should not be considered a "solution".