You are here

function _photos_unzip in Album Photos 7.3

Same name and namespace in other branches
  1. 6.2 photos.module \_photos_unzip()

Unzip.

2 calls to _photos_unzip()
photos_admin_import_validate in inc/photos.admin.inc
Validate upload to album form.
photos_upload_form_submit in inc/photos.edit.inc
Submit upload form.

File

./photos.module, line 2198
Implementation of photos.module.

Code

function _photos_unzip($source, $value, $limits = FALSE) {
  global $user;
  if (version_compare(PHP_VERSION, '5') >= 0) {
    if (!is_file($source)) {
      return t('Compressed file does not exist, please check the path') . ': ' . $source;
    }
    $type = array(
      'jpg',
      'gif',
      'png',
      'jpeg',
      'JPG',
      'GIF',
      'PNG',
      'JPEG',
    );
    $zip = new ZipArchive();

    // @todo update?
    $relative_path = variable_get('file_' . file_default_scheme() . '_path', conf_path() . '/files/');
    $source = str_replace(file_default_scheme() . '://', $relative_path, $source);
    if ($zip
      ->open($source)) {
      for ($x = 0; $x < $zip->numFiles; ++$x) {
        $image = $zip
          ->statIndex($x);
        $filename_parts = explode('.', $image['name']);
        $ext = end($filename_parts);
        if (in_array($ext, $type)) {
          $path = file_create_filename(_photos_rename($image['name'], $ext), photos_check_path());
          if ($temp_file = file_save_data($zip
            ->getFromIndex($x), $path)) {
            $info = image_get_info($temp_file->uri);
            $file = (object) array_merge((array) $temp_file, (array) $value);
            if ($limits['resolution']) {
              list($width, $height) = explode('x', $limits['resolution']);

              // @todo update or remove file limits.
              if ($info['width'] > $width || $info['height'] > $height) {
                $file_image = array(
                  'source' => $file->uri,
                  'info' => $info,
                  'toolkit' => image_get_toolkit(),
                );
                image_scale($file_image, $width, $height);
              }
            }

            // $file->filename = $value->title ? $value->title : $image['name'];
            if ($file->fid = _photos_save_data($file)) {
              if (photos_image_date($file)) {
                $msg[] = 1;
              }
            }
          }
        }
      }
      $zip
        ->close();
      file_unmanaged_delete($source);
      $message = t('%num images extracted successfully!', array(
        '%num' => count($msg),
      ));
    }
    else {
      $message = t('Compressed file does not exist, please check the path') . ': ' . $source;
    }
  }
  return $message;
}