Ejemplo n.º 1
0
 public function uploadFiles($files, $options)
 {
     $result = array();
     $destination = JArrayHelper::getValue($options, "destination");
     $maxSize = JArrayHelper::getValue($options, "max_size");
     $legalExtensions = JArrayHelper::getValue($options, "legal_extensions");
     $legalFileTypes = JArrayHelper::getValue($options, "legal_types");
     // check for error
     foreach ($files as $fileData) {
         // Upload image
         if (!empty($fileData['name'])) {
             $uploadedFile = JArrayHelper::getValue($fileData, 'tmp_name');
             $uploadedName = JArrayHelper::getValue($fileData, 'name');
             $errorCode = JArrayHelper::getValue($fileData, 'error');
             $file = new Prism\File\File();
             // Prepare size validator.
             $KB = 1024 * 1024;
             $fileSize = JArrayHelper::getValue($fileData, "size");
             $uploadMaxSize = $maxSize * $KB;
             // 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.
             $typeValidator = new Prism\File\Validator\Type($uploadedFile, $uploadedName);
             // Get allowed MIME types.
             $mimeTypes = explode(",", $legalFileTypes);
             $mimeTypes = array_map('trim', $mimeTypes);
             $typeValidator->setMimeTypes($mimeTypes);
             // Get allowed file extensions.
             $fileExtensions = explode(",", $legalExtensions);
             $fileExtensions = array_map('trim', $fileExtensions);
             $typeValidator->setLegalExtensions($fileExtensions);
             $file->addValidator($sizeValidator)->addValidator($typeValidator)->addValidator($serverValidator);
             // Validate the file
             if (!$file->isValid()) {
                 throw new RuntimeException($file->getError());
             }
             // Generate file name
             $baseName = JString::strtolower(JFile::makeSafe(basename($fileData['name'])));
             $ext = JFile::getExt($baseName);
             $generatedName = new Prism\String();
             $generatedName->generateRandomString(6);
             $destinationFile = $destination . DIRECTORY_SEPARATOR . $generatedName . "." . $ext;
             // Prepare uploader object.
             $uploader = new Prism\File\Uploader\Local($uploadedFile);
             $uploader->setDestination($destinationFile);
             // Upload temporary file
             $file->setUploader($uploader);
             $file->upload();
             // Get file
             $fileSource = $file->getFile();
             if (!JFile::exists($fileSource)) {
                 throw new RuntimeException(JText::_("COM_CROWDFUNDING_ERROR_FILE_CANT_BE_UPLOADED"));
             }
             $result[] = array("title" => $baseName, "filename" => basename($fileSource));
         }
     }
     return $result;
 }
Ejemplo n.º 2
0
 public function uploadFile($fileData, $type)
 {
     $app = JFactory::getApplication();
     /** @var $app JApplicationAdministrator */
     jimport('joomla.filesystem.archive');
     $uploadedFile = Joomla\Utilities\ArrayHelper::getValue($fileData, 'tmp_name');
     $uploadedName = Joomla\Utilities\ArrayHelper::getValue($fileData, 'name');
     $errorCode = Joomla\Utilities\ArrayHelper::getValue($fileData, 'error');
     $destination = JPath::clean($app->get('tmp_path') . DIRECTORY_SEPARATOR . JFile::makeSafe($uploadedName));
     $file = new Prism\File\File();
     // Prepare size validator.
     $KB = 1024 * 1024;
     $fileSize = (int) $app->input->server->get('CONTENT_LENGTH');
     $mediaParams = JComponentHelper::getParams('com_media');
     /** @var $mediaParams Joomla\Registry\Registry */
     $uploadMaxSize = $mediaParams->get('upload_maxsize') * $KB;
     // Prepare 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));
     $file->addValidator($sizeValidator);
     $file->addValidator($serverValidator);
     // Validate the file
     if (!$file->isValid()) {
         throw new RuntimeException($file->getError());
     }
     // Prepare uploader object.
     $uploader = new Prism\File\Uploader\Local($uploadedFile);
     $uploader->setDestination($destination);
     // Upload the file
     $file->setUploader($uploader);
     $file->upload();
     $fileName = basename($destination);
     // Extract file if it is archive.
     $ext = JString::strtolower(JFile::getExt($fileName));
     if (strcmp($ext, 'zip') === 0) {
         $destFolder = JPath::clean($app->get('tmp_path') . '/' . $type);
         if (is_dir($destFolder)) {
             JFolder::delete($destFolder);
         }
         $filePath = $this->extractFile($destination, $destFolder);
     } else {
         $filePath = $destination;
     }
     return $filePath;
 }
