/** * Shame that this was nicked from gdlib.php and that there isn't a function I could have used from there. * Creates a resized version of image and stores copy in file area * * @param context $context * @param string $component * @param string filearea * @param int $itemid * @param stored_file $originalfile * @param int $newwidth; * @param int $newheight; * @return stored_file */ public static function resize(\stored_file $originalfile, $resizefilename = false, $newwidth = false, $newheight = false, $jpgquality = 90) { if ($resizefilename === false) { $resizefilename = $originalfile->get_filename(); } if (!$newwidth && !$newheight) { return false; } $contextid = $originalfile->get_contextid(); $component = $originalfile->get_component(); $filearea = $originalfile->get_filearea(); $itemid = $originalfile->get_itemid(); $imageinfo = (object) $originalfile->get_imageinfo(); $imagefnc = ''; if (empty($imageinfo)) { return false; } // Create temporary image for processing. $tmpimage = tempnam(sys_get_temp_dir(), 'tmpimg'); \file_put_contents($tmpimage, $originalfile->get_content()); if (!$newheight) { $m = $imageinfo->height / $imageinfo->width; // Multiplier to work out $newheight. $newheight = $newwidth * $m; } else { if (!$newwidth) { $m = $imageinfo->width / $imageinfo->height; // Multiplier to work out $newwidth. $newwidth = $newheight * $m; } } $t = null; switch ($imageinfo->mimetype) { case 'image/gif': if (\function_exists('imagecreatefromgif')) { $im = \imagecreatefromgif($tmpimage); } else { \debugging('GIF not supported on this server'); unlink($tmpimage); return false; } // Guess transparent colour from GIF. $transparent = \imagecolortransparent($im); if ($transparent != -1) { $t = \imagecolorsforindex($im, $transparent); } break; case 'image/jpeg': if (\function_exists('imagecreatefromjpeg')) { $im = \imagecreatefromjpeg($tmpimage); } else { \debugging('JPEG not supported on this server'); unlink($tmpimage); return false; } // If the user uploads a jpeg them we should process as a jpeg if possible. if (\function_exists('imagejpeg')) { $imagefnc = 'imagejpeg'; $filters = null; // Not used. $quality = $jpgquality; } else { if (\function_exists('imagepng')) { $imagefnc = 'imagepng'; $filters = PNG_NO_FILTER; $quality = 1; } else { \debugging('Jpeg and png not supported on this server, please fix server configuration'); unlink($tmpimage); return false; } } break; case 'image/png': if (\function_exists('imagecreatefrompng')) { $im = \imagecreatefrompng($tmpimage); } else { \debugging('PNG not supported on this server'); unlink($tmpimage); return false; } break; default: unlink($tmpimage); return false; } unlink($tmpimage); // The default for all images other than jpegs is to try imagepng first. if (empty($imagefnc)) { if (\function_exists('imagepng')) { $imagefnc = 'imagepng'; $filters = PNG_NO_FILTER; $quality = 1; } else { if (\function_exists('imagejpeg')) { $imagefnc = 'imagejpeg'; $filters = null; // Not used. $quality = $jpgquality; } else { \debugging('Jpeg and png not supported on this server, please fix server configuration'); return false; } } } if (\function_exists('imagecreatetruecolor')) { $newimage = \imagecreatetruecolor($newwidth, $newheight); if ($imageinfo->mimetype != 'image/jpeg' and $imagefnc === 'imagepng') { if ($t) { // Transparent GIF hacking... $transparentcolour = \imagecolorallocate($newimage, $t['red'], $t['green'], $t['blue']); \imagecolortransparent($newimage, $transparentcolour); } \imagealphablending($newimage, false); $color = \imagecolorallocatealpha($newimage, 0, 0, 0, 127); \imagefill($newimage, 0, 0, $color); \imagesavealpha($newimage, true); } } else { $newimage = \imagecreate($newwidth, $newheight); } \imagecopybicubic($newimage, $im, 0, 0, 0, 0, $newwidth, $newheight, $imageinfo->width, $imageinfo->height); $fs = \get_file_storage(); $newimageparams = array('contextid' => $contextid, 'component' => $component, 'filearea' => $filearea, 'itemid' => $itemid, 'filepath' => '/'); \ob_start(); if (!$imagefnc($newimage, null, $quality, $filters)) { return false; } $data = \ob_get_clean(); \imagedestroy($newimage); $newimageparams['filename'] = $resizefilename; if ($resizefilename == $originalfile->get_filename()) { $originalfile->delete(); } $file1 = $fs->create_file_from_string($newimageparams, $data); return $file1; }
/** * Resize the image, if required, then generate an img tag and, if required, a link to the full-size image * @param stored_file $file the image file to process * @param int $maxwidth the maximum width allowed for the image * @param int $maxheight the maximum height allowed for the image * @return string HTML fragment to add to the label */ function label_generate_resized_image(stored_file $file, $maxwidth, $maxheight) { global $CFG; $fullurl = moodle_url::make_draftfile_url($file->get_itemid(), $file->get_filepath(), $file->get_filename()); $link = null; $attrib = array('alt' => $file->get_filename(), 'src' => $fullurl); if ($imginfo = $file->get_imageinfo()) { // Work out the new width / height, bounded by maxwidth / maxheight $width = $imginfo['width']; $height = $imginfo['height']; if (!empty($maxwidth) && $width > $maxwidth) { $height *= (double) $maxwidth / $width; $width = $maxwidth; } if (!empty($maxheight) && $height > $maxheight) { $width *= (double) $maxheight / $height; $height = $maxheight; } $attrib['width'] = $width; $attrib['height'] = $height; // If the size has changed and the image is of a suitable mime type, generate a smaller version if ($width != $imginfo['width']) { $mimetype = $file->get_mimetype(); if ($mimetype === 'image/gif' or $mimetype === 'image/jpeg' or $mimetype === 'image/png') { require_once $CFG->libdir . '/gdlib.php'; $tmproot = make_temp_directory('mod_label'); $tmpfilepath = $tmproot . '/' . $file->get_contenthash(); $file->copy_content_to($tmpfilepath); $data = generate_image_thumbnail($tmpfilepath, $width, $height); unlink($tmpfilepath); if (!empty($data)) { $fs = get_file_storage(); $record = array('contextid' => $file->get_contextid(), 'component' => $file->get_component(), 'filearea' => $file->get_filearea(), 'itemid' => $file->get_itemid(), 'filepath' => '/', 'filename' => 's_' . $file->get_filename()); $smallfile = $fs->create_file_from_string($record, $data); // Replace the image 'src' with the resized file and link to the original $attrib['src'] = moodle_url::make_draftfile_url($smallfile->get_itemid(), $smallfile->get_filepath(), $smallfile->get_filename()); $link = $fullurl; } } } } else { // Assume this is an image type that get_imageinfo cannot handle (e.g. SVG) $attrib['width'] = $maxwidth; } $img = html_writer::empty_tag('img', $attrib); if ($link) { return html_writer::link($link, $img); } else { return $img; } }
/** * Get the image details from a file and return them. * @param stored_file $file * @param $pagecount * @return mixed array|false */ protected static function get_image_details($file, $pagecount) { if ($imageinfo = $file->get_imageinfo()) { $imgurl = moodle_url::make_pluginfile_url($file->get_contextid(), $file->get_component(), $file->get_filearea(), $file->get_itemid(), $file->get_filepath(), $file->get_filename()); // Prevent browser from caching image if it has changed. $imgurl->param('ts', $file->get_timemodified()); return array($imgurl, $imageinfo['width'], $imageinfo['height'], $pagecount); } // Something went wrong. return false; }