Last active
September 17, 2017 09:35
-
-
Save sbrl/4cf2b4dcc9b0f18b3d78e0cd2506f3ec to your computer and use it in GitHub Desktop.
Resize a GD image to fit inside a square of the given size. #php
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 | |
| /******************************************************************************* | |
| ******************************* resizeImage.php ******************************* | |
| ******************************************************************************* | |
| * Resizes a GD image to fit inside a square of the given size. | |
| ******************************************************************************/ | |
| function resize_image($image, $size) | |
| { | |
| $cur_width = imagesx($image); | |
| $cur_height = imagesy($image); | |
| if($cur_width < $size and $cur_height < $size) | |
| return $image; | |
| $width_ratio = $size / $cur_width; | |
| $height_ratio = $size / $cur_height; | |
| $ratio = min($width_ratio, $height_ratio); | |
| $new_height = floor($cur_height * $ratio); | |
| $new_width = floor($cur_width * $ratio); | |
| header("x-resize-to: $new_width x $new_height\n"); | |
| return imagescale($image, $new_width, $new_height); | |
| } | |
| ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment