public function process_form() { $x1 = required_param('x1', PARAM_INT); $y1 = required_param('y1', PARAM_INT); $x2 = required_param('x2', PARAM_INT); $y2 = required_param('y2', PARAM_INT); $width = $x2 - $x1; $height = $y2 - $y1; if ($width > 0 && $height > 0) { $cropped = $this->imageobj->create_new_image($width, $height); imagecopybicubic($cropped, $this->imageobj->image, 0, 0, $x1, $y1, $width, $height, $width, $height); $this->imageobj->save_image($cropped); } }
function process_form() { $x1 = required_param('x1', PARAM_INT); $y1 = required_param('y1', PARAM_INT); $x2 = required_param('x2', PARAM_INT); $y2 = required_param('y2', PARAM_INT); $width = $x2 - $x1; $height = $y2 - $y1; $info = lightboxgallery_image_info($this->imagepath); if ($width > 0 && $height > 0) { if ($image = lightboxgallery_imagecreatefromtype($info->imagesize[2], $this->imagepath)) { $cropped = lightboxgallery_image_create($width, $height); imagecopybicubic($cropped, $image, 0, 0, $x1, $y1, $width, $height, $width, $height); $this->save_image_resource($cropped, $info->imagesize[2]); } } }
function resize($width, $height, $offsetx = 0, $offsety = 0, $return = false) { $resized = $this->create_new_image($width, $height); $cx = $this->width / 2; $cy = $this->height / 2; $ratiow = $width / $this->width; $ratioh = $height / $this->height; if ($ratiow < $ratioh) { $srcw = floor($width / $ratioh); $srch = $this->height; $srcx = floor($cx - $srcw / 2) + $offsetx; $srcy = $offsety; } else { $srcw = $this->width; $srch = floor($height / $ratiow); $srcx = $offsetx; $srcy = floor($cy - $srch / 2) + $offsety; } imagecopybicubic($resized, $this->image, 0, 0, $srcx, $srcy, $width, $height, $srcw, $srch); return $return ? $resized : $this->save_image($resized); }
/** * Generates a thumbnail for the given image * * 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 string $filepath the full path to the original image file * @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($filepath, $width, $height) { global $CFG; if (empty($CFG->gdversion) or empty($filepath) or empty($width) or empty($height)) { return false; } $imageinfo = getimagesize($filepath); if (empty($imageinfo)) { return false; } $originalwidth = $imageinfo[0]; $originalheight = $imageinfo[1]; if (empty($originalwidth) or empty($originalheight)) { return false; } $original = imagecreatefromstring(file_get_contents($filepath)); if (function_exists('imagepng')) { $imagefnc = 'imagepng'; $filters = PNG_NO_FILTER; $quality = 1; } else { if (function_exists('imagejpeg')) { $imagefnc = 'imagejpeg'; $filters = null; $quality = 90; } else { debugging('Neither JPEG nor PNG are supported at this server, please fix the system configuration.'); return false; } } if (function_exists('imagecreatetruecolor') and $CFG->gdversion >= 2) { $thumbnail = imagecreatetruecolor($width, $height); if ($imagefnc === 'imagepng') { imagealphablending($thumbnail, false); imagefill($thumbnail, 0, 0, imagecolorallocatealpha($thumbnail, 0, 0, 0, 127)); imagesavealpha($thumbnail, true); } } else { $thumbnail = imagecreate($width, $height); } $ratio = min($width / $originalwidth, $height / $originalheight); if ($ratio < 1) { $targetwidth = floor($originalwidth * $ratio); $targetheight = floor($originalheight * $ratio); } else { // do not enlarge the original file if it is smaller than the requested thumbnail size $targetwidth = $originalwidth; $targetheight = $originalheight; } $dstx = floor(($width - $targetwidth) / 2); $dsty = floor(($height - $targetheight) / 2); imagecopybicubic($thumbnail, $original, $dstx, $dsty, 0, 0, $targetwidth, $targetheight, $originalwidth, $originalheight); ob_start(); if (!$imagefnc($thumbnail, null, $quality, $filters)) { ob_end_clean(); return false; } $data = ob_get_clean(); imagedestroy($original); imagedestroy($thumbnail); return $data; }
private function get_image_resized($height = THUMBNAIL_HEIGHT, $width = THUMBNAIL_WIDTH, $offsetx = 0, $offsety = 0) { $image = imagecreatefromstring($this->stored_file->get_content()); $resized = imagecreatetruecolor($width, $height); imagealphablending($resized, false); imagesavealpha($resized, true); $cx = $this->width / 2; $cy = $this->height / 2; $ratiow = $width / $this->width; $ratioh = $height / $this->height; if ($ratiow < $ratioh) { $srcw = floor($width / $ratioh); $srch = $this->height; $srcx = floor($cx - $srcw / 2) + $offsetx; $srcy = $offsety; } else { $srcw = $this->width; $srch = floor($height / $ratiow); $srcx = $offsetx; $srcy = floor($cy - $srch / 2) + $offsety; } imagecopybicubic($resized, $image, 0, 0, $srcx, $srcy, $width, $height, $srcw, $srch); return $resized; }
private function get_image_resized(\stored_file $file = null, $height = 250, $width = 250, $offsetx = 0, $offsety = 0, $crop = true) { global $CFG; if (is_null($file) && !($file = $this->get_stored_file_by_type('item'))) { return false; } require_once $CFG->libdir . '/gdlib.php'; $oldmemlimit = @ini_get('memory_limit'); raise_memory_limit(MEMORY_EXTRA); $tempfile = $file->copy_content_to_temp(); if (!imagehelper::memory_check($tempfile)) { return false; } $image = imagecreatefromstring(file_get_contents($tempfile)); // Func exif_read_data is only supported for jpeg/tiff images. $mimetype = $file->get_mimetype(); $isjpegortiff = $mimetype == 'image/jpeg' || $mimetype == 'image/tiff'; if ($isjpegortiff && ($exif = exif_read_data($tempfile))) { $ort = 1; if (isset($exif['IFD0']['Orientation'])) { $ort = $exif['IFD0']['Orientation']; } else { if (isset($exif['Orientation'])) { $ort = $exif['Orientation']; } } $mirror = false; $degree = 0; switch ($ort) { case 2: // Horizontal flip. $mirror = true; break; case 3: // 180 Rotate left. $degree = 180; break; case 4: // Vertical flip. $degree = 180; $mirror = true; break; case 5: // Vertical flip + 90 rotate right. $degree = 270; $mirror = true; break; case 6: // 90 rotate right. $degree = 270; break; case 7: // Horizontal flip + 90 rotate right. $degree = 90; $mirror = true; break; case 8: // 90 rotate left. $degree = 90; break; default: // Do nothing. break; } if ($degree) { $image = imagerotate($image, $degree, 0); } if ($mirror) { $image = imagehelper::mirror($image); } } $info = array('width' => imagesx($image), 'height' => imagesy($image)); $cx = $info['width'] / 2; $cy = $info['height'] / 2; $ratiow = $width / $info['width']; $ratioh = $height / $info['height']; if ($info['width'] <= $width && $info['height'] <= $height) { // Images containing EXIF orientation data don't display correctly in browsers. // So even though we're not making a smaller version of the original here, we still // want to have it displayed right-side up. $width = $info['width']; $height = $info['height']; $ratiow = $width / $info['width']; $ratioh = $height / $info['height']; } if (!$crop) { if ($ratiow < $ratioh) { $height = floor($info['height'] * $ratiow); $width = floor($info['width'] * $ratiow); } else { $height = floor($info['height'] * $ratioh); $width = floor($info['width'] * $ratioh); } $srcw = $info['width']; $srch = $info['height']; $srcx = 0; $srcy = 0; } else { if ($ratiow < $ratioh) { $srcw = floor($width / $ratioh); $srch = $info['height']; $srcx = floor($cx - $srcw / 2) + $offsetx; $srcy = $offsety; } else { $srcw = $info['width']; $srch = floor($height / $ratiow); $srcx = $offsetx; $srcy = floor($cy - $srch / 2) + $offsety; } } $resized = imagecreatetruecolor($width, $height); imagecopybicubic($resized, $image, 0, 0, $srcx, $srcy, $width, $height, $srcw, $srch); unset($image); reduce_memory_limit($oldmemlimit); return $resized; }
/** * Resize an image from an image object. * * @param resource $original The image to work on. * @param array $imageinfo Contains [0] => originalwidth, [1] => originalheight. * @param int|null $width The max width of the resized image, or null to only use the height. * @param int|null $height The max height of the resized image, or null to only use the width. * @param bool $forcecanvas Whether the final dimensions should be set to $width and $height. * @return string|bool False if a problem occurs, else the resized image data. */ function resize_image_from_image($original, $imageinfo, $width, $height, $forcecanvas = false) { global $CFG; if (empty($width) && empty($height) || $forcecanvas && (empty($width) || empty($height))) { // We need do not have the required ddimensions to work with. return false; } if (empty($imageinfo)) { return false; } $originalwidth = $imageinfo[0]; $originalheight = $imageinfo[1]; if (empty($originalwidth) or empty($originalheight)) { return false; } if (function_exists('imagepng')) { $imagefnc = 'imagepng'; $filters = PNG_NO_FILTER; $quality = 1; } else { if (function_exists('imagejpeg')) { $imagefnc = 'imagejpeg'; $filters = null; $quality = 90; } else { debugging('Neither JPEG nor PNG are supported at this server, please fix the system configuration.'); return false; } } if (empty($height)) { $ratio = $width / $originalwidth; } else { if (empty($width)) { $ratio = $height / $originalheight; } else { $ratio = min($width / $originalwidth, $height / $originalheight); } } if ($ratio < 1) { $targetwidth = floor($originalwidth * $ratio); $targetheight = floor($originalheight * $ratio); } else { // Do not enlarge the original file if it is smaller than the requested thumbnail size. $targetwidth = $originalwidth; $targetheight = $originalheight; } $canvaswidth = $targetwidth; $canvasheight = $targetheight; $dstx = 0; $dsty = 0; if ($forcecanvas) { $canvaswidth = $width; $canvasheight = $height; $dstx = floor(($width - $targetwidth) / 2); $dsty = floor(($height - $targetheight) / 2); } if (function_exists('imagecreatetruecolor')) { $newimage = imagecreatetruecolor($canvaswidth, $canvasheight); if ($imagefnc === 'imagepng') { imagealphablending($newimage, false); imagefill($newimage, 0, 0, imagecolorallocatealpha($newimage, 0, 0, 0, 127)); imagesavealpha($newimage, true); } } else { $newimage = imagecreate($canvaswidth, $canvasheight); } imagecopybicubic($newimage, $original, $dstx, $dsty, 0, 0, $targetwidth, $targetheight, $originalwidth, $originalheight); // Capture the image as a string object, rather than straight to file. ob_start(); if (!$imagefnc($newimage, null, $quality, $filters)) { ob_end_clean(); return false; } $data = ob_get_clean(); imagedestroy($original); imagedestroy($newimage); return $data; }
/** * Generates a thumbnail for the given image * * 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 string $filepath the full path to the original image file * @param int $requestedwidth the width of the requested image. * @param int $requestedheight the height of the requested image. * @param bool false = scale, true = crop. * @return string|bool false if a problem occurs or the image data. */ private static function generate_image($filepath, $requestedwidth, $requestedheight, $crop) { if (empty($filepath) or empty($requestedwidth) or empty($requestedheight)) { return false; } $imageinfo = getimagesize($filepath); if (empty($imageinfo)) { return false; } $originalwidth = $imageinfo[0]; $originalheight = $imageinfo[1]; if (empty($originalwidth) or empty($originalheight)) { return false; } $original = imagecreatefromstring(file_get_contents($filepath)); if (function_exists('imagepng')) { $imagefnc = 'imagepng'; $filters = PNG_NO_FILTER; $quality = 1; } else { if (function_exists('imagejpeg')) { $imagefnc = 'imagejpeg'; $filters = null; $quality = 90; } else { debugging('Neither JPEG nor PNG are supported at this server, please fix the system configuration to have the GD PHP extension installed.'); return false; } } $width = $requestedwidth; $height = $requestedheight; // Note: Code transformed from original 'resizeAndCrop' in 'imagelib.php' in the Moodle 1.9 version. if ($crop) { $ratio = $width / $height; $originalratio = $originalwidth / $originalheight; if ($originalratio < $ratio) { // Change the supplied height - 'resizeToWidth'. $ratio = $width / $originalwidth; $height = $originalheight * $ratio; $cropheight = true; } else { // Change the supplied width - 'resizeToHeight'. $ratio = $height / $originalheight; $width = $originalwidth * $ratio; $cropheight = false; } } if (function_exists('imagecreatetruecolor')) { $tempimage = imagecreatetruecolor($width, $height); if ($imagefnc === 'imagepng') { imagealphablending($tempimage, false); imagefill($tempimage, 0, 0, imagecolorallocatealpha($tempimage, 0, 0, 0, 127)); imagesavealpha($tempimage, true); } } else { $tempimage = imagecreate($width, $height); } if (self::is_developer_debug()) { error_log('format_grid::generate_image - original image size, width:' . imagesx($original) . ' height:' . imagesy($original)); error_log('format_grid::generate_image - temp image size, width:' . imagesx($tempimage) . ' height:' . imagesy($tempimage)); } if ($crop) { // First step, resize. imagecopybicubic($tempimage, $original, 0, 0, 0, 0, $width, $height, $originalwidth, $originalheight); imagedestroy($original); $original = $tempimage; // Second step, crop. if ($cropheight) { // Reset after change for resizeToWidth. $height = $requestedheight; // 'cropCenterHeight'. $width = imagesx($original); $srcoffset = imagesy($original) / 2 - $height / 2; } else { // Reset after change for resizeToHeight. $width = $requestedwidth; // 'cropCenterWidth'. $height = imagesy($original); $srcoffset = imagesx($original) / 2 - $width / 2; } if (function_exists('imagecreatetruecolor')) { $finalimage = imagecreatetruecolor($width, $height); if ($imagefnc === 'imagepng') { imagealphablending($finalimage, false); imagefill($finalimage, 0, 0, imagecolorallocatealpha($tempimage, 0, 0, 0, 127)); imagesavealpha($finalimage, true); } } else { $finalimage = imagecreate($width, $height); } if ($cropheight) { // 'cropCenterHeight'. imagecopybicubic($finalimage, $original, 0, 0, 0, $srcoffset, $width, $height, $width, $height); } else { // 'cropCenterWidth'. imagecopybicubic($finalimage, $original, 0, 0, $srcoffset, 0, $width, $height, $width, $height); } if (self::is_developer_debug()) { error_log('format_grid::generate_image(crop) - original image size, width:' . imagesx($original) . ' height:' . imagesy($original)); error_log('format_grid::generate_image(crop) - final image size, width:' . imagesx($finalimage) . ' height:' . imagesy($tempimage) . ' $srcoffset:' . $srcoffset); } } else { $finalimage = $tempimage; $ratio = min($width / $originalwidth, $height / $originalheight); if ($ratio < 1) { $targetwidth = floor($originalwidth * $ratio); $targetheight = floor($originalheight * $ratio); } else { // do not enlarge the original file if it is smaller than the requested thumbnail size $targetwidth = $originalwidth; $targetheight = $originalheight; } $dstx = floor(($width - $targetwidth) / 2); $dsty = floor(($height - $targetheight) / 2); imagecopybicubic($finalimage, $original, $dstx, $dsty, 0, 0, $targetwidth, $targetheight, $originalwidth, $originalheight); } ob_start(); if (!$imagefnc($finalimage, null, $quality, $filters)) { ob_end_clean(); return false; } $data = ob_get_clean(); if (self::is_developer_debug()) { error_log('format_grid::generate_image - new image size, width:' . imagesx($finalimage) . ' height:' . imagesy($finalimage)); } imagedestroy($original); imagedestroy($finalimage); return $data; }
/** * 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; }