/** * @param array $tempIdList * * @return UploadFileList */ public function getTempInputFileList($tempIdList) { if (!is_array($tempIdList)) { $tempIdList = [$tempIdList]; } if (empty($tempIdList)) { $tempIdList = ['-1']; } $list = new UploadFileList(); $items = app()->table('platform_storage_file_tmp')->select()->where('id IN ?', $tempIdList)->all(); foreach ($items as $item) { if (!$item instanceof StorageFileTmp) { continue; } $list->addFile($item->getName(), $item->getType(), $item->getUrl(), 0, $item->getSize(), 'temporary'); } return $list; }
/** * Process Upload photo from file input list => file item list * * @param UploadFileList $fileList * @param array $thumbs * * @return array|null * @throws \InvalidArgumentException */ public function processUploadPhotos(UploadFileList $fileList, $thumbs) { if (null == $thumbs) { $thumbs = $this->getDefaultPhotoThumbsSettings(); } krsort($thumbs); $fileData = []; $uploadTemporaryDir = KENDO_UPLOAD_DIR . '/'; $imageService = app()->imageProcess(); foreach ($fileList->getFiles() as $file) { $path = $file->getPath(); $alloc = []; $pathTemp = app()->storageService()->getPathPattern('photo', null, '.jpg'); $dir = dirname($uploadTemporaryDir . $pathTemp); if (!is_dir($dir)) { if (!mkdir($dir, 0777, 1)) { throw new \InvalidArgumentException("Could not write to directory [{$dir}]"); } } // load original image $img = $imageService->load($path); // keep origin if (true) { $temp = strtr($pathTemp, ['$maker' => 'origin']); $img->resize(1280, null, function ($constraint) { $constraint->aspectRatio(); $constraint->upsize(); })->save($uploadTemporaryDir . $temp); list($w, $h) = getimagesize($uploadTemporaryDir . $temp, $imageInfo); $alloc['origin'] = ['maker' => 'origin', 'path' => $temp, 'width' => (int) $w, 'height' => (int) $h, 'main_type' => 'photo', 'created_at' => KENDO_DATE_TIME]; } foreach ($thumbs as $maker => $thumb) { $temp = strtr($pathTemp, ['$maker' => $maker]); if ($thumb['crop']) { $img->fit($thumb['width'], $thumb['height'], function ($constraint) { $constraint->aspectRatio(); })->save($uploadTemporaryDir . $temp); } else { $img->resize($thumb['width'], $thumb['height'], function ($constraint) { $constraint->aspectRatio(); })->save($uploadTemporaryDir . $temp); } list($w, $h) = getimagesize($uploadTemporaryDir . $temp, $imageInfo); $alloc['children'][$maker] = ['maker' => $maker, 'path' => $temp, 'width' => (int) $w, 'height' => (int) $h, 'main_type' => 'photo', 'created_at' => KENDO_DATE_TIME]; } $fileData[] = $alloc; } $response = null; if (!empty($fileData)) { $storage = app()->storageService(); $response = $storage->saveAllProcessedPhoto($fileData, $uploadTemporaryDir); foreach ($fileData as $item) { if (!empty($item['origin'])) { if (!empty($item['origin']['path'])) { @unlink($uploadTemporaryDir . $item['origin']['path']); } } if (!empty($item['children'])) { foreach ($item['children'] as $child) { if (!empty($child['path'])) { @unlink($uploadTemporaryDir . $child['path']); } } } } } return $response; }