Ejemplo n.º 3
0
 /**
  * Upload and resize the image.
  *
  * @param array $image
  * @param string $destination
  *
  * @throws Exception
  *
  * @return array
  */
 public function uploadImage($image, $destination)
 {
     $app = JFactory::getApplication();
     /** @var $app JApplicationSite */
     $uploadedFile = Joomla\Utilities\ArrayHelper::getValue($image, 'tmp_name');
     $uploadedName = Joomla\Utilities\ArrayHelper::getValue($image, 'name');
     $errorCode = Joomla\Utilities\ArrayHelper::getValue($image, 'error');
     // 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 */
     $file = new Prism\File\File();
     // Prepare size validator.
     $KB = 1024 * 1024;
     $fileSize = (int) $app->input->server->get('CONTENT_LENGTH');
     $uploadMaxSize = $mediaParams->get("upload_maxsize") * $KB;
     // Prepare file size validator
     $fileSizeValidator = 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
     $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);
     // Prepare image size validator.
     $imageSizeValidator = new Prism\File\Validator\Image\Size($uploadedFile);
     $imageSizeValidator->setMinWidth($params->get("image_width", 200));
     $imageSizeValidator->setMinHeight($params->get("image_height", 200));
     $file->addValidator($fileSizeValidator)->addValidator($serverValidator)->addValidator($imageValidator)->addValidator($imageSizeValidator);
     // Validate the file
     if (!$file->isValid()) {
         throw new RuntimeException($file->getError());
     }
     // Generate temporary file name
     $ext = JFile::makeSafe(JFile::getExt($image['name']));
     $generatedName = new Prism\String();
     $generatedName->generateRandomString(16);
     $temporaryFile = $destination . DIRECTORY_SEPARATOR . $generatedName . "." . $ext;
     // Prepare uploader object.
     $uploader = new Prism\File\Uploader\Local($uploadedFile);
     $uploader->setDestination($temporaryFile);
     // Upload temporary file
     $file->setUploader($uploader);
     $file->upload();
     // Get file
     $temporaryFile = JPath::clean($file->getFile());
     if (!is_file($temporaryFile)) {
         throw new RuntimeException(JText::_('COM_CROWDFUNDING_ERROR_FILE_CANT_BE_UPLOADED'));
     }
     return $temporaryFile;
 }
