How to process multiple images that are inside a folder?

See all posts Reply

How to process multiple images that are inside a folder? new!
by Marcio Alves, 13 years, 1 month ago
I made a script that sends a zipped file containing multiple photos. After the server receives this file, it unpacks the.
I'm not able to make the class read these files uncompressed (. Jpg).
Please help me.Reply
Re: How to process multiple images that are inside a folder? new!
by colin, 13 years, 1 month ago
You will need to provide more information. Can the class open a JPEG file on your setup? What is the log produced by the class? What is your code?Reply
Re: How to process multiple images that are inside a folder? new!
by Marcio Alves, 13 years, 1 month ago
Hi my friend

I am wanting to run a folder where the class contains several files sent via the form.

The uploaded file is a. Zip file containing multiple photos. Jpg inside.

I can do with that. Zip is sent, deleted and unpacked in the destination.

I'm just not able to put the unzipped files in the class.

See my code:

$diretorio = '/home/ceanorg/public_html/fotos/galeria/teste/';

//$files = array();

// Diretório atual
$d = dir('/home/ceanorg/public_html/fotos/galeria/teste/');

// Percorre todos os arquivos
while(false !== ($file = $d->read())){

  // Se o arquivo não é este arquivo, e não começa com "." ou "~", então guarde-o para exibição futura.
  if(($file{0} != '.') && ($file{0} != '~') ) {
    
    // Guarda o nome do arquivo e dados completos de uma chamada à stat()
    $files[$file] = stat($file);
  }
}

// Fecha o diretório
$d->close();   

//$files = array('/home/ceanorg/public_html/fotos/galeria/teste/006.jpg', '/home/ceanorg/public_html/fotos/galeria/teste/007.jpg', '/home/ceanorg/public_html/fotos/galeria/teste/008.jpg');
$files[$file] = array();

foreach ($files as $k => $l) {
  foreach ($l as $i => $v) {
    if (!array_key_exists($i, $files))
      $files[$i] = array();
    $files[$i][$k] = $v;
  }
}


foreach ($files as $file) {

  // we instanciate the class for each element of $file
  $handle = new Upload($file);

  // then we check if the file has been uploaded properly
  // in its *temporary* location in the server (often, it is /tmp)
  if ($handle->uploaded) {

    // now, we start the upload 'process'. That is, to copy the uploaded file
    // from its temporary location to the wanted location
    // It could be something like $handle->Process('/home/www/my_uploads/');
    $handle->image_resize            = true;
    $handle->image_ratio_y           = true;
    $handle->image_x                 = 100;
    $handle->Process($diretorio);
    
    // we check if everything went OK
    if ($handle->processed) {
      // everything was fine !
      echo '  file uploaded with success';
      echo '  ' . round(filesize($handle->file_dst_pathname)/256)/4 . 'KB';
      echo '  link to the file just uploaded: ' . $handle->file_dst_name . '';
    } else {
      // one error occured
      echo '  file not uploaded to the wanted location';
      echo '  Error: ' . $handle->error . '';
    }

  } else {
    // if we're here, the upload file failed for some reasons
    // i.e. the server didn't receive the file
    echo '  file not uploaded on the server';
    echo '  Error: ' . $handle->error . '';
  }
}
Reply
Re: How to process multiple images that are inside a folder? new!
by Marcio Alves, 13 years, 1 month ago
Error message that the class return:

file not uploaded on the server
Error: file_error

file not uploaded on the server
Error: file_error

file not uploaded on the server
Error:file_error

file not uploaded on the server
Error: file_errorReply
Re: How to process multiple images that are inside a folder? new!
by colin, 13 years, 1 month ago
Please provide the log produced by the classReply
Re: How to process multiple images that are inside a folder? new!
by Marcio Alves, 13 years, 1 month ago
Hi Colin. Thanks for help me.

First I am listing the files that are inside the folder with the following code:

$diretorio = '/home/ceanorg/public_html/fotos/galeria/teste/';

// abre o diretório
$ponteiro  = opendir($diretorio);

// monta os vetores com os itens encontrados na pasta
while ($nome_itens = readdir($ponteiro)) {
    $itens[] = $nome_itens;
}

// ordena o vetor de itens
sort($itens);

// percorre o vetor para fazer a separacao entre arquivos e pastas 
foreach ($itens as $k) {

  // retira "./" e "../" para que retorne apenas pastas e arquivos
  if ($k!="." && $k!=".."){
    
    // checa se o tipo de arquivo encontrado é uma pasta
    if (is_dir($k)) {

      // caso VERDADEIRO adiciona o item à variável de pastas
      $pastas[]=$k;
  
    } else {
      
      // caso FALSO adiciona o item à variável de arquivos
      $files[]=$k;
    }
  }
}

So far, so OK

Then I want to put the files listed in an array. Exactly where the error happens.

1) If I use this (manual), the class works perfectly:
$files = array('/home/ceanorg/public_html/fotos/galeria/teste/001.jpg', 
  '/home/ceanorg/public_html/fotos/galeria/teste/002.jpg', 
  '/home/ceanorg/public_html/fotos/galeria/teste/003.jpg');


