Is it "good" to change an image file extension?
For instance, I have a jpg
file inputed, and I change it to a png
. Is this OK or should I leave it as a jpg
?
I use the rename()
to change the name and the file extension.
It is related to PHP, because I do my renaming with PHP with a upload script.
Another question is: Is it safe to do it e.g can the files become corrupt?
Changing an image file extension from .jpg
to .png
does not change it to a PNG file. It just changes the name of the file. This does not change the contents of the file at all, so it does not become corrupt. It only changes the name of it.
Leave file names with the appropriate extension, or you will confuse everyone, including yourself.
No, the file cannot become corrupt... a file name is just the name.
Never allow users to dictate the names of files on your system. No matter what they upload, rename it to something with no extension, and store the files outside of the web server's doc root. You don't want them uploading .php
scripts and what not.
Changing the extension of file is just renaming it. The extension serves the purpose of merely defining the data type that the file contains.
Renaming a JPEG file into a PNG is therefore a very bad idea. Although some image viewers may still be able to figure out that your PNG is actually JPEG and view it correctly, others will not.
Use ImageMagick or GD 2 to do any image conversions in PHP.