Hey,
i think you want to have resize while preserving aspect ratio of the thumbnail. If so, you can try with this helper function:
function makeThumbnail($data)
{
$image = new \Phalcon\Image\Adapter\GD($data['path'] . $data['name']);
$dimensions = explode('x', $data['size']);
$new_width = $dimensions[0];
$new_height = $dimensions[1];
$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_width = (int) ($new_height * $source_aspect_ratio);
$temp_height = $new_height;
} 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($data['path'] . $data['size'] . '_' . $data['name']);
}
Usage example:
$data = array(
'path' => 'PATH_TO_YOUR_IMAGE',
'name' => 'samnpleimages.jpg',
'size' => '300x180',
);
$this->makeThumbnail($data);
This will produce thumbnail in same directory as original image with the name:
300x180_samnpleimages.jpg