Created
November 24, 2014 15:47
-
-
Save james2doyle/13a36401d6249729d017 to your computer and use it in GitHub Desktop.
Crop to fit image in Phalcon PHP using the GD adapter
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 | |
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