Basically, I want users to be able to share and upload images. So I've concocted this as a first attempt to to make sure they're not sending anything harmful. I'll step through it, but for reference, I put the whole thing on Pastebin ** EDIT That's the original file - the newest version is Here**:
First, we accept the file and copy it to /home/myapp/uploads. To be clear, the app is served from /home/myapp/public/, so the initial file upload is not in a path that can be accessed by the outside world. We also give it a random name, which is the SHA1 hash of the file, and the current file extension.
$upload = $_FILES['file']['tmp_name'];
$fileName = $_FILES['file']['name'];
$ext = pathinfo($fileName, PATHINFO_EXTENSION);
$uploadPath = '/home/myapp/uploads/'; // Initial directory image is sent to
// move uploaded file to uploads directory
$hash = sha1_file($upload);
$newfile = $hash . "." . $ext;
move_uploaded_file($upload, $uploadPath . $newfile);
Now, we want to make sure that the files we received were actually images.
// Validate the file we received - step one
$imageSize = getimagesize($uploadPath . $newfile);
if($imageSize == false) {
echo "Not an image";
exit();
}
In the config file elsewhere, MAX_IMAGE_SIZE is set to 500, which is the largest dimension (in pixels) a final image should have. If it's larger than 500pixels per side, it should be scaled down proportionately. Note that even if the image is less than 500 pixels per side, we still want to resize it at 100% just to rewrite the file and strip out anything bad (hopefully).
$imageWidth = $imageSize[0];
$imageHeight = $imageSize[1];
$imageType = $imageSize['mime'];
if($imageWidth < MAX_IMAGE_SIZE && $imageWidth < MAX_IMAGE_SIZE) {
$percentage = 1;
} elseif ($imageWidth > $imageHeight) {
$percentage = (MAX_IMAGE_SIZE / $imageWidth);
} else {
$percentage = (MAX_IMAGE_SIZE/ $imageHeight);
}
$newHeight = round($imageHeight * $percentage, 0);
$newWidth = round($imageWidth * $percentage, 0);
Now, rather than looking at the extension, we look at the mime type of the image and figure out how to create an image out of it. At this point, we only accept GIF, JPEG and PNG's
// create image
switch($imageType)
{
case 'image/jpeg':
$img = @imagecreatefromjpeg($uploadPath . $newfile);
break;
case 'image/gif':
$img = @imagecreatefromgif($uploadPath . $newfile);
break;
case 'image/png':
$img = @imagecreatefrompng($uploadPath . $newfile);
break;
default:
echo "not a supported image type";
exit();
break;
}
$destImage = imagecreatetruecolor($newWidth, $newHeight);
imagecopyresampled($destImage, $img, 0, 0, 0, 0, $newWidth, $newHeight, $imageWidth, $imageHeight);
After we've created a new image in memory, we copy it to the publicly accessible directory ('images/uploads'), keeping the previous SHA1 hash as the file name, adding the dimensions of the new image, and saving it as a JPEG regardless of the original format.
imagejpeg($destImage, 'images/uploads/' . $hash . "-" . $newWidth . "x" . $newHeight . ".jpg");
echo json_encode("Uploaded " . $newfile);
At this point, I could delete the original uploads, but am thinking about holding on to them so that a user could request to download the original full-size image if they wanted. I've also read about the imagedestroy() functions, but my understanding is that the memory is released as soon as the script exits, which is immediately after writing the final JPEG.
So that's what I have right now. I feel like this can be refactored into a class at some point, with methods like:
-saveOriginal
-getImageSize
-scaleImage
-saveFinalImage
Of course, once I was 99% of the way through this, it dawned on me that I should have checked https://packagist.org, but being that this is my first time trying to manipulate images in PHP, I thought it was a good learning experience.
Anyways, looking for feedback. Anything I'm missing? Anything that's redundant? Anything that could be done better? Or just do general cleanup and refactoring (i don't like my variable names, for instance).
Any thoughts would be greatly appreciated! Thanks