php – Crop or fit images to a 16:9 aspect ratio

Question

I have this code that calls the first image of an article

<img src="<?php echo get_first_image(); ?>" alt="<?php the_title(); ?>" title="<?php the_title(); ?>">

but since the images are not the same size, it causes the images to not display with the same height.
Here is a picture of how it currently looks
https://ibb.co/xHxs2yG

Here is an image of the result I need https://ibb.co/S0Bt1Dy (with aspect ratio 16:9)

This is the function to calls the first image

function get_first_image() {
global $post, $posts;
$first_image="";
ob_start();
ob_end_clean();
$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
$first_image = $matches [1] [0];
if(empty($first_image)){ //Defines a default image
// Set the default image if there are no image available inside post content
$first_image = "/img/default.jpg";
}
return $first_image;
}

0
unopermanente 1 month 2022-12-22T10:24:31-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse