jrobinsonc
10/21/2016 - 3:24 PM

PHP: Set square size

PHP: Set square size

<?php

// With fixed size:
$square_size = 300;
$curr_width = $_GET['width'];
$curr_height = $_GET['height'];

if ($curr_width === $curr_height) {
    $width = $height = $square_size;
} else if (($height = ($curr_height / $curr_width) * $square_size) >= $square_size) {
    $width = $square_size;
} else if (($width = ($curr_width / $curr_height) * $square_size) >= $square_size) {
    $height = $square_size;
}

printf('<div style="overflow:hidden; width:%1$upx; height:%1$upx;"><img src="image.jpg" style="width:%2$upx; height:%3$upx;"></div>', $square_size, $width, $height);