If the user uploads a JPEG file, you could do something like this to reprocess it:
$newIm = @imagecreatefromjpeg($_FILES['file']['tmp_name']);
if (!$newIm) {
// gd could not create an image from the source
// most likely, the file was not a valid jpeg image
}
You could then discard the $newIm
image using imagedestroy() and use the uploaded file from the user, or save out the image from GD and use that. There could be some issues with saving the GD image as it is not the original image.
Another simple method would be to check the header (first several bytes) of the image file to make sure it is correct; for example all JPEG files begin with 0xff 0xd8
.
See also imagecreatefromstring(), and you can also use getimagesize() to run similar checks on the uploaded image.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…