function process_image_upload($field) { if (!isset($_FILES[$field]['tmp_name']) || !$_FILES[$field]['name']) { return 'empty'; } $temp_image_path = $_FILES[$field]['tmp_name']; $temp_image_name = $_FILES[$field]['name']; list($iwidth, $iheight, $temp_image_type) = getimagesize($temp_image_path); if ($temp_image_type === NULL) { return false; } elseif ($iwidth < 400) { return 'size'; } switch ($temp_image_type) { case IMAGETYPE_GIF: break; case IMAGETYPE_JPEG: break; case IMAGETYPE_PNG: break; default: return false; } $uploaded_image_path = UPLOADED_IMAGE_DESTINATION . $temp_image_name; move_uploaded_file($temp_image_path, $uploaded_image_path); $thumbnail_image_path = THUMBNAIL_IMAGE_DESTINATION . preg_replace('{\\.[^\\.]+$}', '.jpg', $temp_image_name); $main_image_path = MAIN_IMAGE_DESTINATION . preg_replace('{\\.[^\\.]+$}', '.jpg', $temp_image_name); $result = generate_image_thumbnail($uploaded_image_path, $thumbnail_image_path, 150, 150); if ($result) { $result = generate_image_thumbnail($uploaded_image_path, $main_image_path, 400, 400, true); } return $result ? array($uploaded_image_path, $thumbnail_image_path) : false; }
public function test_generate_image_thumbnail() { global $CFG; require_once $CFG->libdir . '/gdlib.php'; // Test with meaningless data. // Now use a fixture. $pngpath = $this->fixturepath . 'gd-logo.png'; $pngthumb = generate_image_thumbnail($pngpath, 24, 24); $this->assertTrue(is_string($pngthumb)); // And check that the generated image was of the correct proportions and mimetype. $imageinfo = getimagesizefromstring($pngthumb); $this->assertEquals(24, $imageinfo[0]); $this->assertEquals(24, $imageinfo[1]); $this->assertEquals('image/png', $imageinfo['mime']); }
/** * 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; } }
/** * Generates a preview for the stored image file * * @param stored_file $file the image we want to preview * @param string $mode preview mode, eg. 'thumb' * @return string|bool false if a problem occurs, the thumbnail image data otherwise */ protected function create_imagefile_preview(stored_file $file, $mode) { global $CFG; require_once $CFG->libdir . '/gdlib.php'; $tmproot = make_temp_directory('thumbnails'); $tmpfilepath = $tmproot . '/' . $file->get_contenthash(); $file->copy_content_to($tmpfilepath); if ($mode === 'tinyicon') { $data = generate_image_thumbnail($tmpfilepath, 24, 24); } else { if ($mode === 'thumb') { $data = generate_image_thumbnail($tmpfilepath, 90, 90); } else { throw new file_exception('storedfileproblem', 'Invalid preview mode requested'); } } unlink($tmpfilepath); return $data; }
/** * Returns the stored thumbnail file, generates it if not present. * * @param string $filepath current path in repository (dir and filename) * @param string $thumbsize 'thumb' or 'icon' * @return null|stored_file */ public function get_thumbnail($filepath, $thumbsize) { global $CFG; $filepath = trim($filepath, '/'); $origfile = $this->get_rootpath() . $filepath; // As thumbnail filename we use original file content hash. if (!$this->is_in_repository($filepath) || !($filecontents = @file_get_contents($origfile))) { // File is not found or is not readable. return null; } $filename = sha1($filecontents); unset($filecontents); // Try to get generated thumbnail for this file. $fs = get_file_storage(); if (!($file = $fs->get_file(SYSCONTEXTID, 'repository_filesystem', $thumbsize, $this->id, '/' . $filepath . '/', $filename))) { // Thumbnail not found . Generate and store thumbnail. require_once $CFG->libdir . '/gdlib.php'; if ($thumbsize === 'thumb') { $size = 90; } else { $size = 24; } if (!($data = @generate_image_thumbnail($origfile, $size, $size))) { // Generation failed. return null; } $record = array('contextid' => SYSCONTEXTID, 'component' => 'repository_filesystem', 'filearea' => $thumbsize, 'itemid' => $this->id, 'filepath' => '/' . $filepath . '/', 'filename' => $filename); $file = $fs->create_file_from_string($record, $data); } return $file; }
public function create_section_image($tempfile, $storedfilerecord, $sectionimage) { global $DB, $CFG; require_once $CFG->libdir . '/gdlib.php'; $fs = get_file_storage(); try { $convertsuccess = true; // Ensure the right quality setting... $mime = $tempfile->get_mimetype(); $imageinfo = $tempfile->get_imageinfo(); $imagemaxwidth = self::get_maximum_image_width(); if ($imageinfo['width'] > $imagemaxwidth) { // Maximum width as defined in lib.php. // Recalculate height:... $ratio = $imagemaxwidth / $imageinfo['width']; $imageinfo['height'] = $imageinfo['height'] * $ratio; // Maintain image's aspect ratio. // Set width:... $imageinfo['width'] = $imagemaxwidth; } $storedfilerecord['mimetype'] = $mime; // Note: It appears that this works with transparent Gif's to, so simplifying. $tmproot = make_temp_directory('gridformaticon'); $tmpfilepath = $tmproot . '/' . $tempfile->get_contenthash(); $tempfile->copy_content_to($tmpfilepath); $data = generate_image_thumbnail($tmpfilepath, $imageinfo['width'], $imageinfo['height']); if (!empty($data)) { $fs->create_file_from_string($storedfilerecord, $data); } else { $convertsuccess = false; } unlink($tmpfilepath); $tempfile->delete(); unset($tempfile); if ($convertsuccess == true) { $DB->set_field('format_grid_icon', 'image', $storedfilerecord['filename'], array('sectionid' => $storedfilerecord['itemid'])); // Set up the displayed image:... $sectionimage->newimage = $storedfilerecord['filename']; $this->setup_displayed_image($sectionimage, $storedfilerecord['contextid'], $this->get_settings()); } else { print_error('imagecannotbeused', 'format_grid', $CFG->wwwroot . "/course/view.php?id=" . $this->courseid); } } catch (Exception $e) { if (isset($tempfile)) { $tempfile->delete(); unset($tempfile); } print 'Grid Format Image Exception:...'; debugging($e->getMessage()); error_log($e->getMessage() . print_r($e, true)); } }