Exemple #1
0
 /**
  * Upload an image.
  *
  * @param  array $image
  * @param  string $destFolder
  *
  * @throws RuntimeException
  *
  * @return array
  */
 public function uploadImage($image, $destFolder)
 {
     // Load parameters.
     $params = JComponentHelper::getParams($this->option);
     /** @var  $params Joomla\Registry\Registry */
     // Joomla! media extension parameters
     $mediaParams = JComponentHelper::getParams('com_media');
     /** @var  $mediaParams Joomla\Registry\Registry */
     $names = array('image' => '', 'thumb' => '', 'square' => '');
     $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, $destFolder, $fileOptions);
     if (JString::strlen($uploadedName) > 0) {
         $KB = 1024 * 1024;
         $uploadMaxSize = $mediaParams->get('upload_maxsize') * $KB;
         $mimeTypes = explode(',', $mediaParams->get('upload_mime'));
         $imageExtensions = explode(',', $mediaParams->get('image_extensions'));
         // Prepare size validator.
         $fileSize = Joomla\Utilities\ArrayHelper::getValue($image, 'size', 0, 'int');
         // 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()) {
             throw new RuntimeException($file->getError());
         }
         try {
             $fileData = $file->upload();
             $names['image'] = $fileData['filename'];
         } catch (\Exception $e) {
             throw new RuntimeException(JText::_('COM_CROWDFUNDING_ERROR_FILE_CANT_BE_UPLOADED'));
         }
         // Generate thumbnails.
         // Create thumbnail.
         $options = 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($options, Prism\Constants::DO_NOT_REPLACE, 'reward_thumb_');
         $names['thumb'] = $fileData['filename'];
         // Create square image.
         $options = 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($options, Prism\Constants::DO_NOT_REPLACE, 'reward_square_');
         $names['square'] = $fileData['filename'];
     }
     return $names;
 }
