/** * Generates a thumbnail for the given image string. * * If the GD library has at least version 2 and PNG support is available, the returned data * is the content of a transparent PNG file containing the thumbnail. Otherwise, the function * returns contents of a JPEG file with black background containing the thumbnail. * * @param resource $original The image to work on. * @param array $imageinfo Contains [0] => originalwidth, [1] => originalheight. * @param int $width The width of the requested thumbnail. * @param int $height The height of the requested thumbnail. * @return string|bool False if a problem occurs, the thumbnail image data otherwise. */ function generate_image_thumbnail_from_image($original, $imageinfo, $width, $height) { return resize_image_from_image($original, $imageinfo, $width, $height, true); }
/** * Generate a resized image for this stored_file. * * @param int|null $width The desired width, or null to only use the height. * @param int|null $height The desired height, or null to only use the width. * @return string|false False when a problem occurs, else the image data. */ public function resize_image($width, $height) { global $CFG; require_once $CFG->libdir . '/gdlib.php'; // Fetch the image information for this image. $imageinfo = @getimagesizefromstring($this->get_content()); if (empty($imageinfo)) { return false; } // Create a new image from the file. $original = @imagecreatefromstring($this->get_content()); // Generate the resized image. return resize_image_from_image($original, $imageinfo, $width, $height); }
public function test_resize_image_from_image() { global $CFG; require_once $CFG->libdir . '/gdlib.php'; $pngpath = $this->fixturepath . 'gd-logo.png'; $origimageinfo = getimagesize($pngpath); $imagecontent = file_get_contents($pngpath); // Preferred height. $imageresource = imagecreatefromstring($imagecontent); $newpng = resize_image_from_image($imageresource, $origimageinfo, null, 24); $this->assertTrue(is_string($newpng)); $imageinfo = getimagesizefromstring($newpng); $this->assertEquals(89, $imageinfo[0]); $this->assertEquals(24, $imageinfo[1]); $this->assertEquals('image/png', $imageinfo['mime']); // Preferred width. $imageresource = imagecreatefromstring($imagecontent); $newpng = resize_image_from_image($imageresource, $origimageinfo, 100, null); $this->assertTrue(is_string($newpng)); $imageinfo = getimagesizefromstring($newpng); $this->assertEquals(100, $imageinfo[0]); $this->assertEquals(26, $imageinfo[1]); $this->assertEquals('image/png', $imageinfo['mime']); // Preferred width and height. $imageresource = imagecreatefromstring($imagecontent); $newpng = resize_image_from_image($imageresource, $origimageinfo, 50, 50); $this->assertTrue(is_string($newpng)); $imageinfo = getimagesizefromstring($newpng); $this->assertEquals(50, $imageinfo[0]); $this->assertEquals(13, $imageinfo[1]); $this->assertEquals('image/png', $imageinfo['mime']); }