2) If I use this other way, the class is not executed
$files = array();


3) The remainder of the code is this:

foreach ($files as $k => $l) {
  foreach ($l as $i => $v) {
    if (!array_key_exists($i, $files))
      $files[$i] = array();
    $files[$i][$k] = $v;
  }
}
    
    
foreach ($files as $file) {

  // we instanciate the class for each element of $file
  $handle = new Upload($file, 'pt_BR');
  

  // then we check if the file has been uploaded properly
  // in its *temporary* location in the server (often, it is /tmp)
  if ($handle->uploaded) {

    // now, we start the upload 'process'. That is, to copy the uploaded file
    // from its temporary location to the wanted location
    // It could be something like $handle->Process('/home/www/my_uploads/');
    
    $handle->image_resize            = true;
    $handle->image_ratio_y           = true;
    $handle->image_x                 = 100;
    $handle->Process('/home/ceanorg/public_html/fotos/galeria/teste/');
    
    // we check if everything went OK
    if ($handle->processed) {
        // everything was fine !
        echo '  file uploaded with success';
        echo '  ' . round(filesize($handle->file_dst_pathname)/256)/4 . 'KB';
        echo '  link to the file just uploaded: ../../fotos/galeria/teste/' . $handle->file_dst_name . ' : ' . $handle->file_dst_name . '';
    } else {
        // one error occured
        echo '  file not uploaded to the wanted location';
        echo '  Error: ' . $handle->error . '';
    }

  } else {
    // if we're here, the upload file failed for some reasons
    // i.e. the server didn't receive the file
    echo '  file not uploaded on the server';
    echo '  Error: ' . $handle->error . '';
  }
}
Reply
Re: How to process multiple images that are inside a folder? new!
by colin, 13 years, 1 month ago
If you deal with local files, you don't need the first foreach loop.Reply
Re: How to process multiple images that are inside a folder? new!
by Marcio Alves, 13 years, 1 month ago
Understand, but the error persists

For simplicity, where I enter the address that contains all the pictures I want to run the class?

Example 1)
$handle = new upload('/home/user/photos/', 'pt_BR');?

Example 2)
$files = array('/home/user/photos/'.$file);
foreach ($files as $k => $l) {
  foreach ($l as $i => $v) {
    if (!array_key_exists($i, $files))
      $files[$i] = array();
    $files[$i][$k] = $v;
  }
}

Other location?

Please, help me

ThanksReply
Re: How to process multiple images that are inside a folder? new!
by colin, 13 years, 1 month ago
Example 1 is OK.

In your example 2, if you deal with local files, do not use the foreach loop! The foreach loop is only for uploaded files.Reply
Re: How to process multiple images that are inside a folder? new!
by Marcio Alves, 13 years, 1 month ago
Thanks Colin

But, I used the example 1 and returned the following error:

file not uploaded to the wanted location
Error: copy_failed


This is my code in full:

$handle = new Upload('/home/ceanorg/public_html/fotos/galeria/teste/', 'pt_BR');

if ($handle->uploaded) {
  
  $handle->image_resize            = true;
  $handle->image_ratio_y           = true;
  $handle->image_x                 = 100;
  $handle->Process('/home/ceanorg/public_html/fotos/galeria/teste/');
  
  // we check if everything went OK
  if ($handle->processed) {
    // everything was fine !
    echo '  file uploaded with success';
    echo '  ' . round(filesize($handle->file_dst_pathname)/256)/4 . 'KB';
    echo '  link to the file just uploaded: ../../fotos/galeria/teste/' . $handle->file_dst_name . ' : ' . $handle->file_dst_name . '';
  } else {
    // one error occured
    echo '  file not uploaded to the wanted location';
    echo '  Error: ' . $handle->error . '';
  }

} else {
  // if we're here, the upload file failed for some reasons
  // i.e. the server didn't receive the file
  echo '  file not uploaded on the server';
  echo '  Error: ' . $handle->error . '';
}
Reply
Re: How to process multiple images that are inside a folder? new!
by colin, 13 years, 1 month ago
Please check the logs produced by the class!!!! Everything the class does is detailed in the log.

