| 1 |
<?php |
| 2 |
|
| 3 |
namespace Averta\Core\Utility; |
| 4 |
|
| 5 |
class Media{ |
| 6 |
|
| 7 |
/** |
| 8 |
* Calculates new width and height to fit the media in a box size |
| 9 |
* |
| 10 |
* @param string $type Resize type. "cover" or "contain" |
| 11 |
* @param int $boxWidth Width of the box to fit the media in |
| 12 |
* @param int $boxHeight Height of the box to fit the media in |
| 13 |
* @param int $width Original width of the media |
| 14 |
* @param int $height Original height of the media |
| 15 |
* |
| 16 |
* @return float[]|int[] |
| 17 |
*/ |
| 18 |
public static function fitInBox( $type, $boxWidth, $boxHeight, $width, $height ) { |
| 19 |
$widthRatio = $width ? $boxWidth / $width : 1; |
| 20 |
$heightRatio = $height ? $boxHeight / $height : 1; |
| 21 |
|
| 22 |
$ratio = $type === 'cover' ? max( $widthRatio, $heightRatio ) : min( $widthRatio, $heightRatio ); |
| 23 |
|
| 24 |
return [ |
| 25 |
$width * $ratio, |
| 26 |
$height * $ratio, |
| 27 |
]; |
| 28 |
} |
| 29 |
} |
| 30 |
|