Example #1
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;
 }
Example #2
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;
 }