2015-07-28 11:49:17 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Wikia\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;
|
2015-07-28 11:49:17 +00:00
|
|
|
|
2018-08-03 12:24:42 +00:00
|
|
|
protected static $count = 0;
|
|
|
|
|
2015-07-28 11:49:17 +00:00
|
|
|
/**
|
|
|
|
* extends image data
|
|
|
|
*
|
2016-03-17 09:37:18 +00:00
|
|
|
* @param array $data image data
|
2017-01-13 12:57:17 +00:00
|
|
|
* @param int $thumbnailFileWidth preferred thumbnail file width
|
|
|
|
* @param int $thumbnailImgTagWidth preferred thumbnail img tag width
|
2016-03-17 09:37:18 +00:00
|
|
|
* @return array|bool false on failure
|
2015-07-28 11:49:17 +00:00
|
|
|
*/
|
2017-01-13 12:57:17 +00:00
|
|
|
public function extendImageData( $data, $thumbnailFileWidth, $thumbnailImgTagWidth = null ) {
|
2016-03-17 09:37:18 +00:00
|
|
|
global $wgPortableInfoboxCustomImageWidth;
|
2015-07-28 11:49:17 +00:00
|
|
|
|
2016-03-17 09:37:18 +00:00
|
|
|
// title param is provided through reference in WikiaFileHelper::getFileFromTitle
|
2016-12-29 10:28:26 +00:00
|
|
|
$title = $data['name'];
|
2016-03-17 09:37:18 +00:00
|
|
|
$file = \WikiaFileHelper::getFileFromTitle( $title );
|
2016-11-21 14:13:13 +00:00
|
|
|
|
2016-12-29 10:28:26 +00:00
|
|
|
if (
|
|
|
|
!$file || !$file->exists() ||
|
|
|
|
!in_array( $file->getMediaType(), [ MEDIATYPE_BITMAP, MEDIATYPE_DRAWING, MEDIATYPE_VIDEO ] )
|
|
|
|
) {
|
2016-03-17 09:37:18 +00:00
|
|
|
return false;
|
|
|
|
}
|
2016-11-21 14:13:13 +00:00
|
|
|
|
2016-03-17 09:37:18 +00:00
|
|
|
// get dimensions
|
2016-03-17 11:12:36 +00:00
|
|
|
$originalWidth = $file->getWidth();
|
2017-01-13 12:57:17 +00:00
|
|
|
// we need to have different thumbnail file dimensions to support (not to have pixelated images) wider infoboxes than default width
|
2017-01-13 14:27:48 +00:00
|
|
|
$fileDimensions = $this->getThumbnailSizes( $thumbnailFileWidth, self::MAX_DESKTOP_THUMBNAIL_HEIGHT,
|
2017-01-13 12:57:17 +00:00
|
|
|
$originalWidth, $file->getHeight() );
|
2017-01-13 14:27:48 +00:00
|
|
|
$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
|
2017-01-13 12:57:17 +00:00
|
|
|
$ratio =
|
|
|
|
!empty( $wgPortableInfoboxCustomImageWidth ) &&
|
|
|
|
$originalWidth > $wgPortableInfoboxCustomImageWidth
|
2017-01-13 13:19:30 +00:00
|
|
|
? $wgPortableInfoboxCustomImageWidth / $fileDimensions['width'] : 1;
|
2016-03-17 09:37:18 +00:00
|
|
|
// get thumbnail
|
|
|
|
$thumbnail = $file->transform( [
|
2017-01-13 12:57:17 +00:00
|
|
|
'width' => round( $fileDimensions['width'] * $ratio ),
|
|
|
|
'height' => round( $fileDimensions['height'] * $ratio ),
|
2016-03-17 09:37:18 +00:00
|
|
|
] );
|
2016-11-03 13:27:43 +00:00
|
|
|
$thumbnail2x = $file->transform( [
|
2017-01-13 12:57:17 +00:00
|
|
|
'width' => round( $fileDimensions['width'] * $ratio * 2 ),
|
|
|
|
'height' => round( $fileDimensions['height'] * $ratio * 2 ),
|
2016-11-03 13:27:43 +00:00
|
|
|
] );
|
|
|
|
if ( !$thumbnail || $thumbnail->isError() || !$thumbnail2x || $thumbnail2x->isError() ) {
|
2015-07-28 11:49:17 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-03-17 09:37:18 +00:00
|
|
|
return array_merge( $data, [
|
2018-08-04 05:38:17 +00:00
|
|
|
'ref' => ++self::$count,
|
2018-03-20 15:53:11 +00:00
|
|
|
'height' => intval( $imgTagDimensions['height'] ),
|
|
|
|
'width' => intval( $imgTagDimensions['width'] ),
|
2018-03-15 14:02:46 +00:00
|
|
|
'originalHeight' => $dataAttrs['height'] ?? '',
|
|
|
|
'originalWidth' => $dataAttrs['width'] ?? '',
|
2016-03-17 09:37:18 +00:00
|
|
|
'thumbnail' => $thumbnail->getUrl(),
|
2016-11-03 13:27:43 +00:00
|
|
|
'thumbnail2x' => $thumbnail2x->getUrl(),
|
2018-08-03 12:24:42 +00:00
|
|
|
'fileName' => $dataAttrs['fileName'] ?? ''
|
2016-03-17 09:37:18 +00:00
|
|
|
] );
|
2015-07-28 11:49:17 +00:00
|
|
|
}
|
|
|
|
|
2016-04-05 15:42:21 +00:00
|
|
|
/**
|
|
|
|
* @param array $images
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function extendImageCollectionData( $images ) {
|
2018-03-02 15:47:26 +00:00
|
|
|
$images = array_map(
|
|
|
|
function ( $image, $index ) {
|
|
|
|
$image['dataRef'] = $index;
|
|
|
|
|
|
|
|
return $image;
|
|
|
|
},
|
|
|
|
$images,
|
|
|
|
array_keys($images)
|
|
|
|
);
|
|
|
|
|
2016-04-05 15:42:21 +00:00
|
|
|
$images[0]['isFirst'] = true;
|
2016-12-29 13:17:48 +00:00
|
|
|
return [
|
2018-08-03 12:24:42 +00:00
|
|
|
'images' => $images
|
2016-04-05 15:42:21 +00:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2015-12-11 00:41:07 +00:00
|
|
|
/**
|
2016-03-17 09:37:18 +00:00
|
|
|
* Calculates image dimensions based on preferred width and max acceptable height
|
2015-12-11 10:56:41 +00:00
|
|
|
*
|
2016-03-17 09:37:18 +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
|
|
|
*/
|
2016-03-17 09:37:18 +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 {
|
2016-03-17 09:37:18 +00:00
|
|
|
$width = min( $preferredWidth, $originalWidth );
|
|
|
|
$height = min( $maxHeight, $width * $originalHeight / $originalWidth );
|
2015-12-11 10:56:41 +00:00
|
|
|
}
|
|
|
|
|
2016-03-17 09:37:18 +00:00
|
|
|
return [ 'height' => round( $height ), 'width' => round( $width ) ];
|
2015-12-11 10:56:41 +00:00
|
|
|
}
|
2017-01-13 08:25:46 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* return real width of the image.
|
|
|
|
* @param \Title $title
|
|
|
|
* @return int number
|
|
|
|
*/
|
|
|
|
public function getFileWidth( $title ) {
|
|
|
|
$file = \WikiaFileHelper::getFileFromTitle( $title );
|
|
|
|
|
|
|
|
if ( $file ) {
|
|
|
|
return $file->getWidth();
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2015-09-09 08:01:24 +00:00
|
|
|
}
|