public static function PhotosUpload::unzip in Album Photos 8.4
Same name and namespace in other branches
- 8.5 src/PhotosUpload.php \Drupal\photos\PhotosUpload::unzip()
- 6.0.x src/PhotosUpload.php \Drupal\photos\PhotosUpload::unzip()
Unzip archive of image files.
2 calls to PhotosUpload::unzip()
- PhotosUpload::moveImageFiles in src/
PhotosUpload.php - Assist batch operation by moving or copying image files to album.
- PhotosUploadForm::submitForm in src/
Form/ PhotosUploadForm.php - Form submission handler.
File
- src/
PhotosUpload.php, line 173
Class
- PhotosUpload
- Functions to help with uploading images to albums.
Namespace
Drupal\photosCode
public static function unzip($source, $value, $scheme = 'default', $account = FALSE) {
$file_count = 0;
if (version_compare(PHP_VERSION, '5') >= 0) {
if (!is_file($source)) {
\Drupal::messenger()
->addMessage(t('Compressed file does not exist, please check the path: @src', [
'@src' => $source,
]));
return 0;
}
$type = [
'jpg',
'gif',
'png',
'jpeg',
'JPG',
'GIF',
'PNG',
'JPEG',
];
$zip = new \ZipArchive();
// Get relative path.
$default_scheme = \Drupal::config('system.file')
->get('default_scheme');
$relative_path = \Drupal::service('file_system')
->realpath($default_scheme . "://") . '/';
$source = str_replace($default_scheme . '://', $relative_path, $source);
// Open zip archive.
if ($zip
->open($source) === TRUE) {
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 = \Drupal::service('file_system')
->createFilename(PhotosUpload::rename($image['name'], $ext), PhotosUpload::path($scheme, '', $account));
if ($temp_file = file_save_data($zip
->getFromIndex($x), $path)) {
// Update file values.
$temp_file->pid = $value->pid;
$temp_file->nid = $value->nid;
// Use image file name as title.
$temp_file->title = $image['name'];
$temp_file->des = $value->des;
// Prepare file entity.
$file = $temp_file;
// Save image.
if (PhotosUpload::saveFile($file)) {
if (PhotosUpload::saveImage($file)) {
$file_count++;
}
}
}
}
}
$zip
->close();
// Delete zip file.
\Drupal::service('file_system')
->delete($source);
}
else {
\Drupal::messenger()
->addWarning(t('Compressed file does not exist, please try again: @src', [
'@src' => $source,
]));
}
}
return $file_count;
}