This morning I was woken up by a call from a client running a Concrete5 website, saying that any images uploaded to their website suddenly stopped working. After an hour or two of experimentation and study I realized the root cause of the issue. A couple days ago this client got a new cellphone (A Samsung Epic) which records GPS EXIF data to every image. She was using this cellphone as her camera and for whatever reason if there is any GPS data in the image then Concrete5 explodes. I suspect this is because GPS data is stored as an array rather than a string.
I've been trying to work on a generic function to remove any EXIF data if GPS EXIF data exists. Here's what I have so far (this is located within tools/files/importers
):
$fileExtension = strtolower(substr($_FILES['Filedata']['name'], strrpos($_FILES['Filedata']['name'], '.')));
if ($fileExtension == ".jpg" || $fileExtension == ".jpeg"){
/* File is a JPEG */
if (exif_read_data($_FILES['Filedata']['tmp_name'], 'GPS') !== false){
/* File contains GPS EXIF data */
if (extension_loaded('magickwand') && function_exists("NewMagickWand")) {
/* ImageMagick is installed and working */
$img = new Imagick($_FILES['Filedata']['tmp_name']);
$img->stripImage();
$img->writeImage($_FILES['Filedata']['tmp_name']);
$img->clear();
} elseif (extension_loaded('gd') && function_exists('gd_info')) {
/* GD is installed and working */
$img = imagecreatefromjpeg($_FILES['Filedata']['tmp_name']);
imagejpeg ($img, $_FILES['Filedata']['tmp_name']);
} else {
/* Neither ImageMagick nor GD are installed */
die('No appropriate image handler to remove EXIF data');
}
}
}
Even with this code in place, when I upload an image the EXIF data remains. I have tried placing die()
commands throughout the code to figure out exactly which path the code is taking. On this particular server I am never entering the if (extension_loaded('magickwand') && function_exists("NewMagickWand"))
block, however I do enter the elseif (extension_loaded('gd') && function_exists('gd_info'))
block. Therefore my ImageMagick code may be just fine, but it seems that GD is not removing the EXIF data properly.
Does anyone have experience with ImageMagick/GD or with PHP and EXIF data?