Last active
December 19, 2015 23:08
-
-
Save ChrisLTD/6032589 to your computer and use it in GitHub Desktop.
Functions for calculating new proportional sizes for images
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* Get a new larger proportional height and width for an image | |
* Adapted from http://stackoverflow.com/a/4820929/648844 | |
* | |
* Parameters: original width, original height, minimum width, minimum height | |
* Return: null if image exceeds minimums or array with rounded width and height | |
*/ | |
function image_resize_up($orig_w, $orig_h, $MIN_W, $MIN_H){ | |
$ratio = $orig_w * 1.0 / $orig_h; | |
$w_undersized = ($orig_w < $MIN_W); | |
$h_undersized = ($orig_h < $MIN_H); | |
if ($w_undersized OR $h_undersized) | |
{ | |
$new_w = round( max($MIN_W, $ratio * $MIN_H) ); | |
$new_h = round( max($MIN_H, $MIN_W / $ratio) ); | |
return array('width' => $new_w, 'height' => $new_h); | |
} | |
return null; | |
} | |
/* | |
* Get a new smaller proportional height and width for an image | |
* Parameters: original width, original height, maximum width, maximum height | |
* Return: null if image is under maximum or array with rounded width and height | |
*/ | |
function image_resize_down($orig_w, $orig_h, $MAX_W, $MAX_H){ | |
$ratio = $orig_w * 1.0 / $orig_h; | |
$w_undersized = ($orig_w > $MAX_W); | |
$h_undersized = ($orig_h > $MAX_H); | |
if ($w_undersized OR $h_undersized) | |
{ | |
$new_w = round( min($MAX_W, $ratio * $MAX_H) ); | |
$new_h = round( min($MAX_H, $MAX_W / $ratio) ); | |
return array('width' => $new_w, 'height' => $new_h); | |
} | |
return null; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment