Skip to content

Instantly share code, notes, and snippets.

@sbrl
Last active September 17, 2017 09:35
Show Gist options
  • Select an option

  • Save sbrl/4cf2b4dcc9b0f18b3d78e0cd2506f3ec to your computer and use it in GitHub Desktop.

Select an option

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
<?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