/** * saved temporaty photo to token by something we need to process * * @param UploadFile $item * @param \Kendo\Storage\StorageInterface $storage * * @return \Platform\Storage\Model\StorageFileTmp */ public function saveToTemporary(UploadFile $item, StorageInterface $storage = null) { if (null == $storage) { $storage = $this->getStorage(); } $pathPattern = $this->getPathPattern('temp', null, '.jpg'); $path = strtr($pathPattern, ['$maker' => 'temp_upload']); $storage->copyFromLocal($item->getPath(), $path); $temp = new StorageFileTmp(['name' => $item->getName(), 'path' => $path, 'size' => $item->getSize(), 'type' => $item->getType(), 'storage_id' => $storage->getId(), 'created_at' => KENDO_DATE_TIME]); $temp->save(); return $temp; }
/** * Process cover photo upload * * @param UploadFile $inputFile * @param array $thumbs * * @return int * @throws \InvalidArgumentException */ public function processCover(UploadFile $inputFile, $thumbs) { if (null == $thumbs) { $thumbs = $this->getDefaultPhotoThumbsSettings(); } // make sure sort caculated. krsort($thumbs); $fileData = []; $uploadTemporaryDir = KENDO_UPLOAD_DIR . '/'; $imageService = app()->imageProcess(); $path = $inputFile->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); // yes, keep origin of avatar file to save after all ... if (true) { $temp = strtr($pathTemp, ['$maker' => 'origin']); $img->resize(1280, null, function ($constraint) { $constraint->aspectRatio(); })->save($uploadTemporaryDir . $temp, 100); 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'])->save($uploadTemporaryDir . $temp); } else { $img->resize($thumb['width'], $thumb['height'])->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[0]; }