/**
  * Prepare safe profile alias.
  *
  * @param string $alias
  * @param int $userId
  *
  * @return string
  */
 public static function safeAlias($alias, $userId = 0)
 {
     $filter = new \JFilterInput();
     $alias = \JString::strtolower($filter->clean($alias, 'ALNUM'));
     // Check for valid alias.
     $aliasValidator = new Validator\Profile\Alias(\JFactory::getDbo(), $alias, $userId);
     if (!$aliasValidator->isValid()) {
         if (!$alias) {
             $alias = StringHelper::generateRandomString(16);
         } else {
             $alias .= mt_rand(10, 1000);
         }
     }
     return $alias;
 }
Example #2
0
 /**
  * Resize the temporary file to new one.
  *
  * <code>
  * $image = $this->input->files->get('media', array(), 'array');
  * $rootFolder = "/root/joomla/tmp";
  *
  * $resizeOptions = array(
  *    'width'  => $options['thumb_width'],
  *    'height' => $options['thumb_height'],
  *    'scale'  => $options['thumb_scale']
  * );
  *
  * $file = new Prism\File\Image($image, $rootFolder);
  *
  * $file->upload();
  * $fileData = $file->resize($resize);
  * </code>
  *
  * @param array $options
  * @param bool $replace Replace the original file with the new one.
  * @param string $prefix Filename prefix.
  *
  * @throws \RuntimeException
  *
  * @return array
  */
 public function resize(array $options, $replace = false, $prefix = '')
 {
     if (!$this->file) {
         throw new \RuntimeException(\JText::sprintf('LIB_PRISM_ERROR_FILE_NOT_FOUND_S', $this->file));
     }
     // Resize image.
     $image = new \JImage();
     $image->loadFile($this->file);
     if (!$image->isLoaded()) {
         throw new \RuntimeException(\JText::sprintf('LIB_PRISM_ERROR_FILE_NOT_FOUND_S', $this->file));
     }
     // Resize to general size.
     $width = ArrayHelper::getValue($options, 'width', 640);
     $width = $width < 50 ? 50 : $width;
     $height = ArrayHelper::getValue($options, 'height', 480);
     $height = $height < 50 ? 50 : $height;
     $scale = ArrayHelper::getValue($options, 'scale', \JImage::SCALE_INSIDE);
     $image->resize($width, $height, false, $scale);
     // Generate new name.
     $generatedName = StringHelper::generateRandomString($this->options->get('filename_length', 16)) . '.png';
     if (is_string($prefix) and $prefix !== '') {
         $generatedName = $prefix . $generatedName;
     }
     $file = \JPath::clean($this->rootFolder . '/' . $generatedName);
     // Store to file.
     $image->toFile($file, IMAGETYPE_PNG);
     if ($replace) {
         \JFile::delete($this->file);
         $this->file = $file;
     }
     // Prepare meta data about the file.
     $fileData = array('filename' => $generatedName, 'filepath' => $file, 'type' => 'image');
     $fileData = array_merge($fileData, $this->prepareImageProperties($this->file));
     return $fileData;
 }