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!
- Compressed? Or smaller in width and height? - E_p
- cubiq.org/create-fixed-size-thumbnails-with-imagemagick / dototot.com/β¦ / imagemagick.org/script/index.php - E_p
- reduced in size - ZeroBone
- preferably without installing additional libraries - ZeroBone
- And what is there? Imagemagic, gd? - E_p
|
1 answer
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); } |