/** * Upload images. * * @param array $files * @param array $rewardsIds * @param array $options * @param Joomla\Registry\Registry $params * * @throws \InvalidArgumentException * @return array */ public function uploadImages(array $files, array $rewardsIds, array $options, $params) { // Joomla! media extension parameters $mediaParams = JComponentHelper::getParams('com_media'); /** @var $mediaParams Joomla\Registry\Registry */ $KB = 1024 * 1024; $uploadMaxSize = $mediaParams->get('upload_maxsize') * $KB; $mimeTypes = explode(',', $mediaParams->get('upload_mime')); $imageExtensions = explode(',', $mediaParams->get('image_extensions')); $images = array(); $rewardsIds = Joomla\Utilities\ArrayHelper::toInteger($rewardsIds); jimport('Prism.libs.Flysystem.init'); $temporaryAdapter = new League\Flysystem\Adapter\Local($options['temporary_path']); $storageAdapter = new League\Flysystem\Adapter\Local($options['destination_path']); $temporaryFilesystem = new League\Flysystem\Filesystem($temporaryAdapter); $storageFilesystem = new League\Flysystem\Filesystem($storageAdapter); $manager = new League\Flysystem\MountManager(['temporary' => $temporaryFilesystem, 'storage' => $storageFilesystem]); foreach ($files as $rewardId => $image) { // If the image is set to not valid reward, continue to next one. // It is impossible to store image to missed reward. if (!in_array((int) $rewardId, $rewardsIds, true)) { continue; } $uploadedFile = Joomla\Utilities\ArrayHelper::getValue($image, 'tmp_name'); $uploadedName = JString::trim(Joomla\Utilities\ArrayHelper::getValue($image, 'name')); $errorCode = Joomla\Utilities\ArrayHelper::getValue($image, 'error'); $fileOptions = new \Joomla\Registry\Registry(array('filename_length' => 12)); $file = new Prism\File\Image($image, $options['temporary_path'], $fileOptions); $result = array('image' => '', 'thumb' => '', 'square' => ''); if ($uploadedName !== null and $uploadedName !== '') { // Prepare size validator. $fileSize = (int) Joomla\Utilities\ArrayHelper::getValue($image, 'size'); // Prepare file size validator. $sizeValidator = new Prism\File\Validator\Size($fileSize, $uploadMaxSize); // Prepare server validator. $serverValidator = new Prism\File\Validator\Server($errorCode, array(UPLOAD_ERR_NO_FILE)); // Prepare image validator. $imageValidator = new Prism\File\Validator\Image($uploadedFile, $uploadedName); // Get allowed mime types from media manager options $imageValidator->setMimeTypes($mimeTypes); // Get allowed image extensions from media manager options $imageValidator->setImageExtensions($imageExtensions); $file->addValidator($sizeValidator)->addValidator($imageValidator)->addValidator($serverValidator); // Validate the file if (!$file->isValid()) { continue; } // Upload it in the temp folder. $fileData = $file->upload(); if ($manager->has('temporary://' . $fileData['filename'])) { // Copy original image. $originalFile = $fileData['filename']; $result['image'] = 'reward_' . $originalFile; $manager->copy('temporary://' . $originalFile, 'storage://' . $result['image']); // Create thumbnail. $resizeOptions = array('width' => $params->get('rewards_image_thumb_width', 200), 'height' => $params->get('rewards_image_thumb_height', 200), 'scale' => $params->get('rewards_image_resizing_scale', JImage::SCALE_INSIDE)); $fileData = $file->resize($resizeOptions, Prism\Constants::DO_NOT_REPLACE, 'reward_thumb_'); $manager->move('temporary://' . $fileData['filename'], 'storage://' . $fileData['filename']); $result['thumb'] = $fileData['filename']; // Create square image. $resizeOptions = array('width' => $params->get('rewards_image_square_width', 50), 'height' => $params->get('rewards_image_square_height', 50), 'scale' => $params->get('rewards_image_resizing_scale', JImage::SCALE_INSIDE)); $fileData = $file->resize($resizeOptions, Prism\Constants::DO_NOT_REPLACE, 'reward_square_'); $manager->move('temporary://' . $fileData['filename'], 'storage://' . $fileData['filename']); $result['square'] = $fileData['filename']; // Remove the original file from temporary folder. $manager->delete('temporary://' . $originalFile); $images[$rewardId] = $result; } } } return $images; }
/** * Copys file from one location to another, between mounts * * @param mixed $source The source path or object * @param mixed $dest The destination path or object * @param bool $overwrite Whether or not to overwrite any existing files by the same name * @return bool **/ public static function copy($source, $dest, $overwrite = false) { list($source, $sourceName, $sourceMount) = self::parseLocation($source); list($dest, $destName, $destMount) = self::parseLocation($dest); // Make sure we got the mounts we need if (!$sourceMount) { throw new \Exception("'{$sourceMount}' has not been mounted", 500); } if (!$destMount) { throw new \Exception("'{$destMount}' has not been mounted", 500); } // Check to see if destination already exists if ($destMount->has(self::getRelativePath($dest))) { if ($overwrite) { $destMount->delete(self::getRelativePath($dest)); } else { return true; } } // Create mount manager $manager = new \League\Flysystem\MountManager([$sourceName => $sourceMount, $destName => $destMount]); // Do copy return $manager->copy($source, $dest); }