A third party can upload files via FTP to a client's webserver. These images should be displayed on a public-available webpage. The upload directory is not accessible via normal HTTP. To increase security I thought it might be a good idea to re-create the images and store it in the web-folder manually.
The import format is always JPG. The function will return the new filename if the image could be created or null otherwise.
Is there any way to improve it? Especially all the "early returns" look kind of ugly.
function processImage($filename, $importPath, $storagePath, $quality = 80) {
$source = $importPath . $filename . '.jpg';
$target = $filename . '_processed.jpg';
$storage = $storagePath . $target;
if (!file_exists($source)) {
return null;
}
$image = imagecreatefromstring(file_get_contents($source));
if (!$image) {
return null;
}
$dimension = [
imagesx($image),
imagesy($image)
];
$newImage = imagecreatetruecolor($dimension[0], $dimension[1]);
if (!$newImage) {
return null;
}
if (!imagecopyresampled($newImage, $image, 0, 0, 0, 0, $dimension[0], $dimension[1], $dimension[0], $dimension[1])) {
return null;
}
if (!imagejpeg($newImage, $storage, $quality)) {
return null;
}
return $target;
}
PS: The filename is available without the extension .jpg
as a string. In case you worry about the first line.
imagecopyresampled
will redraw the image: the resampling will effectively randomize the bits in the image, destroying any ascii data that is hiding in it. \$\endgroup\$script
tag, which is unusual, or takes additional effort to make happen. This limits the effectiveness of such an attack. Hosting a JPG file with malicious data embedded doesn't immediately pose a risk to anyone, except perhaps by giving an attacker a way to circumvent CSP protections if they find an XSS vulnerability. Again, security.stackexchange.com is a great place to discuss such a thing in detail. \$\endgroup\$