Skip to content

Instantly share code, notes, and snippets.

@serebro
Forked from james2doyle/crop-and-fit.php
Last active October 29, 2019 15:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save serebro/59bcf5ec1c612c1a5c0f to your computer and use it in GitHub Desktop.
Save serebro/59bcf5ec1c612c1a5c0f to your computer and use it in GitHub Desktop.
<?php
function resizeImage($source, $dest, $new_width, $new_height, $quality)
{
// Taken from http://salman-w.blogspot.com/2009/04/crop-to-fit-image-using-aspphp.html
$image = new Phalcon\Image\Adapter\GD($source);
$source_height = $image->getHeight();
$source_width = $image->getWidth();
$source_aspect_ratio = $source_width / $source_height;
$desired_aspect_ratio = $new_width / $new_height;
if ($source_aspect_ratio > $desired_aspect_ratio) {
$temp_height = $new_height;
$temp_width = ( int ) ($new_height * $source_aspect_ratio);
} else {
$temp_width = $new_width;
$temp_height = ( int ) ($new_width / $source_aspect_ratio);
}
$x0 = ($temp_width - $new_width) / 2;
$y0 = ($temp_height - $new_height) / 2;
$image->resize($temp_width, $temp_height)->crop($new_width, $new_height, $x0, $y0);
$image->save($dest, $quality);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment