/** * Return a URL that displays $file shrunk to have the closest dimension meet $box. Images smaller than the * bounding box will not be affected. The part of the image that extends beyond the $box dimensions will be * cropped out. The result is an image that completely fills the box with no empty space, but is cropped. * * @param File $file * @param array $box * * @return String */ private function cropURL(File $file, array $box) { global $wgEnableVignette; if ($wgEnableVignette) { $cropUrl = $file->getUrlGenerator()->zoomCropDown()->width($box['w'])->height($box['h'])->url(); } else { list($adjWidth, $adjHeight) = $this->fitClosest($file, $box); $height = $file->getHeight(); $width = $file->getWidth(); if ($adjHeight == $box['h']) { $width = $box['w'] * ($file->getHeight() / $box['h']); } if ($adjWidth == $box['w']) { $height = $box['h'] * ($file->getWidth() / $box['w']); } $cropStr = sprintf("%dpx-0,%d,0,%d", $adjWidth, $width, $height); $append = ''; $mime = strtolower($file->getMimeType()); if ($mime == 'image/svg+xml' || $mime == 'image/svg') { $append = '.png'; } $cropUrl = wfReplaceImageServer($file->getThumbUrl($cropStr . '-' . $file->getName() . $append)); } return $cropUrl; }
/** * Return a URL that displays $file scaled and/or cropped to fill the entire square thumbnail dimensions with * no whitespace if possible. Images smaller than the thumbnail size will be enlarged if their image area (L x W) * is above a certain threshold. This threshold is expressed as a percentage of the requested thumb area and * given by: * * self::thumbEnlargeThreshold * * Small images that do not meet this threshold will be centered within the thumb container and padded with a * transparent background. * * @param File $file * @param int $dimension * @param bool $useWebP * @return string The URL of the image */ public static function getSquaredThumbnailUrl(File $file, $dimension, $useWebP = false) { // Create a new url generator $gen = $file->getUrlGenerator(); // Determine if this image falls into a small image category. We compare the area of the image with the // area of the requested thumb and use self::thumbEnlargeThreshold as the threshold for enlarging $height = $file->getHeight(); $width = $file->getWidth(); $isSmallImage = $height < $dimension || $width < $dimension; $imageBelowThreshold = $height * $width <= self::thumbEnlargeThreshold * $dimension * $dimension; // If height or width is less than a side of our square target thumbnail, we need to decide whether we're // going to enlarge it or not if ($isSmallImage && $imageBelowThreshold) { // Leave the (small) full sized image as is, but put within the requested container with transparent fill $gen->fixedAspectRatioDown()->backgroundFill('transparent'); } else { if ($height > $width) { // Portrait mode, crop at the top $gen->topCrop(); } else { // Landscape mode, crop in the middle $gen->zoomCrop(); } } if ($useWebP) { $gen->webp(); } $url = $gen->width($dimension)->height($dimension)->url(); return $url; }