Jump to content

Archived

This topic is now archived and is closed to further replies.

darga333

please help!! not looping through all the files in the folder

Recommended Posts

Every time I run this script it creates the same thumbails over again, does anyone know how to start with the latest thumbnail that hasnt been created yet (because it times out) and loop through every file in the folder. It is stopping after only the 40th or so each time.

I have changed max execution time to accept 1 hour, and the memory_limit to accept 300MB. It creates about 40 images after a minute.. and then it continues for one more minute and then stops. But in that last minute, for some reason it doesnt create any more thumbnails.

I pasted the whole code below. I am new to this stuff. Please help!!!

<?php
//Creating thumbnails

ini_set(max_execution_time,3600);
ini_set("memory_limit","300M");

$imagefolder='uploads/';
$thumbsfolder='uploads_thumbs/';
$pics=directory($imagefolder,"jpg,JPG,JPEG,jpeg,png,PNG");
$pics=ditchtn($pics,"tn_");
if ($pics[0]!="")
{



foreach ($pics as $p)
{
createthumb($imagefolder.$p,$thumbsfolder."tn_".$p,100,100);

}


}

/*
Function ditchtn($arr,$thumbname)
filters out thumbnails
*/
function ditchtn($arr,$thumbname)
{
foreach ($arr as $item)
{
if (!preg_match("/^".$thumbname."/",$item)){$tmparr[]=$item;}
}
return $tmparr;
}

/*
Function createthumb($name,$filename,$new_w,$new_h)
creates a resized image
variables:
$name Original filename
$filename Filename of the resized image
$new_w width of resized image
$new_h height of resized image
*/
function createthumb($name,$filename,$new_w,$new_h)
{
$system=explode(".",$name);
if (preg_match("/jpg|JPG/",$system[1])){$src_img=imagecreatefromjpeg($name);}
if (preg_match("/png|PNG/",$system[1])){$src_img=imagecreatefrompng($name);}
$old_x=imageSX($src_img);
$old_y=imageSY($src_img);
if ($old_x > $old_y)
{
$thumb_w=$new_w;
$thumb_h=$old_y*($new_h/$old_x);
}
if ($old_x < $old_y)
{
$thumb_w=$old_x*($new_w/$old_y);
$thumb_h=$new_h;
}
if ($old_x == $old_y)
{
$thumb_w=$new_w;
$thumb_h=$new_h;
}
$dst_img=ImageCreateTrueColor($thumb_w,$thumb_h);
imagecopyresampled($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y);
if (preg_match("/png/",$system[1]))
{
imagepng($dst_img,$filename);
} else {
imagejpeg($dst_img,$filename);
}
imagedestroy($dst_img);
imagedestroy($src_img);
}

/*
Function directory($directory,$filters)
reads the content of $directory, takes the files that apply to $filter
and returns an array of the filenames.
You can specify which files to read, for example
$files = directory(".","jpg,gif");
gets all jpg and gif files in this directory.
$files = directory(".","all");
gets all files.
*/
function directory($dir,$filters)
{
$handle=opendir($dir);
$files=array();
if ($filters == "all"){while(($file = readdir($handle))!==false){$files[] = $file;}}
if ($filters != "all")
{
$filters=explode(",",$filters);
while (($file = readdir($handle))!==false)
{
for ($f=0;$f<sizeof($filters);$f++):
$system=explode(".",$file);
if ($system[1] == $filters[$f]){$files[] = $file;}
endfor;
}
}
closedir($handle);
return $files;
}
?>

Share this post


Link to post
Share on other sites
Appologies, but I didn't actually read through all of your code - It looks like a lot of code for what you are trying to do, but anyway... Here is a possible solution for your problem with re-creating the same thumbnails each time the script is run, this will only work if both your original image and the thumbnail have the same name, but are stored in different directories, i.e. /images/ and /images/thumbs:

[code]<?php
//Set som variables up
$orig = array();
$thumbs = array();

//Loop through images directory and put the file names only in an array
foreach(glob("images/*.jpg") as $o) {
    $orig[] .= substr($o,7);
}

//Loop through images/thumbs directory and put the file names only in an array
foreach(glob("images/thumbs/*.jpg") as $t) {
    $thumbs[] .= substr($t,14);
}

//Compare the two arrays, and return the filenames that appear in the original directory but not the thumbs directory
$new = array_diff($orig,$thumbs);

//We end up with the array $new containing all of the filenames for images that need thumbnails creating.
?>[/code]

The above script is untested, it was made up as I went along, may need some tweaking if it doesn't work straight away... As for the script timing out, have you tried using set_time_limit(0) ?

Share this post


Link to post
Share on other sites
hey i read through your reply, and your code, impressive, i wonder how it is even possible to come up with something like that.

The code i pasted before also included a bunch of functions. Sorry about that, I didnt think that you needed them but I pasted everything just in case. The only difference in the filenames is there is a tn_ before each file name when it is written to the uploads_thumbs/ folder. I was thinking that i can just add tn_ to your code images/tn_*.jpg, to get the identical file name?

Now that we have an array of files that do not have thumbnails yet, how do we actually get the code below to integrate with your array $new? Some how the code that i have has to look through your $new array, then, it needs to actually get that corresponding file itself from the folder to create the thumbnail. I have no idea how to do that. I posted only the relevant code needed this time. Thank you so much for helping me with this!

