Учитывая большой набор неупорядоченных изображений в формате jpg или png, я хотел создать сценарий PHP, который сначала отфильтровывает все содержимое папки для разрешенных форматов, копирует их в новую папку, переименованную в числовом порядке (1.jpg, 2.jpg, 3.jpg ..), создайте миниатюру 50x50 каждого изображения в дочерней папке «thumbs», а затем создайте HTML-файл с именем «gallery», который содержит дамп тегов «img» для каждого эскиза..
Он отлично работает до десятка или около того изображений, а затем превышает максимально выделяемый объем памяти.Это внезапно произошло и появляется, когда вызывается функция imagecopyresized.
Любой совет приветствуется.
Источник:
<?php
# Prepare vars
$dir = "O:/zip/";
$newDir = "C:/Users/user/Desktop/zip/";
$thumbs = $newDir."thumbs/";
$gallery = $newDir."gallery.html";
$types = array(".jpg", ".png");
$files = array();
$tempFiles = scandir($dir);
$i = 0;
# Copy and rename images
foreach($tempFiles as $file)
{
$thisType = substr($file,-4);
if(in_array($thisType, $types))
{
$dest = fopen($newDir.$i.$thisType, 'w');
fwrite($dest, file_get_contents($dir.$file));
fclose($dest);
list($width, $height) = getimagesize($newDir.$i.$thisType);
$im = imagecreatetruecolor(50, 50);
if($thisType == '.jpg')
{
imagecopyresized($im, imagecreatefromjpeg($newDir.$i.$thisType), 0, 0, 0, 0, 50, 50, $width, $height);
imagejpeg($im, $thumbs.$i.$thisType);
}
else
if($thisType == '.png')
{
imagecopyresized($im, imagecreatefrompng($newDir.$i.$thisType), 0, 0, 0, 0, 50, 50, $width, $height);
imagepng($im, $thumbs.$i.$thisType);
}
imagedestroy($im);
$html .= "<a href='$newDir$i$thisType'><img src='$thumbs$i$thisType' alt='$i$thisType' width='50' height='50'></a>";
print "Successfully processed $i$thisType<br>";
$i++;
}
}
print "Done.<br>";
# Create html gallery for new imgs
$dest = fopen($gallery, 'w');
fwrite($dest, $html);
fclose($dest);
print "There are ".number_format($i)." image files.\n\r";
?>