How can the code be modified to loop through all images in a specific folder and create thumbnails for each of them?
To loop through all images in a specific folder and create thumbnails for each of them, you can use the PHP `glob()` function to get an array of all image files in the folder. Then, you can iterate over this array and use a function like `imagecopyresampled()` to create thumbnails for each image.
<?php
$folder = 'path/to/folder/';
$files = glob($folder . '*.{jpg,jpeg,png,gif}', GLOB_BRACE);
foreach ($files as $file) {
$image = imagecreatefromstring(file_get_contents($file));
$thumb = imagecreatetruecolor(100, 100);
imagecopyresampled($thumb, $image, 0, 0, 0, 0, 100, 100, imagesx($image), imagesy($image));
imagejpeg($thumb, $folder . 'thumb_' . basename($file), 80);
imagedestroy($image);
imagedestroy($thumb);
}
?>
Keywords
Related Questions
- How can PHP developers optimize their code to efficiently work with date-related data, such as formatting dates for display or performing calculations based on dates?
- When working with a single record in a database table, what are some alternative methods to Fetch Array that could be used in PHP?
- In what situations would it be preferable to manually assign variables to individual entries in a PHP script, rather than using pagination techniques?