| 1 | efrain | 1 | <?php
 | 
        
           |  |  | 2 |   | 
        
           |  |  | 3 | // This file is part of Moodle - http://moodle.org/
 | 
        
           |  |  | 4 | //
 | 
        
           |  |  | 5 | // Moodle is free software: you can redistribute it and/or modify
 | 
        
           |  |  | 6 | // it under the terms of the GNU General Public License as published by
 | 
        
           |  |  | 7 | // the Free Software Foundation, either version 3 of the License, or
 | 
        
           |  |  | 8 | // (at your option) any later version.
 | 
        
           |  |  | 9 | //
 | 
        
           |  |  | 10 | // Moodle is distributed in the hope that it will be useful,
 | 
        
           |  |  | 11 | // but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
        
           |  |  | 12 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
        
           |  |  | 13 | // GNU General Public License for more details.
 | 
        
           |  |  | 14 | //
 | 
        
           |  |  | 15 | // You should have received a copy of the GNU General Public License
 | 
        
           |  |  | 16 | // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
 | 
        
           |  |  | 17 |   | 
        
           |  |  | 18 | /**
 | 
        
           |  |  | 19 |  * gdlib.php - Collection of routines in Moodle related to
 | 
        
           |  |  | 20 |  * processing images using GD
 | 
        
           |  |  | 21 |  *
 | 
        
           |  |  | 22 |  * @package   core
 | 
        
           |  |  | 23 |  * @copyright 1999 onwards Martin Dougiamas  {@link http://moodle.com}
 | 
        
           |  |  | 24 |  * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 | 
        
           |  |  | 25 |  */
 | 
        
           |  |  | 26 |   | 
        
           |  |  | 27 | defined('MOODLE_INTERNAL') || die();
 | 
        
           |  |  | 28 |   | 
        
           |  |  | 29 | /**
 | 
        
           |  |  | 30 |  * Stores optimised icon images in icon file area.
 | 
        
           |  |  | 31 |  *
 | 
        
           |  |  | 32 |  * Since 2.9 this function will generate an icon in the same format as the original file when possible.
 | 
        
           |  |  | 33 |  * To counter that behaviour, you can use the argument $preferpng to generate a PNG icon.
 | 
        
           |  |  | 34 |  *
 | 
        
           |  |  | 35 |  * @param context $context
 | 
        
           |  |  | 36 |  * @param string $component
 | 
        
           |  |  | 37 |  * @param string filearea
 | 
        
           |  |  | 38 |  * @param int $itemid
 | 
        
           |  |  | 39 |  * @param string $originalfile
 | 
        
           |  |  | 40 |  * @param boolean $preferpng When true, it will try to generate a PNG file regardless of the original file.
 | 
        
           |  |  | 41 |  * @return mixed new unique revision number or false if not saved
 | 
        
           |  |  | 42 |  */
 | 
        
           |  |  | 43 | function process_new_icon($context, $component, $filearea, $itemid, $originalfile, $preferpng = false) {
 | 
        
           |  |  | 44 |     global $CFG;
 | 
        
           |  |  | 45 |   | 
        
           |  |  | 46 |     if (!is_file($originalfile)) {
 | 
        
           |  |  | 47 |         return false;
 | 
        
           |  |  | 48 |     }
 | 
        
           |  |  | 49 |   | 
        
           |  |  | 50 |     $imageinfo = getimagesize($originalfile);
 | 
        
           |  |  | 51 |     $imagefnc = '';
 | 
        
           |  |  | 52 |   | 
        
           |  |  | 53 |     if (empty($imageinfo)) {
 | 
        
           |  |  | 54 |         return false;
 | 
        
           |  |  | 55 |     }
 | 
        
           |  |  | 56 |   | 
        
           |  |  | 57 |     $image = new stdClass();
 | 
        
           |  |  | 58 |     $image->width  = $imageinfo[0];
 | 
        
           |  |  | 59 |     $image->height = $imageinfo[1];
 | 
        
           |  |  | 60 |     $image->type   = $imageinfo[2];
 | 
        
           |  |  | 61 |   | 
        
           |  |  | 62 |     $t = null;
 | 
        
           |  |  | 63 |     switch ($image->type) {
 | 
        
           |  |  | 64 |         case IMAGETYPE_GIF:
 | 
        
           |  |  | 65 |             if (function_exists('imagecreatefromgif')) {
 | 
        
           |  |  | 66 |                 $im = imagecreatefromgif($originalfile);
 | 
        
           |  |  | 67 |             } else {
 | 
        
           |  |  | 68 |                 debugging('GIF not supported on this server');
 | 
        
           |  |  | 69 |                 return false;
 | 
        
           |  |  | 70 |             }
 | 
        
           |  |  | 71 |             // Guess transparent colour from GIF.
 | 
        
           |  |  | 72 |             $transparent = imagecolortransparent($im);
 | 
        
           |  |  | 73 |             if ($transparent != -1) {
 | 
        
           |  |  | 74 |                 $t = imagecolorsforindex($im, $transparent);
 | 
        
           |  |  | 75 |             }
 | 
        
           |  |  | 76 |             break;
 | 
        
           |  |  | 77 |         case IMAGETYPE_JPEG:
 | 
        
           |  |  | 78 |             if (function_exists('imagecreatefromjpeg')) {
 | 
        
           |  |  | 79 |                 $im = imagecreatefromjpeg($originalfile);
 | 
        
           |  |  | 80 |             } else {
 | 
        
           |  |  | 81 |                 debugging('JPEG not supported on this server');
 | 
        
           |  |  | 82 |                 return false;
 | 
        
           |  |  | 83 |             }
 | 
        
           |  |  | 84 |             // If the user uploads a jpeg them we should process as a jpeg if possible.
 | 
        
           |  |  | 85 |             if (!$preferpng && function_exists('imagejpeg')) {
 | 
        
           |  |  | 86 |                 $imagefnc = 'imagejpeg';
 | 
        
           |  |  | 87 |                 $imageext = '.jpg';
 | 
        
           |  |  | 88 |                 $filters = null; // Not used.
 | 
        
           |  |  | 89 |                 $quality = 90;
 | 
        
           |  |  | 90 |             }
 | 
        
           |  |  | 91 |             break;
 | 
        
           |  |  | 92 |         case IMAGETYPE_PNG:
 | 
        
           |  |  | 93 |             if (function_exists('imagecreatefrompng')) {
 | 
        
           |  |  | 94 |                 $im = imagecreatefrompng($originalfile);
 | 
        
           |  |  | 95 |             } else {
 | 
        
           |  |  | 96 |                 debugging('PNG not supported on this server');
 | 
        
           |  |  | 97 |                 return false;
 | 
        
           |  |  | 98 |             }
 | 
        
           |  |  | 99 |             break;
 | 
        
           |  |  | 100 |         default:
 | 
        
           |  |  | 101 |             return false;
 | 
        
           |  |  | 102 |     }
 | 
        
           |  |  | 103 |   | 
        
           |  |  | 104 |     // The conversion has not been decided yet, let's apply defaults (png with fallback to jpg).
 | 
        
           |  |  | 105 |     if (empty($imagefnc)) {
 | 
        
           |  |  | 106 |         if (function_exists('imagepng')) {
 | 
        
           |  |  | 107 |             $imagefnc = 'imagepng';
 | 
        
           |  |  | 108 |             $imageext = '.png';
 | 
        
           |  |  | 109 |             $filters = PNG_NO_FILTER;
 | 
        
           |  |  | 110 |             $quality = 1;
 | 
        
           |  |  | 111 |         } else if (function_exists('imagejpeg')) {
 | 
        
           |  |  | 112 |             $imagefnc = 'imagejpeg';
 | 
        
           |  |  | 113 |             $imageext = '.jpg';
 | 
        
           |  |  | 114 |             $filters = null; // Not used.
 | 
        
           |  |  | 115 |             $quality = 90;
 | 
        
           |  |  | 116 |         } else {
 | 
        
           |  |  | 117 |             debugging('Jpeg and png not supported on this server, please fix server configuration');
 | 
        
           |  |  | 118 |             return false;
 | 
        
           |  |  | 119 |         }
 | 
        
           |  |  | 120 |     }
 | 
        
           |  |  | 121 |   | 
        
           |  |  | 122 |     if (function_exists('imagecreatetruecolor')) {
 | 
        
           |  |  | 123 |         $im1 = imagecreatetruecolor(100, 100);
 | 
        
           |  |  | 124 |         $im2 = imagecreatetruecolor(35, 35);
 | 
        
           |  |  | 125 |         $im3 = imagecreatetruecolor(512, 512);
 | 
        
           |  |  | 126 |         if ($image->type != IMAGETYPE_JPEG and $imagefnc === 'imagepng') {
 | 
        
           |  |  | 127 |             if ($t) {
 | 
        
           |  |  | 128 |                 // Transparent GIF hacking...
 | 
        
           |  |  | 129 |                 $transparentcolour = imagecolorallocate($im1 , $t['red'] , $t['green'] , $t['blue']);
 | 
        
           |  |  | 130 |                 imagecolortransparent($im1 , $transparentcolour);
 | 
        
           |  |  | 131 |                 $transparentcolour = imagecolorallocate($im2 , $t['red'] , $t['green'] , $t['blue']);
 | 
        
           |  |  | 132 |                 imagecolortransparent($im2 , $transparentcolour);
 | 
        
           |  |  | 133 |                 $transparentcolour = imagecolorallocate($im3 , $t['red'] , $t['green'] , $t['blue']);
 | 
        
           |  |  | 134 |                 imagecolortransparent($im3 , $transparentcolour);
 | 
        
           |  |  | 135 |             }
 | 
        
           |  |  | 136 |   | 
        
           |  |  | 137 |             imagealphablending($im1, false);
 | 
        
           |  |  | 138 |             $color = imagecolorallocatealpha($im1, 0, 0,  0, 127);
 | 
        
           |  |  | 139 |             imagefill($im1, 0, 0,  $color);
 | 
        
           |  |  | 140 |             imagesavealpha($im1, true);
 | 
        
           |  |  | 141 |   | 
        
           |  |  | 142 |             imagealphablending($im2, false);
 | 
        
           |  |  | 143 |             $color = imagecolorallocatealpha($im2, 0, 0,  0, 127);
 | 
        
           |  |  | 144 |             imagefill($im2, 0, 0,  $color);
 | 
        
           |  |  | 145 |             imagesavealpha($im2, true);
 | 
        
           |  |  | 146 |   | 
        
           |  |  | 147 |             imagealphablending($im3, false);
 | 
        
           |  |  | 148 |             $color = imagecolorallocatealpha($im3, 0, 0,  0, 127);
 | 
        
           |  |  | 149 |             imagefill($im3, 0, 0,  $color);
 | 
        
           |  |  | 150 |             imagesavealpha($im3, true);
 | 
        
           |  |  | 151 |         }
 | 
        
           |  |  | 152 |     } else {
 | 
        
           |  |  | 153 |         $im1 = imagecreate(100, 100);
 | 
        
           |  |  | 154 |         $im2 = imagecreate(35, 35);
 | 
        
           |  |  | 155 |         $im3 = imagecreate(512, 512);
 | 
        
           |  |  | 156 |     }
 | 
        
           |  |  | 157 |   | 
        
           |  |  | 158 |     $cx = floor($image->width / 2);
 | 
        
           |  |  | 159 |     $cy = floor($image->height / 2);
 | 
        
           |  |  | 160 |   | 
        
           |  |  | 161 |     if ($image->width < $image->height) {
 | 
        
           |  |  | 162 |         $half = floor($image->width / 2.0);
 | 
        
           |  |  | 163 |     } else {
 | 
        
           |  |  | 164 |         $half = floor($image->height / 2.0);
 | 
        
           |  |  | 165 |     }
 | 
        
           |  |  | 166 |   | 
        
           | 1441 | ariadna | 167 |     imagecopyresampled($im1, $im, 0, 0, $cx - $half, $cy - $half, 100, 100, $half * 2, $half * 2);
 | 
        
           |  |  | 168 |     imagecopyresampled($im2, $im, 0, 0, $cx - $half, $cy - $half, 35, 35, $half * 2, $half * 2);
 | 
        
           |  |  | 169 |     imagecopyresampled($im3, $im, 0, 0, $cx - $half, $cy - $half, 512, 512, $half * 2, $half * 2);
 | 
        
           | 1 | efrain | 170 |   | 
        
           |  |  | 171 |     $fs = get_file_storage();
 | 
        
           |  |  | 172 |   | 
        
           |  |  | 173 |     $icon = array('contextid'=>$context->id, 'component'=>$component, 'filearea'=>$filearea, 'itemid'=>$itemid, 'filepath'=>'/');
 | 
        
           |  |  | 174 |   | 
        
           |  |  | 175 |     if ($imagefnc === 'imagejpeg') {
 | 
        
           |  |  | 176 |         // Function imagejpeg() accepts less arguments than imagepng() but we need to make $imagefnc accept the same
 | 
        
           |  |  | 177 |         // number of arguments, otherwise PHP8 throws an error.
 | 
        
           |  |  | 178 |         $imagefnc = function($image, $filename, $quality, $unused) {
 | 
        
           |  |  | 179 |             return imagejpeg($image, $filename, $quality);
 | 
        
           |  |  | 180 |         };
 | 
        
           |  |  | 181 |     }
 | 
        
           |  |  | 182 |   | 
        
           |  |  | 183 |     ob_start();
 | 
        
           |  |  | 184 |     if (!$imagefnc($im1, NULL, $quality, $filters)) {
 | 
        
           |  |  | 185 |         // keep old icons
 | 
        
           |  |  | 186 |         ob_end_clean();
 | 
        
           |  |  | 187 |         return false;
 | 
        
           |  |  | 188 |     }
 | 
        
           |  |  | 189 |     $data = ob_get_clean();
 | 
        
           |  |  | 190 |     imagedestroy($im1);
 | 
        
           |  |  | 191 |     $icon['filename'] = 'f1'.$imageext;
 | 
        
           |  |  | 192 |     $fs->delete_area_files($context->id, $component, $filearea, $itemid);
 | 
        
           |  |  | 193 |     $file1 = $fs->create_file_from_string($icon, $data);
 | 
        
           |  |  | 194 |   | 
        
           |  |  | 195 |     ob_start();
 | 
        
           |  |  | 196 |     if (!$imagefnc($im2, NULL, $quality, $filters)) {
 | 
        
           |  |  | 197 |         ob_end_clean();
 | 
        
           |  |  | 198 |         $fs->delete_area_files($context->id, $component, $filearea, $itemid);
 | 
        
           |  |  | 199 |         return false;
 | 
        
           |  |  | 200 |     }
 | 
        
           |  |  | 201 |     $data = ob_get_clean();
 | 
        
           |  |  | 202 |     imagedestroy($im2);
 | 
        
           |  |  | 203 |     $icon['filename'] = 'f2'.$imageext;
 | 
        
           |  |  | 204 |     $fs->create_file_from_string($icon, $data);
 | 
        
           |  |  | 205 |   | 
        
           |  |  | 206 |     ob_start();
 | 
        
           |  |  | 207 |     if (!$imagefnc($im3, NULL, $quality, $filters)) {
 | 
        
           |  |  | 208 |         ob_end_clean();
 | 
        
           |  |  | 209 |         $fs->delete_area_files($context->id, $component, $filearea, $itemid);
 | 
        
           |  |  | 210 |         return false;
 | 
        
           |  |  | 211 |     }
 | 
        
           |  |  | 212 |     $data = ob_get_clean();
 | 
        
           |  |  | 213 |     imagedestroy($im3);
 | 
        
           |  |  | 214 |     $icon['filename'] = 'f3'.$imageext;
 | 
        
           |  |  | 215 |     $fs->create_file_from_string($icon, $data);
 | 
        
           |  |  | 216 |   | 
        
           |  |  | 217 |     return $file1->get_id();
 | 
        
           |  |  | 218 | }
 | 
        
           |  |  | 219 |   | 
        
           |  |  | 220 |   | 
        
           |  |  | 221 | /**
 | 
        
           |  |  | 222 |  * Resize an image from an image path.
 | 
        
           |  |  | 223 |  *
 | 
        
           |  |  | 224 |  * This maintains the aspect ratio of the image.
 | 
        
           |  |  | 225 |  * This will not enlarge the image.
 | 
        
           |  |  | 226 |  *
 | 
        
           |  |  | 227 |  * @param string $filepath The full path to the original image file.
 | 
        
           |  |  | 228 |  * @param int|null $width The max width of the resized image, or null to only use the height.
 | 
        
           |  |  | 229 |  * @param int|null $height The max height of the resized image, or null to only use the width.
 | 
        
           |  |  | 230 |  * @param bool $forcecanvas Whether the final dimensions should be set to $width and $height.
 | 
        
           |  |  | 231 |  * @return string|bool False if a problem occurs, else the resized image data.
 | 
        
           |  |  | 232 |  */
 | 
        
           |  |  | 233 | function resize_image($filepath, $width, $height, $forcecanvas = false) {
 | 
        
           |  |  | 234 |     if (empty($filepath)) {
 | 
        
           |  |  | 235 |         return false;
 | 
        
           |  |  | 236 |     }
 | 
        
           |  |  | 237 |   | 
        
           |  |  | 238 |     // Fetch the image information for this image.
 | 
        
           |  |  | 239 |     $imageinfo = @getimagesize($filepath);
 | 
        
           |  |  | 240 |     if (empty($imageinfo)) {
 | 
        
           |  |  | 241 |         return false;
 | 
        
           |  |  | 242 |     }
 | 
        
           |  |  | 243 |   | 
        
           |  |  | 244 |     // Create a new image from the file.
 | 
        
           |  |  | 245 |     $original = @imagecreatefromstring(file_get_contents($filepath));
 | 
        
           |  |  | 246 |   | 
        
           |  |  | 247 |     // Generate the thumbnail.
 | 
        
           |  |  | 248 |     return resize_image_from_image($original, $imageinfo, $width, $height, $forcecanvas);
 | 
        
           |  |  | 249 | }
 | 
        
           |  |  | 250 |   | 
        
           |  |  | 251 | /**
 | 
        
           |  |  | 252 |  * Resize an image from an image object.
 | 
        
           |  |  | 253 |  *
 | 
        
           |  |  | 254 |  * @param resource|\GdImage $original The image to work on.
 | 
        
           |  |  | 255 |  * @param array $imageinfo Contains [0] => originalwidth, [1] => originalheight.
 | 
        
           |  |  | 256 |  * @param int|null $width The max width of the resized image, or null to only use the height.
 | 
        
           |  |  | 257 |  * @param int|null $height The max height of the resized image, or null to only use the width.
 | 
        
           |  |  | 258 |  * @param bool $forcecanvas Whether the final dimensions should be set to $width and $height.
 | 
        
           |  |  | 259 |  * @return string|bool False if a problem occurs, else the resized image data.
 | 
        
           |  |  | 260 |  */
 | 
        
           |  |  | 261 | function resize_image_from_image($original, $imageinfo, $width, $height, $forcecanvas = false) {
 | 
        
           |  |  | 262 |     global $CFG;
 | 
        
           |  |  | 263 |   | 
        
           |  |  | 264 |     if (empty($width) && empty($height) || ($forcecanvas && (empty($width) || empty($height)))) {
 | 
        
           |  |  | 265 |         // We need do not have the required ddimensions to work with.
 | 
        
           |  |  | 266 |         return false;
 | 
        
           |  |  | 267 |     }
 | 
        
           |  |  | 268 |   | 
        
           |  |  | 269 |     if (empty($imageinfo)) {
 | 
        
           |  |  | 270 |         return false;
 | 
        
           |  |  | 271 |     }
 | 
        
           |  |  | 272 |   | 
        
           |  |  | 273 |     $originalwidth  = $imageinfo[0];
 | 
        
           |  |  | 274 |     $originalheight = $imageinfo[1];
 | 
        
           |  |  | 275 |     if (empty($originalwidth) or empty($originalheight)) {
 | 
        
           |  |  | 276 |         return false;
 | 
        
           |  |  | 277 |     }
 | 
        
           |  |  | 278 |   | 
        
           |  |  | 279 |     if (function_exists('imagepng')) {
 | 
        
           |  |  | 280 |         $imagefnc = 'imagepng';
 | 
        
           |  |  | 281 |         $filters = PNG_NO_FILTER;
 | 
        
           |  |  | 282 |         $quality = 1;
 | 
        
           |  |  | 283 |     } else if (function_exists('imagejpeg')) {
 | 
        
           |  |  | 284 |         $imagefnc = 'imagejpeg';
 | 
        
           |  |  | 285 |         $filters = null;
 | 
        
           |  |  | 286 |         $quality = 90;
 | 
        
           |  |  | 287 |     } else {
 | 
        
           |  |  | 288 |         debugging('Neither JPEG nor PNG are supported at this server, please fix the system configuration.');
 | 
        
           |  |  | 289 |         return false;
 | 
        
           |  |  | 290 |     }
 | 
        
           |  |  | 291 |   | 
        
           |  |  | 292 |     if (empty($height)) {
 | 
        
           |  |  | 293 |         $ratio = $width / $originalwidth;
 | 
        
           |  |  | 294 |     } else if (empty($width)) {
 | 
        
           |  |  | 295 |         $ratio = $height / $originalheight;
 | 
        
           |  |  | 296 |     } else {
 | 
        
           |  |  | 297 |         $ratio = min($width / $originalwidth, $height / $originalheight);
 | 
        
           |  |  | 298 |     }
 | 
        
           |  |  | 299 |   | 
        
           |  |  | 300 |     if ($ratio < 1) {
 | 
        
           |  |  | 301 |         $targetwidth    = round($originalwidth * $ratio);
 | 
        
           |  |  | 302 |         $targetheight   = round($originalheight * $ratio);
 | 
        
           |  |  | 303 |     } else {
 | 
        
           |  |  | 304 |         // Do not enlarge the original file if it is smaller than the requested thumbnail size.
 | 
        
           |  |  | 305 |         $targetwidth    = $originalwidth;
 | 
        
           |  |  | 306 |         $targetheight   = $originalheight;
 | 
        
           |  |  | 307 |     }
 | 
        
           |  |  | 308 |   | 
        
           |  |  | 309 |     $canvaswidth = $targetwidth;
 | 
        
           |  |  | 310 |     $canvasheight = $targetheight;
 | 
        
           |  |  | 311 |     $dstx = 0;
 | 
        
           |  |  | 312 |     $dsty = 0;
 | 
        
           |  |  | 313 |   | 
        
           |  |  | 314 |     if ($forcecanvas) {
 | 
        
           |  |  | 315 |         $canvaswidth = $width;
 | 
        
           |  |  | 316 |         $canvasheight = $height;
 | 
        
           |  |  | 317 |         $dstx = floor(($width - $targetwidth) / 2);
 | 
        
           |  |  | 318 |         $dsty = floor(($height - $targetheight) / 2);
 | 
        
           |  |  | 319 |     }
 | 
        
           |  |  | 320 |   | 
        
           |  |  | 321 |     if (function_exists('imagecreatetruecolor')) {
 | 
        
           |  |  | 322 |         $newimage = imagecreatetruecolor($canvaswidth, $canvasheight);
 | 
        
           |  |  | 323 |         if ($imagefnc === 'imagepng') {
 | 
        
           |  |  | 324 |             imagealphablending($newimage, false);
 | 
        
           |  |  | 325 |             imagefill($newimage, 0, 0, imagecolorallocatealpha($newimage, 0, 0, 0, 127));
 | 
        
           |  |  | 326 |             imagesavealpha($newimage, true);
 | 
        
           |  |  | 327 |         }
 | 
        
           |  |  | 328 |     } else {
 | 
        
           |  |  | 329 |         $newimage = imagecreate($canvaswidth, $canvasheight);
 | 
        
           |  |  | 330 |     }
 | 
        
           |  |  | 331 |   | 
        
           | 1441 | ariadna | 332 |     imagecopyresampled($newimage, $original, $dstx, $dsty, 0, 0, $targetwidth, $targetheight, $originalwidth, $originalheight);
 | 
        
           | 1 | efrain | 333 |   | 
        
           |  |  | 334 |     if ($imagefnc === 'imagejpeg') {
 | 
        
           |  |  | 335 |         // Function imagejpeg() accepts less arguments than imagepng() but we need to make $imagefnc accept the same
 | 
        
           |  |  | 336 |         // number of arguments, otherwise PHP8 throws an error.
 | 
        
           |  |  | 337 |         $imagefnc = function($image, $filename, $quality, $unused) {
 | 
        
           |  |  | 338 |             return imagejpeg($image, $filename, $quality);
 | 
        
           |  |  | 339 |         };
 | 
        
           |  |  | 340 |     }
 | 
        
           |  |  | 341 |   | 
        
           |  |  | 342 |     // Capture the image as a string object, rather than straight to file.
 | 
        
           |  |  | 343 |     ob_start();
 | 
        
           |  |  | 344 |     if (!$imagefnc($newimage, null, $quality, $filters)) {
 | 
        
           |  |  | 345 |         ob_end_clean();
 | 
        
           |  |  | 346 |         return false;
 | 
        
           |  |  | 347 |     }
 | 
        
           |  |  | 348 |     $data = ob_get_clean();
 | 
        
           |  |  | 349 |     imagedestroy($original);
 | 
        
           |  |  | 350 |     imagedestroy($newimage);
 | 
        
           |  |  | 351 |   | 
        
           |  |  | 352 |     return $data;
 | 
        
           |  |  | 353 | }
 | 
        
           |  |  | 354 |   | 
        
           |  |  | 355 | /**
 | 
        
           |  |  | 356 |  * Generates a thumbnail for the given image
 | 
        
           |  |  | 357 |  *
 | 
        
           |  |  | 358 |  * If the GD library has at least version 2 and PNG support is available, the returned data
 | 
        
           |  |  | 359 |  * is the content of a transparent PNG file containing the thumbnail. Otherwise, the function
 | 
        
           |  |  | 360 |  * returns contents of a JPEG file with black background containing the thumbnail.
 | 
        
           |  |  | 361 |  *
 | 
        
           |  |  | 362 |  * @param string $filepath the full path to the original image file
 | 
        
           |  |  | 363 |  * @param int $width the width of the requested thumbnail
 | 
        
           |  |  | 364 |  * @param int $height the height of the requested thumbnail
 | 
        
           |  |  | 365 |  * @return string|bool false if a problem occurs, the thumbnail image data otherwise
 | 
        
           |  |  | 366 |  */
 | 
        
           |  |  | 367 | function generate_image_thumbnail($filepath, $width, $height) {
 | 
        
           |  |  | 368 |     return resize_image($filepath, $width, $height, true);
 | 
        
           |  |  | 369 | }
 | 
        
           |  |  | 370 |   | 
        
           |  |  | 371 | /**
 | 
        
           |  |  | 372 |  * Generates a thumbnail for the given image string.
 | 
        
           |  |  | 373 |  *
 | 
        
           |  |  | 374 |  * If the GD library has at least version 2 and PNG support is available, the returned data
 | 
        
           |  |  | 375 |  * is the content of a transparent PNG file containing the thumbnail. Otherwise, the function
 | 
        
           |  |  | 376 |  * returns contents of a JPEG file with black background containing the thumbnail.
 | 
        
           |  |  | 377 |  *
 | 
        
           |  |  | 378 |  * @param   string $filedata The image content as a string
 | 
        
           |  |  | 379 |  * @param   int $width the width of the requested thumbnail
 | 
        
           |  |  | 380 |  * @param   int $height the height of the requested thumbnail
 | 
        
           |  |  | 381 |  * @return  string|bool false if a problem occurs, the thumbnail image data otherwise
 | 
        
           |  |  | 382 |  */
 | 
        
           |  |  | 383 | function generate_image_thumbnail_from_string($filedata, $width, $height) {
 | 
        
           |  |  | 384 |     if (empty($filedata) or empty($width) or empty($height)) {
 | 
        
           |  |  | 385 |         return false;
 | 
        
           |  |  | 386 |     }
 | 
        
           |  |  | 387 |   | 
        
           |  |  | 388 |     // Fetch the image information for this image.
 | 
        
           |  |  | 389 |     $imageinfo = @getimagesizefromstring($filedata);
 | 
        
           |  |  | 390 |     if (empty($imageinfo)) {
 | 
        
           |  |  | 391 |         return false;
 | 
        
           |  |  | 392 |     }
 | 
        
           |  |  | 393 |   | 
        
           |  |  | 394 |     // Create a new image from the file.
 | 
        
           |  |  | 395 |     $original = @imagecreatefromstring($filedata);
 | 
        
           |  |  | 396 |   | 
        
           |  |  | 397 |     // Generate the thumbnail.
 | 
        
           |  |  | 398 |     return generate_image_thumbnail_from_image($original, $imageinfo, $width, $height);
 | 
        
           |  |  | 399 | }
 | 
        
           |  |  | 400 |   | 
        
           |  |  | 401 | /**
 | 
        
           |  |  | 402 |  * Generates a thumbnail for the given image string.
 | 
        
           |  |  | 403 |  *
 | 
        
           |  |  | 404 |  * If the GD library has at least version 2 and PNG support is available, the returned data
 | 
        
           |  |  | 405 |  * is the content of a transparent PNG file containing the thumbnail. Otherwise, the function
 | 
        
           |  |  | 406 |  * returns contents of a JPEG file with black background containing the thumbnail.
 | 
        
           |  |  | 407 |  *
 | 
        
           |  |  | 408 |  * @param   resource $original The image to work on.
 | 
        
           |  |  | 409 |  * @param   array $imageinfo Contains [0] => originalwidth, [1] => originalheight.
 | 
        
           |  |  | 410 |  * @param   int $width The width of the requested thumbnail.
 | 
        
           |  |  | 411 |  * @param   int $height The height of the requested thumbnail.
 | 
        
           |  |  | 412 |  * @return  string|bool False if a problem occurs, the thumbnail image data otherwise.
 | 
        
           |  |  | 413 |  */
 | 
        
           |  |  | 414 | function generate_image_thumbnail_from_image($original, $imageinfo, $width, $height) {
 | 
        
           |  |  | 415 |     return resize_image_from_image($original, $imageinfo, $width, $height, true);
 | 
        
           |  |  | 416 | }
 |