I have script showing profile pictures of users from my site but it’s hardcoded to only show jpg, problem is that users can upload png and gif as well and when that happens their picture is not displayed, how can I fix this, not changing this line too much as it’s part of the bigger thing and it can’t be all reworked just for this one line?
I think the correct approach to this problem would be to use something like createimagefrompng to convert uploaded images to jpg. This would keep everything consistent.
However, if you can’t do that then a more hacky approach would be to check if the file exists as a jpg, then png, then gif…so that line above would be turned into something like:
Well I think we’re close here but there’s no need for the last bit as I have default pic for users, perhaps I should’ve included the whole piece of the code responsible for showing the pic.
Another quick hacky “fix” would be to prevent them from uploading anything other than .jpg files, if the site can only display profile pictures in that format. Really, whoever added the upload code should be anticipated the situation, knowing that the output is a particular format. As labofoz said above, you could just convert them during the upload process, too.
Another option would be to modify your function to retrieve the last-modified date/time of the images, and use the most recent one.
You’d have to check whether the file date/time is set when the file is uploaded, or whether the original date/time from the users computer is retained - I can’t remember how it works. As the $_FILES array doesn’t seem to contain a date/time entry, I would guess it gets a new one.