PortableInfobox/includes/services/Helpers/PortableInfoboxImagesHelper.php

136 lines
3.8 KiB
PHP
Raw Normal View History

<?php
namespace PortableInfobox\Helpers;
2016-12-29 10:58:27 +00:00
class PortableInfoboxImagesHelper {
2015-09-09 08:16:00 +00:00
const MAX_DESKTOP_THUMBNAIL_HEIGHT = 500;
/**
* extends image data
*
2018-08-22 14:20:49 +00:00
* @param \File|\Title|string $file image
* @param int $thumbnailFileWidth preferred thumbnail file width
2018-08-16 09:25:53 +00:00
* @param int|null $thumbnailImgTagWidth preferred thumbnail img tag width
* @return array|bool false on failure
*/
2018-08-22 14:20:49 +00:00
public function extendImageData( $file, $thumbnailFileWidth, $thumbnailImgTagWidth = null ) {
global $wgPortableInfoboxCustomImageWidth;
2018-08-22 14:20:49 +00:00
$file = $this->getFile( $file );
2018-08-22 14:20:49 +00:00
if ( !$file || !in_array( $file->getMediaType(), [ MEDIATYPE_BITMAP, MEDIATYPE_DRAWING ] ) ) {
return [];
}
// get dimensions
2016-03-17 11:12:36 +00:00
$originalWidth = $file->getWidth();
2018-10-02 07:41:19 +00:00
// we need to have different thumbnail file dimensions to support (not to have pixelated images)
// wider infoboxes than default width
$fileDimensions = $this->getThumbnailSizes(
$thumbnailFileWidth,
self::MAX_DESKTOP_THUMBNAIL_HEIGHT,
$originalWidth,
$file->getHeight()
);
$imgTagDimensions =
empty( $thumbnailImgTagWidth )
? $fileDimensions
: $this->getThumbnailSizes( $thumbnailImgTagWidth,
self::MAX_DESKTOP_THUMBNAIL_HEIGHT, $originalWidth, $file->getHeight() );
2016-03-17 11:12:36 +00:00
// if custom and big enough, scale thumbnail size
$ratio =
!empty( $wgPortableInfoboxCustomImageWidth ) &&
$originalWidth > $wgPortableInfoboxCustomImageWidth
? $wgPortableInfoboxCustomImageWidth / $fileDimensions['width'] : 1;
// get thumbnail
$thumbnail = $file->transform( [
'width' => round( $fileDimensions['width'] * $ratio ),
'height' => round( $fileDimensions['height'] * $ratio ),
] );
$thumbnail2x = $file->transform( [
'width' => round( $fileDimensions['width'] * $ratio * 2 ),
'height' => round( $fileDimensions['height'] * $ratio * 2 ),
] );
if ( !$thumbnail || $thumbnail->isError() || !$thumbnail2x || $thumbnail2x->isError() ) {
return [];
}
2018-08-22 14:20:49 +00:00
return [
'height' => intval( $imgTagDimensions['height'] ),
'width' => intval( $imgTagDimensions['width'] ),
'thumbnail' => $thumbnail->getUrl(),
2018-08-07 12:13:14 +00:00
'thumbnail2x' => $thumbnail2x->getUrl()
2018-08-22 14:20:49 +00:00
];
}
/**
* @param array $images
* @return array
*/
public function extendImageCollectionData( $images ) {
$images = array_map(
function ( $image, $index ) {
2018-08-22 14:20:49 +00:00
$image['ref'] = $index + 1;
if ( empty( $image['caption'] ) ) {
$image['caption'] = $image['name'];
}
return $image;
},
$images,
2018-08-16 09:25:53 +00:00
array_keys( $images )
);
$images[0]['isFirst'] = true;
2018-08-22 14:20:49 +00:00
return $images;
}
/**
* Calculates image dimensions based on preferred width and max acceptable height
2015-12-11 10:56:41 +00:00
*
* @param int $preferredWidth
* @param int $maxHeight
* @param int $originalWidth
* @param int $originalHeight
* @return array [ 'width' => int, 'height' => int ]
2015-09-09 08:16:00 +00:00
*/
public function getThumbnailSizes( $preferredWidth, $maxHeight, $originalWidth, $originalHeight ) {
if ( ( $originalHeight / $originalWidth ) > ( $maxHeight / $preferredWidth ) ) {
$height = min( $maxHeight, $originalHeight );
$width = min( $preferredWidth, $height * $originalWidth / $originalHeight );
2015-12-11 10:56:41 +00:00
} else {
$width = min( $preferredWidth, $originalWidth );
$height = min( $maxHeight, $width * $originalHeight / $originalWidth );
2015-12-11 10:56:41 +00:00
}
return [ 'height' => round( $height ), 'width' => round( $width ) ];
2015-12-11 10:56:41 +00:00
}
/**
2018-08-22 14:20:49 +00:00
* Get file
*
2018-08-22 14:20:49 +00:00
* Note: this method turns a string $file into an object, affecting the calling code version
* of this variable
*
2018-08-22 14:20:49 +00:00
* @param \File|\Title|string $file
* @return \File|null file
2018-08-08 09:42:22 +00:00
*/
2018-08-22 14:20:49 +00:00
public function getFile( $file ) {
if ( is_string( $file ) ) {
$file = \Title::newFromText( $file, NS_FILE );
}
if ( $file instanceof \Title ) {
$file = wfFindFile( $file );
}
2018-08-22 14:20:49 +00:00
if ( $file instanceof \File && $file->exists() ) {
return $file;
}
return null;
2018-08-08 09:42:22 +00:00
}
2015-09-09 08:01:24 +00:00
}