Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: GIF upload + file content not matching extension #70

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 19 additions & 6 deletions src/Repository/BaseUploadHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ class BaseUploadHandler
'max_height' => 'Image exceeds maximum height',
'min_height' => 'Image requires a minimum height',
'abort' => 'File upload aborted',
'image_resize' => 'Failed to resize image'
'image_resize' => 'Failed to resize image',
'type_mismatch' => 'File content does not match file extension',
);

protected $image_objects = array();
Expand Down Expand Up @@ -683,6 +684,21 @@ protected function gd_orient_image($file, $src_img) {
return true;
}

protected function gd_write_func($type, $src_img, $new_file_path, $image_quality = null)
{
switch ($type) {
case 'jpg':
case 'jpeg':
return imagejpeg($src_img, $new_file_path, $image_quality);
case 'gif':
return imagegif($src_img, $new_file_path);
case 'png':
return imagepng($src_img, $new_file_path, $image_quality);
}

return false;
}

protected function gd_create_scaled_image($file, $version, $options) {
if (!function_exists('imagecreatetruecolor')) {
error_log('Function not found: imagecreatetruecolor');
Expand All @@ -694,18 +710,15 @@ protected function gd_create_scaled_image($file, $version, $options) {
case 'jpg':
case 'jpeg':
$src_func = 'imagecreatefromjpeg';
$write_func = 'imagejpeg';
$image_quality = isset($options['jpeg_quality']) ?
$options['jpeg_quality'] : 75;
break;
case 'gif':
$src_func = 'imagecreatefromgif';
$write_func = 'imagegif';
$image_quality = null;
break;
case 'png':
$src_func = 'imagecreatefrompng';
$write_func = 'imagepng';
$image_quality = isset($options['png_quality']) ?
$options['png_quality'] : 9;
break;
Expand Down Expand Up @@ -742,7 +755,7 @@ protected function gd_create_scaled_image($file, $version, $options) {
);
if ($scale >= 1) {
if ($image_oriented) {
return $write_func($src_img, $new_file_path, $image_quality);
return $this->gd_write_func($type, $src_img, $new_file_path, $image_quality);
}
if ($file->path !== $new_file_path) {
return copy($file->path, $new_file_path);
Expand Down Expand Up @@ -788,7 +801,7 @@ protected function gd_create_scaled_image($file, $version, $options) {
$new_height,
$img_width,
$img_height
) && $write_func($new_img, $new_file_path, $image_quality);
) && $this->gd_write_func($type, $src_img, $new_file_path, $image_quality);
$this->gd_set_image_object($file->path, $new_img);
return $success;
}
Expand Down
5 changes: 5 additions & 0 deletions src/Repository/Local/UploadHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,11 @@ function_exists('exif_read_data') &&
return false;
}
}
$img_info = $this->get_image_size($uploaded_file);
if ($img_info['mime'] !== $file->type) {
$file->error = $this->get_error_message('type_mismatch');
return false;
}
return true;
}

Expand Down