You are here

function _photos_unzip in Album Photos 6.2

Same name and namespace in other branches
  1. 7.3 photos.module \_photos_unzip()
2 calls to _photos_unzip()
photos_admin_import_validate in inc/photos.admin.inc
photos_upload_form_submit in inc/photos.edit.inc

File

./photos.module, line 1339

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();
    if ($zip
      ->open($source)) {
      for ($x = 0; $x < $zip->numFiles; ++$x) {
        $image = $zip
          ->statIndex($x);
        $ext = end(explode('.', $image['name']));
        if (in_array($ext, $type)) {
          $path = file_create_filename(_photos_rename($image['name'], $ext), photos_check_path());
          if ($filepath = file_save_data($zip
            ->getFromIndex($x), $path)) {
            $info = image_get_info($filepath);
            $file = new stdClass();
            $file = $value;
            $file->uid = $user->uid;
            $file->filepath = $filepath;
            if ($limits['resolution']) {
              list($width, $height) = explode('x', $limits['resolution']);
              if ($info['width'] > $width || $info['height'] > $height) {
                image_scale($filepath, $filepath, $width, $height);
              }
            }
            $file->filesize = $info['file_size'];
            $file->filename = $value->title ? $value->title : $image['name'];
            $file->filemime = $info['mime_type'];
            if ($file->fid = _photos_save_data($file)) {
              if (photos_image_date($file)) {
                $msg[] = 1;
              }
            }
          }
        }
      }
      $zip
        ->close();
      file_delete($source);
      $message = t('To extract the success of %num image', array(
        '%num' => count($msg),
      ));
    }
    else {
      $message = t('Compressed file does not exist, please check the path: ') . $source;
    }
  }
  return $message;
}