You probably have a permission issue.Reply
Re: How to process multiple images that are inside a folder? new!
by Marcio Alves, 13 years, 1 month ago
Permission to the folder /home/ceanorg/public_html/fotos/galeria/teste is CHMOD 777
:(Reply
Re: How to process multiple images that are inside a folder? new!
by colin, 13 years, 1 month ago
As I said before, check the logs produced by the class!!!!Reply
Re: How to process multiple images that are inside a folder? new!
by Marcio Alves, 13 years, 1 month ago
Dear Colin

The error log can be seen in http://www.cean.org.br/error.txtReply
Re: How to process multiple images that are inside a folder? new!
by colin, 13 years, 1 month ago
I mean the log produced by the class. See the first item of the FAQ.Reply
Re: How to process multiple images that are inside a folder? new!
by Marcio Alves, 13 years, 1 month ago
Sorry

The error produced by the class can be seen in http://cean.org.br/adm/image/upload.phpReply
Re: How to process multiple images that are inside a folder? new!
by colin, 13 years, 1 month ago
You need to instantiate the class with a local image file, not a directory.

You first get all your file names in an array, and then loop through it, instantiating the class each time with each file name.
$files = array('pic1.jpg', 'pic2.jpg', 'pic3.jpg');
foreach ($files as $file) {
  $handle = new Upload($file);
  if ($handle->uploaded) {
    $handle->image_resize            = true;
    $handle->image_ratio_y           = true;
    $handle->image_x                 = 100;
    $handle->Process('destination_directory/');
  }
}
Reply
Re: How to process multiple images that are inside a folder? new!
by Marcio Alves, 13 years, 1 month ago
Thank you for your help and patience. Based on them could solve the problem.

In order to help more people that perhaps might have the same problem, post here my code to send pictures zipped:

$uploadcaminho='test';
$codigo='13';

// START UPLOAD .ZIP FILE
print "VERIFICANDO ARQUIVOS ENVIADOS";
if ($uploadcaminho) {
  print "FOLDER DESTINATION (".$uploadcaminho.") [FOLDER - OK]";
} else {
  die('FOLDER DESTINATION - ERROR');
}
if (is_numeric($codigo)) {
  print "CODE (".$codigo.")[CODE - OK]";
} else {
  die('CODE ERROR');
}
if (!file_exists("/home/folder/".$uploadcaminho)) {
  mkdir("/home/folder/".$uploadcaminho, 0755);
  print "/home/folder/".$uploadcaminho." [FOLDER CREATED - OK]";
} else {
  print "/home/folder/".$uploadcaminho." [FOLDER EXISTing]";
}

print "START UPLOAD FILES ...";

if ($_FILES['my_field']['error'] == 0) {
  move_uploaded_file($_FILES['my_field']['tmp_name'], "/home/folder/".$uploadcaminho."/".$my_field_name);
  rename("/home/folder/".$uploadcaminho."/".$my_field_name, "/home/folder/".$uploadcaminho."/".$codigo.".zip");
  $my_field_name = $codigo.".zip";
} else {
  die("ERROr ".$_FILES['my_field']['error']);
}

// START UNZIP
$zip = new ZipArchive();
$zip->open("/home/folder/$uploadcaminho/$my_field_name");
$zip->extractTo("/home/folder/$uploadcaminho");
$zip->close(); 
echo "FILE $my_field_name UNZIP SUCCESS";

//DEL UNZIP FILE
unlink ("/home/folder/teste/$my_field_name");
echo "FILE $my_field_name DELETED WITH SUCESS!";

// ---------- MULTIPLE UPLOADS ----------
  
$diretorio = '/home/folder/teste/';
$ponteiro  = opendir($diretorio);

while ($nome_itens = readdir($ponteiro)) {
    $itens[] = $nome_itens;
}
sort($itens);

foreach ($itens as $k) {
  if ($k!="." && $k!=".."){
    if (is_dir($k)) {
      $pastas[]=$k;
    } else {
      $arquivos[]=$diretorio.'/'.$k;
    }
  }
}

foreach ($arquivos as $file) {
  $handle = new Upload($file);
  
  if ($handle->uploaded) {
    $handle->image_resize              = true;
    $handle->image_ratio_y           = true;
    $handle->image_x                 = 100;
    $handle->Process($diretorio);
    
    if ($handle->processed) {
      echo '  file uploaded with success';
      echo '  ' . round(filesize($handle->file_dst_pathname)/256)/4 . 'KB';
      echo '  link to the file just uploaded: "../../fotos/galeria/teste/' . $handle->file_dst_name . ' : ' . $handle->file_dst_name . '';
    } else {
      echo '  file not uploaded to the wanted location';
      echo '  Error: ' . $handle->error . '';
      echo '  Log: ' . $handle->log . '';
    }

  } else {
      echo '  file not uploaded on the server';
      echo '  Error: ' . $handle->error . '';
      echo '  Log: ' . $handle->log . '';
  }
}
Reply
Re: How to process multiple images that are inside a folder? new!
by colin, 13 years, 1 month ago
I glad you managed in the end.

Thank you for contributing your code.Reply
Re: How to process multiple images that are inside a folder? new!
by Marcio Alves, 13 years, 1 month ago
Colin,more issue:

How to get the width of the image being sent and whether it is greater than XX pixels, reduce.

This example s not function:
if($handle->image_x > 800) {
  $handle->image_resize = true;
  $handle->image_ratio = true;
  $handle->image_x = 800; 	//width
}
Reply
Re: How to process multiple images that are inside a folder? new!
by Marcio Alves, 13 years, 1 month ago
The issue has resolved. Code;

$handle->mime_getimagesize;
if($handle->image_src_x > 800) {
  $handle->image_resize = true;
  $handle->image_ratio = true;
  $handle->image_x = 800; 	//width
}
Reply