Page 1 of 1
recursive function to go down in a dir
Posted: Thu Dec 29, 2005 5:44 pm
by fansdefoot
Hey ,
I wanna make a script that resize all pictures of a directory because they are quite big for the net.
I got the function for just one picture and now I need to make the function that take all the pictures ' paths of the current directory and all directory down.
This is my function.
Code: Select all
function direc($dir)
{
$dossier=opendir($dir);
$nb = 0;
while($image=readdir($dossier)){
$l=array('.', '..');
if(!in_array($image,$l)){
if(is_dir($dir."/".$image)){
$o[$nb] = direc($dir."/".$image); // recursivity for directory down
}
else{
$nb++;
$o[$nb] = $dir."/".$image;
}
}
}
return $o;
}
I wanna at the end an array $o with the path of all pictures of the current directory and all directory down .
For now , I have only the paths of the current directory . What's wrong with the recursivity ?
Thanks in advance for your help.
Posted: Thu Dec 29, 2005 6:09 pm
by josh
here's something I threw together about a week ago to resize a bunch of high resolution photos, not the best script but it got the job done,
as you see you have to write a function that scans the directory, and have that function call itself (beware of infinite recursion)
Code: Select all
<?php
ob_implicit_flush();
set_time_limit(0);
error_reporting(E_ALL);
$location = glob("/path/to/images/high_res/*/");
?>
<html>
<head>
<title>
Generating medium rez's
</title>
<script language="javascript" type="text/javascript">
function writit(id, text) {
if (document.getElementById)
{
x = document.getElementById(id);
x.innerHTML = '';
x.innerHTML = text;
}
else if (document.all)
{
x = document.all[id];
x.innerHTML = text;
}
else if (document.layers)
{
x = document.layers[id];
text2 = '<P CLASS="testclass">' + text + '</P>;';
x.document.open();
x.document.write(text2);
x.document.close();
}
}
</script>
</head>
<body>
<div id="message">
Starting...
</div>
<?php
foreach ($location as $loc) {
$loc = str_replace("/path/to/images/high_res/", NULL, $loc);
echo $loc."<br />";
}
echo ("<br />");
foreach ($location as $loc) {
$loc = str_replace("/path/to/images/high_res/", NULL, $loc);
scanlocation($loc);
}
function writit($msg) {
echo ("<script language=\"javascript\" type=\"text/javascript\">writit('message', '".$msg."')</script>");
}
function scanlocation($location) {
writit('Scanning');
if ($location==NULL) exit();
$imagedir='/var/www/images/high_res/'.$location;
$thumbnaildir='/var/www/images/med_res/'.$location;
if (!file_exists($thumbnaildir)) {
mkdir($thumbnaildir);
chmod($thumbnaildir, 0777);
}
$x=1;
$images = glob($imagedir . "{*.jpg,*.JPG}", GLOB_BRACE);
foreach ($images as $k => $file) {
$images[$k]='/'.str_replace($imagedir, NULL, $file);
}
$thumbs = glob($thumbnaildir . "{*.jpg,*.JPG}", GLOB_BRACE);
foreach ($thumbs as $k => $file) {
$thumbs[$k]='/'.str_replace($thumbnaildir, NULL, $file);
}
$images = array_diff($images, $thumbs);
$total = count($images);
writit($total.' images found, starting');
foreach($images as $file) {
$msg = "<b>$x of $total for $location [";
$percent = round(($x/$total)*100, 1);
$msg .= $percent;
$msg .= "%]";
writit($msg);
if (!$data=@file_get_contents($imagedir.$file)) {
echo ('failed on '.$file.'<br />');
continue;
}
list($width_orig, $height_orig, $type, $attr)=getimagesize($imagedir.$file);
$width=768;
$height=512;
if ($width && ($width_orig < $height_orig)) {
$width = ($height / $height_orig) * $width_orig;
} else {
$height = ($width / $width_orig) * $height_orig;
}
// Resample
$filename = $thumbnaildir.$file;
if (!file_exists($filename)) {
$image_p = imagecreatetruecolor($width, $height);
$image = imagecreatefromstring($data);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
$image = imagecreatefrompng('/var/www/html/dev/_bin/watermark.png');
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, 768, 510);
imagejpeg($image_p, $filename, 100);
}
$x++;
}
echo ("<font color='green'>$location is done!!!</font><br />");
}
?>
</body>
</html>
Posted: Thu Dec 29, 2005 6:22 pm
by LazyJones
when '$o' is an array, you can insert elements in it by o[] = $image (you don't need the $nb)
Your function returns an array. So if you do this: o[] = direc(...) you insert an array into an array, not add the returning values to the array. (best that I can do with explaining in my english...)
So what you need to do is get the return value into another array (let's say $tmp) and merge it into $o.
Posted: Fri Dec 30, 2005 4:17 am
by fansdefoot
How can I merge into $o[] please?
Otherwise I have a solution
Code: Select all
function liste_recursive($dossier_a_parcourir,$liste,$nivo)
{
$dir = opendir($dossier_a_parcourir) ;
$nivo++;
while( $file = readdir($dir) )
{
if((is_dir($dossier_a_parcourir.'/'.$file)) && $file != '.' && $file != '..')
{
$path = $dossier_a_parcourir.'/'.$file;
// $liste['listechemin'][] = $path;
// $liste['listedossier'][] = $file;
// $liste['listenivo'][] = $nivo;
$liste=liste_recursive($path,$liste,$nivo);
}else{
if($file != '.' && $file != '..')
{
$path = $dossier_a_parcourir.'/'.$file;
$liste['listechemin'][] = $path;
$liste['listedossier'][] = $file;
$liste['listenivo'][] = $nivo;
}
}
}
return $liste;
}
and you call it with
Code: Select all
$racine = 'NY';
$liste = array("listechemin" => array(), "listedossier" => array(), "listenivo" => array());
$liste = liste_recursive($racine,$liste,0);
/* Ne pas tenir compte de cette partie, elle n'est utile qu'au developement */
echo '<br />';
$nbelem = (count($liste['listedossier']));
echo 'nombre de valeurs dans cet array = '.$nbelem.'<br /><br />';
for($i = 0;$i<$nbelem;$i++)
{
echo '$listenivo['.$i.'] = '.$liste['listenivo'][$i].'<br />';
echo '$listechemin['.$i.'] = '.$liste['listechemin'][$i].'<br />';
echo '$listedossier['.$i.'] = '.$liste['listedossier'][$i].'<hr />';
}