mirror of
https://github.com/Universal-Omega/PortableInfobox.git
synced 2024-11-27 17:50:38 +00:00
Prevent DivisionByZeroError in getThumbnailSizes (#130)
This commit is contained in:
parent
0e551d714d
commit
5736d9bfe1
|
@ -101,6 +101,33 @@ class PortableInfoboxImagesHelper {
|
|||
* @return array [ 'width' => int, 'height' => int ]
|
||||
*/
|
||||
public function getThumbnailSizes( $preferredWidth, $maxHeight, $originalWidth, $originalHeight ) {
|
||||
// Prevent division by zero by ensuring width and height are valid
|
||||
if ( $originalWidth == 0 && $originalHeight == 0 ) {
|
||||
// Return default sizes if both original dimensions are zero
|
||||
return [ 'height' => 0, 'width' => 0 ];
|
||||
}
|
||||
|
||||
// Handle case where only the original width is zero
|
||||
if ( $originalWidth == 0 ) {
|
||||
$height = min( $maxHeight, $originalHeight );
|
||||
// No width can be calculated
|
||||
return [ 'height' => round( $height ), 'width' => 0 ];
|
||||
}
|
||||
|
||||
// Handle case where only the original height is zero
|
||||
if ( $originalHeight == 0 ) {
|
||||
$width = min( $preferredWidth, $originalWidth );
|
||||
// No height can be calculated
|
||||
return [ 'height' => 0, 'width' => round( $width ) ];
|
||||
}
|
||||
|
||||
// Prevent issues with invalid maxHeight or preferredWidth
|
||||
if ( $preferredWidth == 0 || $maxHeight == 0 ) {
|
||||
// If either maxHeight or preferredWidth is zero, return the original dimensions
|
||||
return [ 'height' => $originalHeight, 'width' => $originalWidth ];
|
||||
}
|
||||
|
||||
// Standard aspect ratio handling if all dimensions are valid
|
||||
if ( ( $originalHeight / $originalWidth ) > ( $maxHeight / $preferredWidth ) ) {
|
||||
$height = min( $maxHeight, $originalHeight );
|
||||
$width = min( $preferredWidth, $height * $originalWidth / $originalHeight );
|
||||
|
|
Loading…
Reference in a new issue