Good day! I have a folder on the server with exclusively png pictures. I need in another folder using PHP to fold their smaller versions so that the web page loads quickly. Not very many pictures, about 70 pieces. Please attach the code to the messages. I first meet with thumbnails. PS: I have the latest version of PHP and as far as I know, the GD library is built in there. Many thanks in advance for your help!

1 answer 1

Try this:

$sourceFolder = '/path/to/sources/'; // дирСктория с исходниками $destFolder = '/path/to/dest/' // дирСктория назначСния $newImageWidth = 100; // итоговая ΡˆΠΈΡ€ΠΈΠ½Π° изобраТСния $newImageHeight = 100; // итоговая высота изобраТСния foreach (glob($sourceFolder . '*.png', GLOB_BRACE) as $origImagePath) { $origImageInfo = pathinfo($origImagePath); list($origImageWidth, $origImageHeight) = getimagesize($origImagePath); $newImage = imagecreatetruecolor($newImageWidth, $newImageHeight); imagealphablending($newImage, false); imagesavealpha($newImage, true); imagecopyresampled($newImage, imagecreatefrompng($origImagePath), 0, 0, 0, 0, $newImageWidth, $newImageHeight, $origImageWidth, $origImageHeight); $newImagePath = $destFolder . $origImageInfo['basename']; if (file_exists($newImagePath)) { unlink($newImagePath); } imagepng($newImage, $newImagePath); }