Merge branch 'MDL-39488_m24' of git://github.com/rwijaya/moodle into MOODLE_24_STABLE
[moodle.git] / lib / gdlib.php
blobb4e4918713fe1b42368a0fa728dd5eb489de05cc
1 <?php
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.
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/>.
18 /**
19 * gdlib.php - Collection of routines in Moodle related to
20 * processing images using GD
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
27 defined('MOODLE_INTERNAL') || die();
29 /**
30 * Copies a rectangular portion of the source image to another rectangle in the destination image
32 * This function calls imagecopyresampled() if it is available and GD version is 2 at least.
33 * Otherwise it reimplements the same behaviour. See the PHP manual page for more info.
35 * @link http://php.net/manual/en/function.imagecopyresampled.php
36 * @param resource $dst_img the destination GD image resource
37 * @param resource $src_img the source GD image resource
38 * @param int $dst_x vthe X coordinate of the upper left corner in the destination image
39 * @param int $dst_y the Y coordinate of the upper left corner in the destination image
40 * @param int $src_x the X coordinate of the upper left corner in the source image
41 * @param int $src_y the Y coordinate of the upper left corner in the source image
42 * @param int $dst_w the width of the destination rectangle
43 * @param int $dst_h the height of the destination rectangle
44 * @param int $src_w the width of the source rectangle
45 * @param int $src_h the height of the source rectangle
46 * @return bool tru on success, false otherwise
48 function imagecopybicubic($dst_img, $src_img, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h) {
49 global $CFG;
51 if (function_exists('imagecopyresampled') and $CFG->gdversion >= 2) {
52 return imagecopyresampled($dst_img, $src_img, $dst_x, $dst_y, $src_x, $src_y,
53 $dst_w, $dst_h, $src_w, $src_h);
56 $totalcolors = imagecolorstotal($src_img);
57 for ($i=0; $i<$totalcolors; $i++) {
58 if ($colors = imagecolorsforindex($src_img, $i)) {
59 imagecolorallocate($dst_img, $colors['red'], $colors['green'], $colors['blue']);
63 $scalex = ($src_w - 1) / $dst_w;
64 $scaley = ($src_h - 1) / $dst_h;
66 $scalex2 = $scalex / 2.0;
67 $scaley2 = $scaley / 2.0;
69 for ($j = 0; $j < $dst_h; $j++) {
70 $sy = $j * $scaley;
72 for ($i = 0; $i < $dst_w; $i++) {
73 $sx = $i * $scalex;
75 $c1 = imagecolorsforindex($src_img, imagecolorat($src_img, (int)$sx, (int)$sy + $scaley2));
76 $c2 = imagecolorsforindex($src_img, imagecolorat($src_img, (int)$sx, (int)$sy));
77 $c3 = imagecolorsforindex($src_img, imagecolorat($src_img, (int)$sx + $scalex2, (int)$sy + $scaley2));
78 $c4 = imagecolorsforindex($src_img, imagecolorat($src_img, (int)$sx + $scalex2, (int)$sy));
80 $red = (int) (($c1['red'] + $c2['red'] + $c3['red'] + $c4['red']) / 4);
81 $green = (int) (($c1['green'] + $c2['green'] + $c3['green'] + $c4['green']) / 4);
82 $blue = (int) (($c1['blue'] + $c2['blue'] + $c3['blue'] + $c4['blue']) / 4);
84 $color = imagecolorclosest($dst_img, $red, $green, $blue);
85 imagesetpixel($dst_img, $i + $dst_x, $j + $dst_y, $color);
90 /**
91 * Stores optimised icon images in icon file area
93 * @param context $context
94 * @param string $component
95 * @param string filearea
96 * @param int $itemid
97 * @param string $originalfile
98 * @return mixed new unique revision number or false if not saved
100 function process_new_icon($context, $component, $filearea, $itemid, $originalfile) {
101 global $CFG;
103 if (empty($CFG->gdversion)) {
104 return false;
107 if (!is_file($originalfile)) {
108 return false;
111 $imageinfo = getimagesize($originalfile);
113 if (empty($imageinfo)) {
114 return false;
117 $image = new stdClass();
118 $image->width = $imageinfo[0];
119 $image->height = $imageinfo[1];
120 $image->type = $imageinfo[2];
122 $t = null;
123 switch ($image->type) {
124 case IMAGETYPE_GIF:
125 if (function_exists('imagecreatefromgif')) {
126 $im = imagecreatefromgif($originalfile);
127 } else {
128 debugging('GIF not supported on this server');
129 return false;
131 // Guess transparent colour from GIF.
132 $transparent = imagecolortransparent($im);
133 if ($transparent != -1) {
134 $t = imagecolorsforindex($im, $transparent);
136 break;
137 case IMAGETYPE_JPEG:
138 if (function_exists('imagecreatefromjpeg')) {
139 $im = imagecreatefromjpeg($originalfile);
140 } else {
141 debugging('JPEG not supported on this server');
142 return false;
144 break;
145 case IMAGETYPE_PNG:
146 if (function_exists('imagecreatefrompng')) {
147 $im = imagecreatefrompng($originalfile);
148 } else {
149 debugging('PNG not supported on this server');
150 return false;
152 break;
153 default:
154 return false;
157 if (function_exists('imagepng')) {
158 $imagefnc = 'imagepng';
159 $imageext = '.png';
160 $filters = PNG_NO_FILTER;
161 $quality = 1;
162 } else if (function_exists('imagejpeg')) {
163 $imagefnc = 'imagejpeg';
164 $imageext = '.jpg';
165 $filters = null; // not used
166 $quality = 90;
167 } else {
168 debugging('Jpeg and png not supported on this server, please fix server configuration');
169 return false;
172 if (function_exists('imagecreatetruecolor') and $CFG->gdversion >= 2) {
173 $im1 = imagecreatetruecolor(100, 100);
174 $im2 = imagecreatetruecolor(35, 35);
175 $im3 = imagecreatetruecolor(512, 512);
176 if ($image->type != IMAGETYPE_JPEG and $imagefnc === 'imagepng') {
177 if ($t) {
178 // Transparent GIF hacking...
179 $transparentcolour = imagecolorallocate($im1 , $t['red'] , $t['green'] , $t['blue']);
180 imagecolortransparent($im1 , $transparentcolour);
181 $transparentcolour = imagecolorallocate($im2 , $t['red'] , $t['green'] , $t['blue']);
182 imagecolortransparent($im2 , $transparentcolour);
183 $transparentcolour = imagecolorallocate($im3 , $t['red'] , $t['green'] , $t['blue']);
184 imagecolortransparent($im3 , $transparentcolour);
187 imagealphablending($im1, false);
188 $color = imagecolorallocatealpha($im1, 0, 0, 0, 127);
189 imagefill($im1, 0, 0, $color);
190 imagesavealpha($im1, true);
192 imagealphablending($im2, false);
193 $color = imagecolorallocatealpha($im2, 0, 0, 0, 127);
194 imagefill($im2, 0, 0, $color);
195 imagesavealpha($im2, true);
197 imagealphablending($im3, false);
198 $color = imagecolorallocatealpha($im3, 0, 0, 0, 127);
199 imagefill($im3, 0, 0, $color);
200 imagesavealpha($im3, true);
202 } else {
203 $im1 = imagecreate(100, 100);
204 $im2 = imagecreate(35, 35);
205 $im3 = imagecreate(512, 512);
208 $cx = $image->width / 2;
209 $cy = $image->height / 2;
211 if ($image->width < $image->height) {
212 $half = floor($image->width / 2.0);
213 } else {
214 $half = floor($image->height / 2.0);
217 imagecopybicubic($im1, $im, 0, 0, $cx - $half, $cy - $half, 100, 100, $half * 2, $half * 2);
218 imagecopybicubic($im2, $im, 0, 0, $cx - $half, $cy - $half, 35, 35, $half * 2, $half * 2);
219 imagecopybicubic($im3, $im, 0, 0, $cx - $half, $cy - $half, 512, 512, $half * 2, $half * 2);
221 $fs = get_file_storage();
223 $icon = array('contextid'=>$context->id, 'component'=>$component, 'filearea'=>$filearea, 'itemid'=>$itemid, 'filepath'=>'/');
225 ob_start();
226 if (!$imagefnc($im1, NULL, $quality, $filters)) {
227 // keep old icons
228 ob_end_clean();
229 return false;
231 $data = ob_get_clean();
232 imagedestroy($im1);
233 $icon['filename'] = 'f1'.$imageext;
234 $fs->delete_area_files($context->id, $component, $filearea, $itemid);
235 $file1 = $fs->create_file_from_string($icon, $data);
237 ob_start();
238 if (!$imagefnc($im2, NULL, $quality, $filters)) {
239 ob_end_clean();
240 $fs->delete_area_files($context->id, $component, $filearea, $itemid);
241 return false;
243 $data = ob_get_clean();
244 imagedestroy($im2);
245 $icon['filename'] = 'f2'.$imageext;
246 $fs->create_file_from_string($icon, $data);
248 ob_start();
249 if (!$imagefnc($im3, NULL, $quality, $filters)) {
250 ob_end_clean();
251 $fs->delete_area_files($context->id, $component, $filearea, $itemid);
252 return false;
254 $data = ob_get_clean();
255 imagedestroy($im3);
256 $icon['filename'] = 'f3'.$imageext;
257 $fs->create_file_from_string($icon, $data);
259 return $file1->get_id();
263 * Generates a thumbnail for the given image
265 * If the GD library has at least version 2 and PNG support is available, the returned data
266 * is the content of a transparent PNG file containing the thumbnail. Otherwise, the function
267 * returns contents of a JPEG file with black background containing the thumbnail.
269 * @param string $filepath the full path to the original image file
270 * @param int $width the width of the requested thumbnail
271 * @param int $height the height of the requested thumbnail
272 * @return string|bool false if a problem occurs, the thumbnail image data otherwise
274 function generate_image_thumbnail($filepath, $width, $height) {
275 global $CFG;
277 if (empty($CFG->gdversion) or empty($filepath) or empty($width) or empty($height)) {
278 return false;
281 $imageinfo = getimagesize($filepath);
283 if (empty($imageinfo)) {
284 return false;
287 $originalwidth = $imageinfo[0];
288 $originalheight = $imageinfo[1];
290 if (empty($originalwidth) or empty($originalheight)) {
291 return false;
294 $original = imagecreatefromstring(file_get_contents($filepath));
296 if (function_exists('imagepng')) {
297 $imagefnc = 'imagepng';
298 $filters = PNG_NO_FILTER;
299 $quality = 1;
300 } else if (function_exists('imagejpeg')) {
301 $imagefnc = 'imagejpeg';
302 $filters = null;
303 $quality = 90;
304 } else {
305 debugging('Neither JPEG nor PNG are supported at this server, please fix the system configuration.');
306 return false;
309 if (function_exists('imagecreatetruecolor') and $CFG->gdversion >= 2) {
310 $thumbnail = imagecreatetruecolor($width, $height);
311 if ($imagefnc === 'imagepng') {
312 imagealphablending($thumbnail, false);
313 imagefill($thumbnail, 0, 0, imagecolorallocatealpha($thumbnail, 0, 0, 0, 127));
314 imagesavealpha($thumbnail, true);
316 } else {
317 $thumbnail = imagecreate($width, $height);
320 $ratio = min($width / $originalwidth, $height / $originalheight);
322 if ($ratio < 1) {
323 $targetwidth = floor($originalwidth * $ratio);
324 $targetheight = floor($originalheight * $ratio);
325 } else {
326 // do not enlarge the original file if it is smaller than the requested thumbnail size
327 $targetwidth = $originalwidth;
328 $targetheight = $originalheight;
331 $dstx = floor(($width - $targetwidth) / 2);
332 $dsty = floor(($height - $targetheight) / 2);
334 imagecopybicubic($thumbnail, $original, $dstx, $dsty, 0, 0, $targetwidth, $targetheight, $originalwidth, $originalheight);
336 ob_start();
337 if (!$imagefnc($thumbnail, null, $quality, $filters)) {
338 ob_end_clean();
339 return false;
341 $data = ob_get_clean();
342 imagedestroy($original);
343 imagedestroy($thumbnail);
345 return $data;