public static function createThumb($path, $width = 100, $height = 100, $crop = 2) { $myImage = new JImage(); $myImage->loadFile(JPATH_SITE . DS . $path); if ($myImage->isLoaded()) { // $filename = end(explode('/', $path)); $filename = JFile::getName($path); $filefolder = substr(md5(self::getFolderPath($path)), 1, 10); $newfilename = $width . 'x' . $height . '_' . $filefolder . '_' . JFile::makeSafe($filename); $fileExists = JFile::exists(JPATH_CACHE . '/' . $newfilename); if (!$fileExists) { $resizedImage = $myImage->resize($width, $height, true, $crop); $properties = $myImage->getImageFileProperties($path); $mime = $properties->mime; if ($mime == 'image/jpeg') { $type = IMAGETYPE_JPEG; } elseif ($mime = 'image/png') { $type = IMAGETYPE_PNG; } elseif ($mime = 'image/gif') { $type = IMAGETYPE_GIF; } $resizedImage->toFile(JPATH_CACHE . '/' . $newfilename, $type); } return $newfilename; } else { return "My file is not loaded"; } }
/** * Create a thumbnail from an image file. * * <code> * $myFile = "/tmp/myfile.jpg"; * * $options = array( * "destination" => "image/mypic.jpg", * "width" => 200, * "height" => 200, * "scale" => JImage::SCALE_INSIDE * ); * * $file = new PrismFileImage($myFile); * $file->createThumbnail($options); * * </code> * * @param array $options Some options used in the process of generating thumbnail. * * @throws \InvalidArgumentException * @throws \RuntimeException * * @return string A location to the new file. */ public function createThumbnail($options) { $width = ArrayHelper::getValue($options, "width", 100); $height = ArrayHelper::getValue($options, "height", 100); $scale = ArrayHelper::getValue($options, "scale", \JImage::SCALE_INSIDE); $destination = ArrayHelper::getValue($options, "destination"); if (!$destination) { throw new \InvalidArgumentException(\JText::_("LIB_PRISM_ERROR_INVALID_FILE_DESTINATION")); } // Generate thumbnail. $image = new \JImage(); $image->loadFile($this->file); if (!$image->isLoaded()) { throw new \RuntimeException(\JText::sprintf('LIB_PRISM_ERROR_FILE_NOT_FOUND', $this->file)); } // Resize the file as a new object $thumb = $image->resize($width, $height, true, $scale); $fileName = basename($this->file); $ext = \JString::strtolower(\JFile::getExt(\JFile::makeSafe($fileName))); switch ($ext) { case "gif": $type = IMAGETYPE_GIF; break; case "png": $type = IMAGETYPE_PNG; break; case IMAGETYPE_JPEG: default: $type = IMAGETYPE_JPEG; } $thumb->toFile($destination, $type); return $destination; }
public static function getProportion($path) { $myImage = new JImage(); $imgPath = JPATH_SITE . DS . $path; $myImage->loadFile($imgPath); if ($myImage->isLoaded()) { $properties = $myImage->getImageFileProperties($imgPath); return $properties->height / $properties->width * 100; } else { return; } }
public static function createThumb($path, $width = 100, $height = 100, $crop = 2, $cachefolder = 'hgimages', $external = 0) { $myImage = new JImage(); if (!$external) { $myImage->loadFile(JPATH_SITE . DS . $path); } else { $myImage->loadFile($path); } if ($myImage->isLoaded()) { // $filename = end(explode('/', $path)); $filename = JFile::getName($path); $filefolder = substr(md5(self::getFolderPath($path)), 1, 10); $newfilename = $width . 'x' . $height . 'x' . $crop . '_' . $filefolder . '_' . JFile::makeSafe($filename); $hgimages = JPATH_CACHE . '/' . $cachefolder . '/'; if (!JFolder::exists($hgimages)) { JFolder::create($hgimages); } $fileExists = JFile::exists($hgimages . $newfilename); if (!$fileExists) { switch ($crop) { // Case for self::CROP case 4: $resizedImage = $myImage->crop($width, $height, null, null, true); break; // Case for self::CROP_RESIZE // Case for self::CROP_RESIZE case 5: $resizedImage = $myImage->cropResize($width, $height, true); break; default: $resizedImage = $myImage->resize($width, $height, true, $crop); break; } $properties = $myImage->getImageFileProperties($path); $mime = $properties->mime; if ($mime == 'image/jpeg') { $type = IMAGETYPE_JPEG; } elseif ($mime = 'image/png') { $type = IMAGETYPE_PNG; } elseif ($mime = 'image/gif') { $type = IMAGETYPE_GIF; } $resizedImage->toFile($hgimages . $newfilename, $type); } return $newfilename; } else { return "My file is not loaded"; } }
public function onMediaEditorProcess($filePath) { jimport('joomla.filesystem.file'); $image = new JImage($filePath); if ($image->isLoaded() == false) { throw new LogicException('Failed to load image'); } $image->rotate(180, 0, false); $extension = JFile::getExt($filePath); if (in_array($extension, array('png', 'gif'))) { $imageType = $extension; } else { $imageType = 'jpg'; } $image->toFile($filePath, $imageType); }
/** * Crop the image and generates smaller ones. * * @param string $file * @param array $options * * @throws Exception * * @return array */ public function cropImage($file, $options) { // Resize image $image = new JImage(); $image->loadFile($file); if (!$image->isLoaded()) { throw new Exception(JText::sprintf('COM_CROWDFUNDING_ERROR_FILE_NOT_FOUND', $file)); } $destinationFolder = Joomla\Utilities\ArrayHelper::getValue($options, "destination"); // Generate temporary file name $generatedName = new Prism\String(); $generatedName->generateRandomString(32); $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; // Create main image $width = Joomla\Utilities\ArrayHelper::getValue($options, "width", 200); $width = $width < 25 ? 50 : $width; $height = Joomla\Utilities\ArrayHelper::getValue($options, "height", 200); $height = $height < 25 ? 50 : $height; $left = Joomla\Utilities\ArrayHelper::getValue($options, "x", 0); $top = Joomla\Utilities\ArrayHelper::getValue($options, "y", 0); $image->crop($width, $height, $left, $top, false); // Resize to general size. $width = Joomla\Utilities\ArrayHelper::getValue($options, "resize_width", 200); $width = $width < 25 ? 50 : $width; $height = Joomla\Utilities\ArrayHelper::getValue($options, "resize_height", 200); $height = $height < 25 ? 50 : $height; $image->resize($width, $height, false); // Store to file. $image->toFile($imageFile, IMAGETYPE_PNG); // Load parameters. $params = JComponentHelper::getParams($this->option); /** @var $params Joomla\Registry\Registry */ // Create small image $width = $params->get("image_small_width", 100); $height = $params->get("image_small_height", 100); $image->resize($width, $height, false); $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); $image->toFile($squareFile, IMAGETYPE_PNG); $names = array("image" => $imageName, "image_small" => $smallName, "image_square" => $squareName); // Remove the temporary file. if (is_file($file)) { JFile::delete($file); } return $names; }
/** * Upload a pitch image. * * @param array $image * * @throws Exception * * @return array */ public function uploadPitchImage($image) { $app = JFactory::getApplication(); /** @var $app JApplicationSite */ $uploadedFile = JArrayHelper::getValue($image, 'tmp_name'); $uploadedName = JArrayHelper::getValue($image, 'name'); $errorCode = JArrayHelper::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 */ 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 ITPrismFile(); // Prepare size validator. $KB = 1024 * 1024; $fileSize = (int) $app->input->server->get('CONTENT_LENGTH'); $uploadMaxSize = $mediaParams->get("upload_maxsize") * $KB; $sizeValidator = new ITPrismFileValidatorSize($fileSize, $uploadMaxSize); // Prepare server validator. $serverValidator = new ITPrismFileValidatorServer($errorCode, array(UPLOAD_ERR_NO_FILE)); // Prepare image validator. $imageValidator = new ITPrismFileValidatorImage($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']))); jimport("itprism.string"); $generatedName = new ITPrismString(); $generatedName->generateRandomString(32); $tmpDestFile = $tmpFolder . DIRECTORY_SEPARATOR . $generatedName . "." . $ext; // Prepare uploader object. $uploader = new ITPrismFileUploaderLocal($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; }
/** * Generate the file preview. U */ private function _generatePreview() { // Create thumbnail (for jpeg) if there is none $supportedImageMine = array('image/png', 'image/jpeg'); if (in_array($this->mimetype, $supportedImageMine)) { jimport('joomla.image'); require_once JPATH_ROOT . DS . 'libraries' . DS . 'joomla' . DS . 'image' . DS . 'filters' . DS . 'sharpen.php'; $image = new JImage(JPATH_ROOT . DS . $this->path); if (!$image->isLoaded()) { return false; } $pathinfo = pathinfo($this->path); $width = $image->getWidth(); $height = $image->getHeight(); // Generate preview if ($width > 640) { $height = 640 / $width * $height; $width = 640; } if ($height > 640) { $width = 640 / $height * $width; $height = 640; } // Resize for preview $image = $image->resize($width, $height); $previewPath = JPATH_ROOT . DS . $pathinfo['dirname'] . DS . $pathinfo['filename'] . '_preview.jpg'; $image->filter('sharpen'); $image->toFile($previewPath, IMAGETYPE_JPEG, array('quality' => 90)); // crop them to predefined aspect ratio if necessary // and the resize them if ($width / $height > 1.3) { $image = $image->crop($height * 1.3, $height, ($width - $height * 1.3) / 2, 0); $image = $image->resize(StreamTableFile::PHOTO_THUMB_WIDTH, StreamTableFile::PHOTO_THUMB_HEIGHT); } elseif ($height / $width > 1.3) { $image = $image->crop($width, $width * 1.3, 0, $height - $width * 1.3); $image = $image->resize(StreamTableFile::PHOTO_THUMB_HEIGHT, StreamTableFile::PHOTO_THUMB_WIDTH); } else { $image = $image->resize(StreamTableFile::PHOTO_THUMB_WIDTH, StreamTableFile::PHOTO_THUMB_WIDTH); } $thumbPath = JPATH_ROOT . DS . $pathinfo['dirname'] . DS . $pathinfo['filename'] . '_thumb.jpg'; $image->toFile($thumbPath, IMAGETYPE_JPEG, array('quality' => 100)); if (JFile::exists($thumbPath)) { $this->setParam('has_preview', true); $this->setParam('thumb_path', $pathinfo['dirname'] . DS . $pathinfo['filename'] . '_thumb.jpg'); $this->setParam('preview_path', $pathinfo['dirname'] . DS . $pathinfo['filename'] . '_preview.jpg'); $this->setParam('width', $width); $this->setParam('height', $height); } } else { $this->setParam('has_preview', false); $this->setParam('thumb_path', ''); $this->setParam('preview_path', ''); $this->setParam('width', ''); $this->setParam('height', ''); } }
/** * 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; }
/** * 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; }
/** * Crop the image and generates smaller ones. * * @param string $file * @param array $options * @param Joomla\Registry\Registry $params * * @throws Exception * * @return array */ public function cropImage($file, $options, $params) { // Resize image $image = new JImage(); $image->loadFile($file); if (!$image->isLoaded()) { throw new Exception(JText::sprintf('COM_SOCIALCOMMUNITY_ERROR_FILE_NOT_FOUND', $file)); } $destinationFolder = Joomla\Utilities\ArrayHelper::getValue($options, 'destination'); // Generate temporary file name $generatedName = Prism\Utilities\StringHelper::generateRandomString(24); $profileName = $generatedName . '_profile.png'; $smallName = $generatedName . '_small.png'; $squareName = $generatedName . '_square.png'; $iconName = $generatedName . '_icon.png'; $imageFile = JPath::clean($destinationFolder . DIRECTORY_SEPARATOR . $profileName); $smallFile = JPath::clean($destinationFolder . DIRECTORY_SEPARATOR . $smallName); $squareFile = JPath::clean($destinationFolder . DIRECTORY_SEPARATOR . $squareName); $iconFile = JPath::clean($destinationFolder . DIRECTORY_SEPARATOR . $iconName); // Create profile image. $width = Joomla\Utilities\ArrayHelper::getValue($options, 'width', 200); $width = $width < 25 ? 50 : $width; $height = Joomla\Utilities\ArrayHelper::getValue($options, 'height', 200); $height = $height < 25 ? 50 : $height; $left = Joomla\Utilities\ArrayHelper::getValue($options, 'x', 0); $top = Joomla\Utilities\ArrayHelper::getValue($options, 'y', 0); $image->crop($width, $height, $left, $top, false); // Resize to general size. $width = $params->get('image_width', 200); $width = $width < 25 ? 50 : $width; $height = $params->get('image_height', 200); $height = $height < 25 ? 50 : $height; $image->resize($width, $height, false); // Store to file. $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); $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); $image->toFile($squareFile, IMAGETYPE_PNG); // Create icon image. $width = $params->get('image_icon_width', 25); $height = $params->get('image_icon_height', 25); $image->resize($width, $height, false); $image->toFile($iconFile, IMAGETYPE_PNG); $names = array('image_profile' => $profileName, 'image_small' => $smallName, 'image_square' => $squareName, 'image_icon' => $iconName); // Remove the temporary file. if (JFile::exists($file)) { JFile::delete($file); } return $names; }
/** * 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); }
/** * Upload an 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 */ $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 = $destination . DIRECTORY_SEPARATOR . $imageName; // Get the scale method. $scaleMethod = $params->get('image_resizing_scale', JImage::SCALE_INSIDE); // Create main image $width = $params->get('pitch_image_width', 600); $height = $params->get('pitch_image_height', 400); $image->resize($width, $height, false, $scaleMethod); $image->toFile($imageFile, IMAGETYPE_PNG); // Remove the temporary file. if (is_file($tmpDestFile)) { JFile::delete($tmpDestFile); } return $imageName; }
/** * 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); }