MDL-37308 files: convert_image() now supports transparency.
authorAdrian Greeve <adrian@moodle.com>
Tue, 9 Jun 2015 06:06:42 +0000 (14:06 +0800)
committerJun Pataleta <jun@moodle.com>
Mon, 30 Nov 2015 02:28:18 +0000 (20:28 -0600)
lib/filestorage/file_storage.php

index c832f61..801825f 100644 (file)
@@ -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');
             }