Exemple #2
0
 /**
  * Upload images.
  *
  * @param  array $files
  * @param  string $destFolder
  * @param  array $rewardsIds
  *
  * @return array
  */
 public function uploadImages($files, $destFolder, $rewardsIds)
 {
     // Load parameters.
     $params = JComponentHelper::getParams($this->option);
     /** @var  $params Joomla\Registry\Registry */
     // 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();
     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($rewardId, $rewardsIds)) {
             continue;
         }
         $uploadedFile = Joomla\Utilities\ArrayHelper::getValue($image, 'tmp_name');
         $uploadedName = Joomla\String\String::trim(Joomla\Utilities\ArrayHelper::getValue($image, 'name'));
         $errorCode = Joomla\Utilities\ArrayHelper::getValue($image, 'error');
         $file = new Prism\File\Image();
         if (!empty($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;
             }
             // Generate temporary file name
             $ext = Joomla\String\String::strtolower(JFile::makeSafe(JFile::getExt($image['name'])));
             $generatedName = new Prism\String();
             $generatedName->generateRandomString(12, "reward_");
             $destFile = JPath::clean($destFolder . DIRECTORY_SEPARATOR . $generatedName . "." . $ext);
             // Prepare uploader object.
             $uploader = new Prism\File\Uploader\Local($uploadedFile);
             $uploader->setDestination($destFile);
             // Upload temporary file
             $file->setUploader($uploader);
             $file->upload();
             // Get file
             $imageSource = $file->getFile();
             if (!is_file($imageSource)) {
                 continue;
             }
             // Generate thumbnails.
             // Create thumbnail.
             $generatedName->generateRandomString(12, "reward_thumb_");
             $options = array("width" => $params->get("rewards_image_thumb_width", 200), "height" => $params->get("rewards_image_thumb_height", 200), "scale" => $params->get("image_resizing_scale", JImage::SCALE_INSIDE), "destination" => JPath::clean($destFolder . DIRECTORY_SEPARATOR . $generatedName . "." . $ext));
             $thumbSource = $file->createThumbnail($options);
             // Create square image.
             $generatedName->generateRandomString(12, "reward_square_");
             $options = array("width" => $params->get("rewards_image_square_width", 50), "height" => $params->get("rewards_image_square_height", 50), "scale" => $params->get("image_resizing_scale", JImage::SCALE_INSIDE), "destination" => JPath::clean($destFolder . DIRECTORY_SEPARATOR . $generatedName . "." . $ext));
             $squareSource = $file->createThumbnail($options);
             $names = array("image" => "", "thumb" => "", "square" => "");
             $names['image'] = basename($imageSource);
             $names["thumb"] = basename($thumbSource);
             $names["square"] = basename($squareSource);
             $images[$rewardId] = $names;
         }
     }
     return $images;
 }
 /**
  * Upload an image
  *
  * @param array $image Array with information about uploaded file.
  * @param string $destination
  *
  * @throws RuntimeException
  * @return array
  */
 public function uploadImage($image, $destination)
 {
     $app = JFactory::getApplication();
     /** @var $app JApplicationAdministrator */
     $uploadedFile = Joomla\Utilities\ArrayHelper::getValue($image, 'tmp_name');
     $uploadedName = Joomla\Utilities\ArrayHelper::getValue($image, 'name');
     $errorCode = Joomla\Utilities\ArrayHelper::getValue($image, 'error');
     // Joomla! media extension parameters
     /** @var  $mediaParams Joomla\Registry\Registry */
     $mediaParams = JComponentHelper::getParams('com_media');
     $file = new Prism\File\Image($image, $destination);
     // Prepare size validator.
     $KB = 1024 * 1024;
     $fileSize = (int) $app->input->server->get('CONTENT_LENGTH');
     $uploadMaxSize = $mediaParams->get('upload_maxsize') * $KB;
     // Prepare file size validator
     $sizeValidator = new Prism\File\Validator\Size($fileSize, $uploadMaxSize);
     // Prepare server validator.
     $serverValidator = new Prism\File\Validator\Server($errorCode);
     // Prepare image validator.
     $imageValidator = new Prism\File\Validator\Image($uploadedFile, $uploadedName);
     // Get allowed mime types from media manager options
     $mimeTypes = explode(',', $mediaParams->get('upload_mime'));
     $imageValidator->setMimeTypes($mimeTypes);
     // Get allowed image extensions from media manager options
     $imageExtensions = explode(',', $mediaParams->get('image_extensions'));
     $imageValidator->setImageExtensions($imageExtensions);
     $file->addValidator($sizeValidator)->addValidator($imageValidator)->addValidator($serverValidator);
     // Validate the file
     if (!$file->isValid()) {
         throw new RuntimeException($file->getError());
     }
     return $file->upload();
 }
 /**
  * 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;
 }
Exemple #5
0
 /**
  * Upload an image.
  *
  * @param  array $image
  * @param  string $destFolder
  *
  * @throws RuntimeException
  *
  * @return array
  */
 public function uploadImage($image, $destFolder)
 {
     // Load parameters.
     $params = JComponentHelper::getParams($this->option);
     /** @var  $params Joomla\Registry\Registry */
     // Joomla! media extension parameters
     $mediaParams = JComponentHelper::getParams("com_media");
     /** @var  $mediaParams Joomla\Registry\Registry */
     $names = array("image" => "", "thumb" => "", "square" => "");
     $KB = 1024 * 1024;
     $uploadMaxSize = $mediaParams->get("upload_maxsize") * $KB;
     $mimeTypes = explode(",", $mediaParams->get("upload_mime"));
     $imageExtensions = explode(",", $mediaParams->get("image_extensions"));
     $uploadedFile = JArrayHelper::getValue($image, 'tmp_name');
     $uploadedName = JString::trim(JArrayHelper::getValue($image, 'name'));
     $errorCode = JArrayHelper::getValue($image, 'error');
     $file = new Prism\File\Image();
     if (!empty($uploadedName)) {
         // Prepare size validator.
         $fileSize = (int) JArrayHelper::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()) {
             throw new RuntimeException($file->getError());
         }
         // Generate temporary file name
         $ext = JString::strtolower(JFile::makeSafe(JFile::getExt($image['name'])));
         $generatedName = Prism\Utilities\StringHelper::generateRandomString(12, "reward_");
         $destFile = JPath::clean($destFolder . DIRECTORY_SEPARATOR . $generatedName . "." . $ext);
         // Prepare uploader object.
         $uploader = new Prism\File\Uploader\Local($uploadedFile);
         $uploader->setDestination($destFile);
         // Upload temporary file
         $file->setUploader($uploader);
         $file->upload();
         // Get file
         $imageSource = $file->getFile();
         if (!is_file($imageSource)) {
             throw new RuntimeException(JText::_("COM_CROWDFUNDING_ERROR_FILE_CANT_BE_UPLOADED"));
         }
         // Generate thumbnails.
         // Create thumbnail.
         $generatedName = Prism\Utilities\StringHelper::generateRandomString(12, "reward_thumb_");
         $options = array("width" => $params->get("rewards_image_thumb_width", 200), "height" => $params->get("rewards_image_thumb_height", 200), "destination" => JPath::clean($destFolder . DIRECTORY_SEPARATOR . $generatedName . "." . $ext));
         $thumbSource = $file->createThumbnail($options);
         // Create square image.
         $generatedName = Prism\Utilities\StringHelper::generateRandomString(12, "reward_square_");
         $options = array("width" => $params->get("rewards_image_square_width", 50), "height" => $params->get("rewards_image_square_height", 50), "destination" => JPath::clean($destFolder . DIRECTORY_SEPARATOR . $generatedName . "." . $ext));
         $squareSource = $file->createThumbnail($options);
         $names['image'] = basename($imageSource);
         $names["thumb"] = basename($thumbSource);
         $names["square"] = basename($squareSource);
     }
     return $names;
 }