Ejemplo n.º 4
0
 /**
  * Upload a pitch image.
  *
  * @param  array $image
  *
  * @throws Exception
  *
  * @return array
  */
 public function uploadPitchImage($image)
 {
     $app = JFactory::getApplication();
     /** @var $app JApplicationSite */
     $uploadedFile = Joomla\Utilities\ArrayHelper::getValue($image, 'tmp_name');
     $uploadedName = Joomla\Utilities\ArrayHelper::getValue($image, 'name');
     $errorCode = Joomla\Utilities\ArrayHelper::getValue($image, 'error');
     // Load parameters.
     $params = JComponentHelper::getParams($this->option);
     /** @var  $params Joomla\Registry\Registry */
     $destFolder = JPath::clean(JPATH_ROOT . DIRECTORY_SEPARATOR . $params->get("images_directory", "images/crowdfunding"));
     $tmpFolder = $app->get("tmp_path");
     // Joomla! media extension parameters
     $mediaParams = JComponentHelper::getParams("com_media");
     /** @var  $mediaParams Joomla\Registry\Registry */
     $file = new Prism\File\File();
     // Prepare size validator.
     $KB = 1024 * 1024;
     $fileSize = (int) $app->input->server->get('CONTENT_LENGTH');
     $uploadMaxSize = $mediaParams->get("upload_maxsize") * $KB;
     $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
     $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());
     }
     // Generate temporary file name
     $ext = JString::strtolower(JFile::makeSafe(JFile::getExt($image['name'])));
     $generatedName = Prism\Utilities\StringHelper::generateRandomString(32);
     $tmpDestFile = JPath::clean($tmpFolder . DIRECTORY_SEPARATOR . $generatedName . "." . $ext);
     // Prepare uploader object.
     $uploader = new Prism\File\Uploader\Local($uploadedFile);
     $uploader->setDestination($tmpDestFile);
     // Upload temporary file
     $file->setUploader($uploader);
     $file->upload();
     // Get file
     $tmpDestFile = $file->getFile();
     if (!is_file($tmpDestFile)) {
         throw new Exception('COM_CROWDFUNDING_ERROR_FILE_CANT_BE_UPLOADED');
     }
     // Resize image
     $image = new JImage();
     $image->loadFile($tmpDestFile);
     if (!$image->isLoaded()) {
         throw new Exception(JText::sprintf('COM_CROWDFUNDING_ERROR_FILE_NOT_FOUND', $tmpDestFile));
     }
     $imageName = $generatedName . "_pimage.png";
     $imageFile = JPath::clean($destFolder . DIRECTORY_SEPARATOR . $imageName);
     // Create main image
     $width = $params->get("pitch_image_width", 600);
     $height = $params->get("pitch_image_height", 400);
     $image->resize($width, $height, false);
     $image->toFile($imageFile, IMAGETYPE_PNG);
     // Remove the temporary
     if (is_file($tmpDestFile)) {
         JFile::delete($tmpDestFile);
     }
     return $imageName;
 }
Ejemplo n.º 5
0
 /**
  * Store the file in a folder of the extension.
  *
  * @param array $image
  * @param bool $resizeImage
  *
  * @throws \RuntimeException
  * @throws \Exception
  * @throws \InvalidArgumentException
  *
  * @return array
  */
 public function uploadImage($image, $resizeImage = false)
 {
     $app = JFactory::getApplication();
     /** @var $app JApplicationSite */
     $uploadedFile = ArrayHelper::getValue($image, 'tmp_name');
     $uploadedName = ArrayHelper::getValue($image, 'name');
     $errorCode = ArrayHelper::getValue($image, 'error');
     $params = JComponentHelper::getParams($this->option);
     /** @var  $params Joomla\Registry\Registry */
     $filesystemHelper = new Prism\Filesystem\Helper($params);
     $mediaFolder = $filesystemHelper->getMediaFolder();
     $destinationFolder = JPath::clean(JPATH_ROOT . DIRECTORY_SEPARATOR . $mediaFolder);
     $temporaryFolder = $app->get('tmp_path');
     // Joomla! media extension parameters
     $mediaParams = JComponentHelper::getParams('com_media');
     /** @var $mediaParams Joomla\Registry\Registry */
     $file = new Prism\File\File();
     // Prepare size validator.
     $KB = 1024 * 1024;
     $fileSize = (int) $app->input->server->get('CONTENT_LENGTH');
     $uploadMaxSize = $mediaParams->get('upload_maxsize') * $KB;
     // Prepare file validators.
     $sizeValidator = new Prism\File\Validator\Size($fileSize, $uploadMaxSize);
     $serverValidator = new Prism\File\Validator\Server($errorCode, array(UPLOAD_ERR_NO_FILE));
     $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($serverValidator)->addValidator($imageValidator);
     // Validate the file
     if (!$file->isValid()) {
         throw new RuntimeException($file->getError());
     }
     // Generate temporary file name
     $ext = strtolower(JFile::makeSafe(JFile::getExt($image['name'])));
     $generatedName = Prism\Utilities\StringHelper::generateRandomString();
     $temporaryFile = $generatedName . '_reward.' . $ext;
     $temporaryDestination = JPath::clean($temporaryFolder . DIRECTORY_SEPARATOR . $temporaryFile);
     // Prepare uploader object.
     $uploader = new Prism\File\Uploader\Local($uploadedFile);
     $uploader->setDestination($temporaryDestination);
     // Upload temporary file
     $file->setUploader($uploader);
     $file->upload();
     $temporaryFile = $file->getFile();
     if (!is_file($temporaryFile)) {
         throw new Exception('COM_GAMIFICATION_ERROR_FILE_CANT_BE_UPLOADED');
     }
     // Resize image
     $image = new JImage();
     $image->loadFile($temporaryFile);
     if (!$image->isLoaded()) {
         throw new Exception(JText::sprintf('COM_GAMIFICATION_ERROR_FILE_NOT_FOUND', $temporaryDestination));
     }
     $imageName = $generatedName . '_image.png';
     $smallName = $generatedName . '_small.png';
     $squareName = $generatedName . '_square.png';
     $imageFile = $destinationFolder . DIRECTORY_SEPARATOR . $imageName;
     $smallFile = $destinationFolder . DIRECTORY_SEPARATOR . $smallName;
     $squareFile = $destinationFolder . DIRECTORY_SEPARATOR . $squareName;
     $scaleOption = $params->get('image_resizing_scale', JImage::SCALE_INSIDE);
     // Create main image
     if (!$resizeImage) {
         $image->toFile($imageFile, IMAGETYPE_PNG);
     } else {
         $width = $params->get('image_width', 200);
         $height = $params->get('image_height', 200);
         $image->resize($width, $height, false, $scaleOption);
         $image->toFile($imageFile, IMAGETYPE_PNG);
     }
     // Create small image
     $width = $params->get('image_small_width', 100);
     $height = $params->get('image_small_height', 100);
     $image->resize($width, $height, false, $scaleOption);
     $image->toFile($smallFile, IMAGETYPE_PNG);
     // Create square image
     $width = $params->get('image_square_width', 50);
     $height = $params->get('image_square_height', 50);
     $image->resize($width, $height, false, $scaleOption);
     $image->toFile($squareFile, IMAGETYPE_PNG);
     $names = array('image' => $imageName, 'image_small' => $smallName, 'image_square' => $squareName);
     // Remove the temporary file.
     if (JFile::exists($temporaryFile)) {
         JFile::delete($temporaryFile);
     }
     return $names;
 }
