dear everyone i'm having some problems with a script i've been putting together. it's purpose is to loop through a folder get all the images in it, add references of them to a database and resize them using the GD library. for the most part it is fine, however since i've tried to start resizing using the gd library i'm encountering a problem resulting in the following error message:
Fatal error: Allowed memory size of 8388608 bytes exhausted (tried to allocate 3840 bytes) in /home/virtual/site200/fst/var/www/html/void test/index2.php on line 69
i've looked into this message and stuck this at the top of my config.inc.php include to see if it fixes it:
$test = ini_set ("memory_limit", "200M");
print ("hello" . "$test");
but nothing, the reason i assigned it to $test is so i can see what it returns, which is nothing. making me think it dosen't work.
i think / hope it's something in the code which is just wrong, or "un clean" and causing a memory leak or something. the images i'm working with are in the 5-600kb range, but am going to need the script to handle batch's of hundreds of images at a time.
i don't have access to the php.ini file either as i'm on shared hosting.
here is the body of code as it is at the moment, the script is processing about 3/4 images then stopping with the above fatal error message.
<?php
//this bit gets all of the images in the upload folder and stuffs it in an array
$dir="/home/virtual/dspmedia.co.uk/var/ftp/uploads/void/incoming";
$images_dir="/home/virtual/dspmedia.co.uk/var/www/html/void test/incoming";
if (is_dir($dir)) {
$fd = @opendir($dir);
if($fd) {
while (($part = @readdir($fd)) == true) {
if ($part != "." && $part != "..") {
$file_array[]='/home/virtual/dspmedia.co.uk/var/ftp/uploads/void/incoming/'.$part;
}
}
}
sort($file_array);
reset($file_array);
//add images into database
for ($i=0; $i<count($file_array); $i++) {
mysql_query("
INSERT INTO images (
img_LOCATION,
img_THUMB
) VALUES (
'0',
'1'
)
");
$new_id = mysql_insert_id();
$filename = "$new_id".".jpg";
$thumbname = "tb_"."$new_id".".jpg";
mysql_query("
UPDATE images SET
img_LOCATION = '$filename',
img_THUMB = '$thumbname'
WHERE img_ID = '$new_id'
");
//now lets work out if it's a wide or tall image
$size = GetImageSize($file_array[$i]);
// Wide Image
if($size[0] > $size[1])
{
$thumbnail_width = 150;
$thumbnail_height = (int)(150 * $size[1] / $size[0]);
}
// Tall Image
else
{
$thumbnail_width = (int)(150 * $size[0] / $size[1]);
$thumbnail_height = 150;
}
//now onto the meat, resizing the bitch
$function_to_read = 'ImageCreateFromJPEG';
$function_to_write = 'ImageJPEG';
// Read the source file
$source_handle = $function_to_read($file_array[$i]);
if ($source_handle) {
// Let's create a blank image for the thumbnail
$destination_handle =
ImageCreateTrueColor($thumbnail_width, $thumbnail_height);
// Now we resize it
ImageCopyResampled($destination_handle, $source_handle,
0, 0, 0, 0, $thumbnail_width, $thumbnail_height, $size[0], $size[1]);
}
// Let's save the thumbnail
$function_to_write($destination_handle, $images_dir . '/tb_' . $filename);
//this moves them from the uploads folder to the db images folder
copy($file_array[$i], $images_dir . '/' . $filename);
}
//this bit pumps out all the urls to the moved images
if (is_dir($images_dir)) {
$fd = @opendir($images_dir);
if($fd) {
while (($part = @readdir($fd)) == true) {
if ($part != "." && $part != "..") {
$nfile_array[]='incoming/'.$part;
}
}
}
sort($nfile_array);
reset($nfile_array);
for($i=0;$i<count($nfile_array);$i++) {
$npart=$nfile_array[$i];
if (!strstr($npart,".inc.php") && !strstr($npart,"index.php")) {
$fsize=ceil(filesize($npart)/1000);
if (is_dir($npart)) {
print("<tr><TD><a href=\"$npart\">$npart</a></TD><TD>Directory</TD></TR>\n");
} else {
print("<tr><TD><a href=\"$npart\">$npart</a></TD><TD>$fsize KB</TD></TR>\n");
}
}
}
}
}
exit;
?>
anything you guys might think will help / be the answer just anything. i'm really pulling my hair out over this one!