Skip to content

Commit

Permalink
Fix resize of GIF/PNG with transparency; fixes #6797
Browse files Browse the repository at this point in the history
  • Loading branch information
cedric-anne authored and trasher committed Jan 27, 2020
1 parent b8166fe commit 66617ea
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
4 changes: 3 additions & 1 deletion inc/document.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -1623,12 +1623,14 @@ public static function getImage($path, $context, $mwidth = null, $mheight = null
}

$infos = pathinfo($path);
// output images with possible transparency to png, other to jpg
$extension = in_array(strtolower($infos['extension']), ['png', 'gif']) ? 'png' : 'jpg';
$context_path = sprintf(
'%1$s_%2$s-%3$s.%4$s',
$infos['dirname'] . '/' . $infos['filename'],
$mwidth,
$mheight,
'jpg' //resizePicture always produces JPG files
$extension
);

//let's check if file already exists
Expand Down
21 changes: 20 additions & 1 deletion inc/toolbox.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -1351,12 +1351,31 @@ static function resizePicture($source_path, $dest_path, $new_width = 71, $new_he
//create new img resource for store thumbnail
$source_dest = imagecreatetruecolor($new_width, $new_height);

// set transparent background for PNG/GIF
if ($img_type === IMAGETYPE_GIF || $img_type === IMAGETYPE_PNG) {
imagecolortransparent($source_dest, imagecolorallocatealpha($source_dest, 0, 0, 0, 127));
imagealphablending($source_dest, false);
imagesavealpha($source_dest, true);
}

//resize image
imagecopyresampled($source_dest, $source_res, 0, 0, $img_x, $img_y,
$new_width, $new_height, $img_width, $img_height);

//output img
return imagejpeg($source_dest, $dest_path, 90);
$result = null;
switch ($img_type) {
case IMAGETYPE_GIF :
case IMAGETYPE_PNG :
$result = imagepng($source_dest, $dest_path);
break;

case IMAGETYPE_JPEG :
default :
$result = imagejpeg($source_dest, $dest_path, 90);
break;
}
return $result;
}


Expand Down
9 changes: 7 additions & 2 deletions inc/user.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -711,8 +711,13 @@ function prepareInputForUpdate($input) {
// Move uploaded file
$filename = uniqid($this->fields['id'].'_');
$sub = substr($filename, -2); /* 2 hex digit */
$tmp = explode(".", $input["_picture"]);
$extension = Toolbox::strtolower(array_pop($tmp));

// output images with possible transparency to png, other to jpg
$extension = strtolower(pathinfo($fullpath, PATHINFO_EXTENSION));
$extension = in_array($extension, ['png', 'gif'])
? 'png'
: 'jpg';

@mkdir(GLPI_PICTURE_DIR . "/$sub");
$picture_path = GLPI_PICTURE_DIR . "/$sub/${filename}.$extension";
self::dropPictureFiles("$sub/${filename}.$extension");
Expand Down

0 comments on commit 66617ea

Please sign in to comment.