Ejemplo n.º 6
0
 /**
  * Store the file in a folder of the extension.
  *
  * @param array $image
  *
  * @throws \InvalidArgumentException
  * @throws \Exception
  * @throws \UnexpectedValueException
  * @throws \RuntimeException
  *
  * @return string
  */
 public function uploadImage($image)
 {
     $app = JFactory::getApplication();
     /** @var $app JApplicationSite */
     $uploadedFile = ArrayHelper::getValue($image, 'tmp_name');
     $uploadedName = ArrayHelper::getValue($image, 'name');
     $errorCode = ArrayHelper::getValue($image, 'error');
     $params = JComponentHelper::getParams($this->option);
     /** @var  $params Joomla\Registry\Registry */
     $filesystemHelper = new Prism\Filesystem\Helper($params);
     $mediaFolder = $filesystemHelper->getMediaFolder();
     $destinationFolder = JPath::clean(JPATH_ROOT . DIRECTORY_SEPARATOR . $mediaFolder);
     // Joomla! media extension parameters
     $mediaParams = JComponentHelper::getParams('com_media');
     /** @var $mediaParams Joomla\Registry\Registry */
     $file = new Prism\File\File();
     // Prepare size validator.
     $KB = 1024 * 1024;
     $fileSize = (int) $app->input->server->get('CONTENT_LENGTH');
     $uploadMaxSize = $mediaParams->get('upload_maxsize') * $KB;
     // Prepare file validators.
     $sizeValidator = new Prism\File\Validator\Size($fileSize, $uploadMaxSize);
     $serverValidator = new Prism\File\Validator\Server($errorCode, array(UPLOAD_ERR_NO_FILE));
     $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($serverValidator)->addValidator($imageValidator);
     // Validate the file
     if (!$file->isValid()) {
         throw new RuntimeException($file->getError());
     }
     // Generate temporary file name
     $ext = strtolower(JFile::makeSafe(JFile::getExt($image['name'])));
     $generatedName = Prism\Utilities\StringHelper::generateRandomString(16);
     $imageName = $generatedName . '_badge.' . $ext;
     $destination = JPath::clean($destinationFolder . DIRECTORY_SEPARATOR . $imageName);
     // Prepare uploader object.
     $uploader = new Prism\File\Uploader\Local($uploadedFile);
     $uploader->setDestination($destination);
     // Upload temporary file
     $file->setUploader($uploader);
     $file->upload();
     $source = $file->getFile();
     return basename($source);
 }