also how do you make your reply look so nice and formatted with the different color around your code??

<?php

ini_set(max_execution_time,3600);
ini_set("memory_limit","300M");

$imagefolder='uploads/';
$thumbsfolder='uploads_thumbs/';
$pics=directory($imagefolder,"jpg,JPG,JPEG,jpeg,png,PNG");
$pics=ditchtn($pics,"tn_");
if ($pics[0]!="")
{


//Start the script again but start on the next 20 pictures

foreach ($pics as $p)
{
createthumb($imagefolder.$p,$thumbsfolder."tn_".$p,100,100);
echo "$p";
}


}

?>









[!--quoteo(post=362577:date=Apr 7 2006, 11:35 AM:name=SemiApocalyptic)--][div class=\'quotetop\']QUOTE(SemiApocalyptic @ Apr 7 2006, 11:35 AM) [snapback]362577[/snapback][/div][div class=\'quotemain\'][!--quotec--]
Appologies, but I didn't actually read through all of your code - It looks like a lot of code for what you are trying to do, but anyway... Here is a possible solution for your problem with re-creating the same thumbnails each time the script is run, this will only work if both your original image and the thumbnail have the same name, but are stored in different directories, i.e. /images/ and /images/thumbs:

[code]<?php
//Set som variables up
$orig = array();
$thumbs = array();

//Loop through images directory and put the file names only in an array
foreach(glob("images/*.jpg") as $o) {
    $orig[] .= substr($o,7);
}

//Loop through images/thumbs directory and put the file names only in an array
foreach(glob("images/thumbs/*.jpg") as $t) {
    $thumbs[] .= substr($t,14);
}

//Compare the two arrays, and return the filenames that appear in the original directory but not the thumbs directory
$new = array_diff($orig,$thumbs);

//We end up with the array $new containing all of the filenames for images that need thumbnails creating.
?>[/code]

The above script is untested, it was made up as I went along, may need some tweaking if it doesn't work straight away... As for the script timing out, have you tried using set_time_limit(0) ?
[/quote]

Share this post


Link to post
Share on other sites
[!--quoteo(post=362623:date=Apr 7 2006, 08:10 PM:name=darga333)--][div class=\'quotetop\']QUOTE(darga333 @ Apr 7 2006, 08:10 PM) [snapback]362623[/snapback][/div][div class=\'quotemain\'][!--quotec--]The only difference in the filenames is there is a tn_ before each file name when it is written to the uploads_thumbs/ folder. I was thinking that i can just add tn_ to your code images/tn_*.jpg, to get the identical file name?[/quote]Not exactly, the best thing to do to get the filenames to match here is to alter the substr() to knock of the tn_ along with the preceding directory name, so use $thumbs[] .= substr($t,17); instead of $thumbs[] .= substr($t,14);

[!--quoteo(post=362623:date=Apr 7 2006, 08:10 PM:name=darga333)--][div class=\'quotetop\']QUOTE(darga333 @ Apr 7 2006, 08:10 PM) [snapback]362623[/snapback][/div][div class=\'quotemain\'][!--quotec--]also how do you make your reply look so nice and formatted with the different color around your code??[/quote]Use the [code ] and [/code ] tags.


As for integrating it with your current script, I guess you could modify this a bit:
[code] foreach ($new as $p) {
createthumb($imagefolder.$p,$thumbsfolder."tn_".$p,100,100);
echo "$p";
}[/code]
This will then loop through our newly created array containing only the required images, instead of the array of filenames built with your code here:
[code]$pics=directory($imagefolder,"jpg,JPG,JPEG,jpeg,png,PNG");
$pics=ditchtn($pics,"tn_");[/code]

Share this post


Link to post
Share on other sites
Hey thanks so much!!!! I managed to get the scipt to run correctly by changing it back to the original way you suggested - same file name for original file and for thumbnails. For some reason it wouldnt work with different file names. But I must say, I could not have even came close to doing this without you.

I also managed to get the script to create a thumbnail once you click upload. Which was a task in itself.

Well thanks again! Everything is perfect! Wow you are good! I notice you help alot of people on here! How long have you been coding in PHP? I cant wait to be able to code like that and to be able to come up with instant solutions! What would you recommend someone like me that is just getting involved, to become an expert in php and to be able to know everything, write my own scripts, and develop my own web aplications? I am really looking forward to it!


[!--quoteo(post=362649:date=Apr 7 2006, 03:28 PM:name=SemiApocalyptic)--][div class=\'quotetop\']QUOTE(SemiApocalyptic @ Apr 7 2006, 03:28 PM) [snapback]362649[/snapback][/div][div class=\'quotemain\'][!--quotec--]
Not exactly, the best thing to do to get the filenames to match here is to alter the substr() to knock of the tn_ along with the preceding directory name, so use $thumbs[] .= substr($t,17); instead of $thumbs[] .= substr($t,14);

Use the [code ] and [/code ] tags.
As for integrating it with your current script, I guess you could modify this a bit:
[code] foreach ($new as $p) {
createthumb($imagefolder.$p,$thumbsfolder."tn_".$p,100,100);
echo "$p";
}[/code]
This will then loop through our newly created array containing only the required images, instead of the array of filenames built with your code here:
[code]$pics=directory($imagefolder,"jpg,JPG,JPEG,jpeg,png,PNG");
$pics=ditchtn($pics,"tn_");[/code]
[/quote]

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.