How can one ensure that all images displayed from a folder using PHP are resized proportionally to maintain consistency in size?
When displaying images from a folder using PHP, it is important to resize them proportionally to maintain consistency in size. This can be achieved by calculating the aspect ratio of each image and then resizing them accordingly using PHP's GD library functions.
<?php
$folder_path = "path/to/images/folder/";
$images = glob($folder_path . "*.{jpg,jpeg,png,gif}", GLOB_BRACE);
foreach($images as $image) {
list($width, $height) = getimagesize($image);
$aspect_ratio = $width / $height;
$new_width = 200; // set desired width
$new_height = $new_width / $aspect_ratio;
$image_resized = imagecreatetruecolor($new_width, $new_height);
$image_source = imagecreatefromstring(file_get_contents($image));
imagecopyresampled($image_resized, $image_source, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
header('Content-Type: image/jpeg');
imagejpeg($image_resized, null, 100);
}
?>