Ejemplo n.º 7
0
 /**
  * Upload an image
  *
  * @param array $image Array with information about uploaded file.
  *
  * @throws RuntimeException
  * @return array
  */
 public function uploadImage($image)
 {
     $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');
     $tmpFolder = $app->get("tmp_path");
     /** @var  $params Joomla\Registry\Registry */
     $params = JComponentHelper::getParams($this->option);
     $destFolder = JPath::clean(JPATH_ROOT . DIRECTORY_SEPARATOR . $params->get("images_directory", "/images/profiles"));
     $options = array("width" => $params->get("image_width"), "height" => $params->get("image_height"), "small_width" => $params->get("image_small_width"), "small_height" => $params->get("image_small_height"), "square_width" => $params->get("image_square_width"), "square_height" => $params->get("image_square_height"), "icon_width" => $params->get("image_icon_width"), "icon_height" => $params->get("image_icon_height"));
     // Joomla! media extension parameters
     /** @var  $mediaParams Joomla\Registry\Registry */
     $mediaParams = JComponentHelper::getParams("com_media");
     jimport("itprism.file");
     jimport("itprism.file.uploader.local");
     jimport("itprism.file.validator.size");
     jimport("itprism.file.validator.image");
     jimport("itprism.file.validator.server");
     $file = new Prism\File\File();
     // 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, array(UPLOAD_ERR_NO_FILE));
     // 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());
     }
     // Generate temporary file name
     $ext = JFile::makeSafe(JFile::getExt($image['name']));
     $generatedName = new Prism\String();
     $generatedName->generateRandomString(32);
     $tmpDestFile = $tmpFolder . DIRECTORY_SEPARATOR . $generatedName . "." . $ext;
     // Prepare uploader object.
     $uploader = new Prism\File\Uploader\Local($uploadedFile);
     $uploader->setDestination($tmpDestFile);
     // Upload temporary file
     $file->setUploader($uploader);
     $file->upload();
     // Get file
     $tmpSourceFile = $file->getFile();
     if (!is_file($tmpSourceFile)) {
         throw new RuntimeException('COM_SOCIALCOMMUNITY_ERROR_FILE_CANT_BE_UPLOADED');
     }
     // Generate file names for the image files.
     $generatedName->generateRandomString(32);
     $imageName = $generatedName . "_image.png";
     $smallName = $generatedName . "_small.png";
     $squareName = $generatedName . "_square.png";
     $iconName = $generatedName . "_icon.png";
     // Resize image
     $image = new JImage();
     $image->loadFile($tmpSourceFile);
     if (!$image->isLoaded()) {
         throw new RuntimeException(JText::sprintf('COM_SOCIALCOMMUNITY_ERROR_FILE_NOT_FOUND', $tmpSourceFile));
     }
     $imageFile = $destFolder . DIRECTORY_SEPARATOR . $imageName;
     $smallFile = $destFolder . DIRECTORY_SEPARATOR . $smallName;
     $squareFile = $destFolder . DIRECTORY_SEPARATOR . $squareName;
     $iconFile = $destFolder . DIRECTORY_SEPARATOR . $iconName;
     // Create profile picture
     $width = Joomla\Utilities\ArrayHelper::getValue($options, "image_width", 200);
     $height = Joomla\Utilities\ArrayHelper::getValue($options, "image_height", 200);
     $image->resize($width, $height, false);
     $image->toFile($imageFile, IMAGETYPE_PNG);
     // Create small profile picture
     $width = Joomla\Utilities\ArrayHelper::getValue($options, "small_width", 100);
     $height = Joomla\Utilities\ArrayHelper::getValue($options, "small_height", 100);
     $image->resize($width, $height, false);
     $image->toFile($smallFile, IMAGETYPE_PNG);
     // Create square picture
     $width = Joomla\Utilities\ArrayHelper::getValue($options, "square_width", 50);
     $height = Joomla\Utilities\ArrayHelper::getValue($options, "square_height", 50);
     $image->resize($width, $height, false);
     $image->toFile($squareFile, IMAGETYPE_PNG);
     // Create icon picture
     $width = Joomla\Utilities\ArrayHelper::getValue($options, "icon_width", 24);
     $height = Joomla\Utilities\ArrayHelper::getValue($options, "icon_height", 24);
     $image->resize($width, $height, false);
     $image->toFile($iconFile, IMAGETYPE_PNG);
     // Remove the temporary file
     if (JFile::exists($tmpSourceFile)) {
         JFile::delete($tmpSourceFile);
     }
     return $names = array("image" => $imageName, "image_small" => $smallName, "image_icon" => $iconName, "image_square" => $squareName);
 }
