From baddb058a9a7a25a49d697b13e2d151a3b40643f Mon Sep 17 00:00:00 2001 From: Adrian Greeve Date: Tue, 9 Jun 2015 14:06:42 +0800 Subject: [PATCH] MDL-37308 files: convert_image() now supports transparency. --- lib/filestorage/file_storage.php | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/lib/filestorage/file_storage.php b/lib/filestorage/file_storage.php index c832f619057..801825f8d87 100644 --- a/lib/filestorage/file_storage.php +++ b/lib/filestorage/file_storage.php @@ -1556,7 +1556,16 @@ class file_storage { $img = imagecreatefromstring($file->get_content()); if ($height != $newheight or $width != $newwidth) { $newimg = imagecreatetruecolor($newwidth, $newheight); - if (!imagecopyresized($newimg, $img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height)) { + + // Maintain transparency. + if ($filerecord['mimetype'] == 'image/png' || $filerecord['mimetype'] == 'image/gif') { + $colour = imagecolorallocatealpha($newimg, 0, 0, 0, 127); + imagecolortransparent($newimg, $colour); + imagealphablending($newimg, false); + imagesavealpha($newimg, true); + } + + if (!imagecopyresampled($newimg, $img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height)) { // weird throw new file_exception('storedfileproblem', 'Can not resize image'); }