How to crop image 16:9 ratio in PHP ?

how to crop image in 16:9 ratio in php for view image proper on your website or app ?
Crop image in php.
Below program crop you image in 16:9 ratio for show image proper.

function crop_image($image) {

//$x_o ? $y_o – Output image top left angle coordinates on input image
//$w_o ? h_o – Width and height of output image

list($w_i, $h_i, $type) = getimagesize($image); // Return the size and image type (number)

//calculating 16:9 ratio
$w_o = $w_i;
$h_o = 9 * $w_o / 16;

//if output height is longer then width
if ($h_i < $h_o) {
$h_o = $h_i;
$w_o = 16 * $h_o / 9;

$x_o = $w_i – $w_o;
$y_o = $h_i – $h_o;

$types = array(“jpg”, “gif”, “jpeg”, “png”); // Array with image types
$ext = $types[$type]; // If you know image type, “code” of image type, get type name
if ($ext) {
$func = ‘imagecreatefrom’.$ext; // Get the function name for the type, in the way to create image
$img_i = $func($image); // Creating the descriptor for input image
} else {
echo ‘Incorrect image’; // Showing an error, if the image type is unsupported
return false;
if ($x_o + $w_o > $w_i) $w_o = $w_i – $x_o; // If width of output image is bigger then input image (considering x_o), reduce it
if ($y_o + $h_o > $h_i) $h_o = $h_i – $y_o; // If height of output image is bigger then input image (considering y_o), reduce it
$img_o = imagecreatetruecolor($w_o, $h_o); // Creating descriptor for input image
imagecopy($img_o, $img_i, 0, 0, $x_o/2, $y_o/2, $w_o, $h_o); // Move part of image from input to output
$func = ‘image’.$ext; // Function that allows to save the result
return $func($img_o, $image); // Overwrite input image with output on server, return action’s result

$image = ‘C:\xampp\htdocs\crop\images\1.jpg’;
echo crop_image($image);

Leave a Reply

Your email address will not be published. Required fields are marked *