Ejemplo n.º 8
-1
 /**
  * Upload an image
  *
  * @param array $image Array with information about uploaded file.
  *
  * @throws RuntimeException
  * @return array
  */
 public function uploadImage($image)
 {
     $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');
     $params = JComponentHelper::getParams($this->option);
     /** @var  $params Joomla\Registry\Registry */
     $temporaryFolder = JPath::clean($app->get('tmp_path'));
     // Joomla! media extension parameters
     /** @var  $mediaParams Joomla\Registry\Registry */
     $mediaParams = JComponentHelper::getParams('com_media');
     $file = new Prism\File\File();
     // 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());
     }
     // Generate temporary file name
     $ext = JFile::makeSafe(JFile::getExt($image['name']));
     $generatedName = Prism\Utilities\StringHelper::generateRandomString(16);
     $originalFile = JPath::clean($temporaryFolder . DIRECTORY_SEPARATOR . $generatedName . '.' . $ext);
     if (!JFile::upload($uploadedFile, $originalFile)) {
         throw new \RuntimeException(\JText::_('COM_SOCIALCOMMUNITY_ERROR_FILE_CANT_BE_UPLOADED'));
     }
     // Generate file names for the image files.
     $generatedName = Prism\Utilities\StringHelper::generateRandomString(16);
     $imageName = $generatedName . '_image.png';
     $smallName = $generatedName . '_small.png';
     $squareName = $generatedName . '_square.png';
     $iconName = $generatedName . '_icon.png';
     // Resize image
     $image = new JImage();
     $image->loadFile($originalFile);
     if (!$image->isLoaded()) {
         throw new RuntimeException(JText::sprintf('COM_SOCIALCOMMUNITY_ERROR_FILE_NOT_FOUND', $originalFile));
     }
     $imageFile = JPath::clean($temporaryFolder . DIRECTORY_SEPARATOR . $imageName);
     $smallFile = JPath::clean($temporaryFolder . DIRECTORY_SEPARATOR . $smallName);
     $squareFile = JPath::clean($temporaryFolder . DIRECTORY_SEPARATOR . $squareName);
     $iconFile = JPath::clean($temporaryFolder . DIRECTORY_SEPARATOR . $iconName);
     // Create profile picture
     $width = $params->get('image_width', 200);
     $height = $params->get('image_height', 200);
     $image->resize($width, $height, false);
     $image->toFile($imageFile, IMAGETYPE_PNG);
     // Create small profile picture
     $width = $params->get('small_width', 100);
     $height = $params->get('small_height', 100);
     $image->resize($width, $height, false);
     $image->toFile($smallFile, IMAGETYPE_PNG);
     // Create square picture
     $width = $params->get('square_width', 50);
     $height = $params->get('square_height', 50);
     $image->resize($width, $height, false);
     $image->toFile($squareFile, IMAGETYPE_PNG);
     // Create icon picture
     $width = $params->get('icon_width', 24);
     $height = $params->get('icon_height', 24);
     $image->resize($width, $height, false);
     $image->toFile($iconFile, IMAGETYPE_PNG);
     // Remove the temporary file
     if (JFile::exists($originalFile)) {
         JFile::delete($originalFile);
     }
     return $names = array('image' => $imageName, 'image_small' => $smallName, 'image_icon' => $iconName, 'image_square' => $squareName);
 }