Beispiel #1
0
 /**
  * Creates a thumbnail from this field
  *
  * @return	bool
  * @param	string $filename							The name of the file.
  * @param	int[optional] $width						The width.
  * @param	int[optional] $height						The height.
  * @param	bool[optional] $allowEnlargement			Is enlargment allowed?
  * @param	bool[optional] $forceOriginalAspectRatio	Should we force the original aspect ratio?
  * @param	int[optional] $quality						The quality, only applies on jpg-images.
  */
 public function createThumbnail($filename, $width = null, $height = null, $allowEnlargement = false, $forceOriginalAspectRatio = true, $quality = 100)
 {
     $thumbnail = new SpoonThumbnail($this->getTempFileName(), $width, $height, true);
     $thumbnail->setAllowEnlargement($allowEnlargement);
     $thumbnail->setForceOriginalAspectRatio($forceOriginalAspectRatio);
     return $thumbnail->parseToFile($filename, $quality);
 }
Beispiel #2
0
 /**
  * Generate thumbnails based on the folders in the path
  * Use
  *  - 128x128 as foldername to generate an image where the width will be
  *      128px and the height will be 128px
  *  - 128x as foldername to generate an image where the width will be
  *      128px, the height will be calculated based on the aspect ratio.
  *  - x128 as foldername to generate an image where the height will be
  *      128px, the width will be calculated based on the aspect ratio.
  *
  * @param string $path       The path wherein the thumbnail-folders will be stored.
  * @param string $sourceFile The location of the source file.
  */
 public static function generateThumbnails($path, $sourceFile)
 {
     // get folder listing
     $folders = self::getThumbnailFolders($path);
     $filename = basename($sourceFile);
     // loop folders
     foreach ($folders as $folder) {
         // generate the thumbnail
         $thumbnail = new \SpoonThumbnail($sourceFile, $folder['width'], $folder['height']);
         $thumbnail->setAllowEnlargement(true);
         // if the width & height are specified we should ignore the aspect ratio
         if ($folder['width'] !== null && $folder['height'] !== null) {
             $thumbnail->setForceOriginalAspectRatio(false);
         }
         $thumbnail->parseToFile($folder['path'] . '/' . $filename);
     }
 }
Beispiel #3
0
 private function uploadFile()
 {
     //--Check if the file is an image or file
     if ($this->isImage()) {
         // the image path
         $path = FRONTEND_FILES_PATH . '/Media/Images';
         if (!\SpoonDirectory::exists($path . '/Source')) {
             \SpoonDirectory::create($path . '/Source');
         }
     } else {
         // the file path
         $path = FRONTEND_FILES_PATH . '/Media/Files';
     }
     // create folders if needed
     // build the filename
     $filename = $this->checkFilename();
     $item = array();
     $item["filename"] = $filename;
     $item["extension"] = $this->field->getExtension();
     $item["created_on"] = BackendModel::getUTCDate('Y-m-d H:i:s');
     $item["filesize"] = $this->field->getFileSize("b");
     $data = array();
     //--Check if file is an image to specify data
     if ($this->isImage()) {
         $item["filetype"] = $this->fieldTypeImage;
         //--Put file on disk
         $this->field->moveFile($path . "/Source/" . $filename);
         // create folders if needed
         if (!\SpoonDirectory::exists($path . '/128x128')) {
             \SpoonDirectory::create($path . '/128x128');
         }
         //--Create all tumbs/resizes of file
         $thumbnail = new \SpoonThumbnail($path . "/Source/" . $filename);
         $thumbnail->setAllowEnlargement(true);
         \Common\Core\Model::generateThumbnails($path, $path . '/Source/' . $filename);
     } else {
         $item["filetype"] = $this->fieldTypeFile;
         // move the source file
         $this->field->moveFile($path . "/" . $filename);
     }
     //--Serialize data
     $item["data"] = serialize($data);
     //--Store item so we can access it
     $this->item = $item;
     //--Insert into media
     return BackendModel::getContainer()->get('database')->insert("media", $item);
 }
Beispiel #4
0
 /**
  * Execute the action
  */
 public function execute()
 {
     // call parent, this will probably add some general CSS/JS or other required files
     parent::execute();
     ini_set('max_execution_time', 150);
     // Get all the modules
     $modules = BackendModel::getModules();
     // Check if modules is not empty
     if (!empty($modules)) {
         // Create filesystem object
         $filesystem = new Filesystem();
         // Loop the modules
         foreach ($modules as $module) {
             // Create var dir for ease of use
             $dir = FRONTEND_FILES_PATH . '/' . $module . '/Images/';
             // Check if dir exists
             if ($filesystem->exists($dir . 'Source/')) {
                 // Create Finder object
                 $finderDir = new Finder();
                 // Get all the dirs in the modules/images folder.
                 $dirs = $finderDir->directories()->in($dir)->exclude(array("Source", 'source'));
                 // Check if $dirs is not empty
                 if (!empty($dirs)) {
                     // Create Finder object for the files
                     $finderFiles = new Finder();
                     // Get all the files in the source-dir
                     $files = $finderFiles->files()->in($dir . 'Source/');
                     // Check if $files is not empty
                     if (!empty($files)) {
                         // Loop all the dirs
                         foreach ($dirs as $row) {
                             // Explode the dir-name
                             $chunks = explode("x", $row->getBasename(), 2);
                             // Create folder array
                             $folder = array();
                             $folder['dirname'] = $row->getBasename();
                             $folder['path'] = $row->getRealPath();
                             $folder['width'] = $chunks[0] != '' ? (int) $chunks[0] : null;
                             $folder['height'] = $chunks[1] != '' ? (int) $chunks[1] : null;
                             // Loop all the files
                             foreach ($files as $file) {
                                 set_time_limit(150);
                                 // Check if the file exists
                                 if (!$filesystem->exists($dir . $row->getBasename() . '/' . $file->getBasename())) {
                                     // generate the thumbnail
                                     $thumbnail = new \SpoonThumbnail($dir . 'Source/' . $file->getBasename(), $folder['width'], $folder['height']);
                                     $thumbnail->setAllowEnlargement(true);
                                     // if the width & height are specified we should ignore the aspect ratio
                                     if ($folder['width'] !== null && $folder['height'] !== null) {
                                         $thumbnail->setForceOriginalAspectRatio(false);
                                     }
                                     $thumbnail->parseToFile($folder['path'] . '/' . $file->getBasename());
                                 }
                             }
                         }
                     }
                 }
             }
         }
     }
     echo "ok";
     die;
     // redirect
     //$this->redirect(BackendModel::createURLForAction('index') . '&report=generate_images');
 }
 public function testIsSupportedFileType()
 {
     $this->assertTrue(SpoonThumbnail::isSupportedFileType(dirname(dirname(realpath(__FILE__))) . '/tmp/spoon.jpg'));
 }
Beispiel #6
0
 /**
  * Validate the form add image
  *
  * @return void
  */
 private function validateForm()
 {
     //--Check if the add-image form is submitted
     if ($this->frm->isSubmitted()) {
         //--Clean up fields in the form (NOT ALLOWED: fields from plupload like name are deleted)
         //$this->frm->cleanupFields();
         //--Get image field
         $filImage = $this->frm->getField('images');
         //--Check if the field is filled in
         if ($filImage->isFilled()) {
             //--Image extension and mime type
             $filImage->isAllowedExtension(array('jpg', 'png', 'gif', 'jpeg'), BL::err('JPGGIFAndPNGOnly'));
             $filImage->isAllowedMimeType(array('image/jpg', 'image/png', 'image/gif', 'image/jpeg'), BL::err('JPGGIFAndPNGOnly'));
             //--Check if there are no errors.
             $strError = $filImage->getErrors();
             if ($strError === null) {
                 //--Get the filename
                 $strFilename = BackendGalleryModel::checkFilename(substr($_REQUEST["name"], 0, 0 - (strlen($filImage->getExtension()) + 1)), $filImage->getExtension());
                 //--Fill in the item
                 $item = array();
                 $item["album_id"] = (int) $this->id;
                 $item["user_id"] = BackendAuthentication::getUser()->getUserId();
                 $item["language"] = BL::getWorkingLanguage();
                 $item["filename"] = $strFilename;
                 $item["description"] = "";
                 $item["publish_on"] = BackendModel::getUTCDate();
                 $item["hidden"] = "N";
                 $item["sequence"] = BackendGalleryModel::getMaximumImageSequence($this->id) + 1;
                 //--the image path
                 $imagePath = FRONTEND_FILES_PATH . '/Gallery/Images';
                 //--create folders if needed
                 $resolutions = $this->get('fork.settings')->get("Gallery", 'resolutions', false);
                 foreach ($resolutions as $res) {
                     if (!\SpoonDirectory::exists($imagePath . '/' . $res)) {
                         \SpoonDirectory::create($imagePath . '/' . $res);
                         // Create filesystem object
                         $filesystem = new Filesystem();
                         // Create var dir for ease of use
                         $dir = $imagePath;
                         // Check if dir exists
                         if ($filesystem->exists($dir . '/Source/')) {
                             // Create Finder object for the files
                             $finderFiles = new Finder();
                             // Get all the files in the source-dir
                             $files = $finderFiles->files()->in($dir . '/Source/');
                             // Check if $files is not empty
                             if (!empty($files)) {
                                 // Explode the dir-name
                                 $chunks = explode("x", $res, 2);
                                 // Create folder array
                                 $folder = array();
                                 $folder['width'] = $chunks[0] != '' ? (int) $chunks[0] : null;
                                 $folder['height'] = $chunks[1] != '' ? (int) $chunks[1] : null;
                                 // Loop all the files
                                 foreach ($files as $file) {
                                     set_time_limit(150);
                                     // Check if the file exists
                                     if (!$filesystem->exists($imagePath . '/' . $res . '/' . $file->getBasename())) {
                                         // generate the thumbnail
                                         $thumbnail = new \SpoonThumbnail($dir . '/Source/' . $file->getBasename(), $folder['width'], $folder['height']);
                                         $thumbnail->setAllowEnlargement(true);
                                         // if the width & height are specified we should ignore the aspect ratio
                                         if ($folder['width'] !== null && $folder['height'] !== null) {
                                             $thumbnail->setForceOriginalAspectRatio(false);
                                         }
                                         $thumbnail->parseToFile($imagePath . '/' . $res . '/' . $file->getBasename());
                                     }
                                 }
                             }
                         }
                     }
                 }
                 if (!\SpoonDirectory::exists($imagePath . '/Source')) {
                     \SpoonDirectory::create($imagePath . '/Source');
                 }
                 if (!\SpoonDirectory::exists($imagePath . '/128x128')) {
                     \SpoonDirectory::create($imagePath . '/128x128');
                 }
                 if (!\SpoonDirectory::exists($imagePath . '/800x')) {
                     \SpoonDirectory::create($imagePath . '/800x');
                 }
                 if (!\SpoonDirectory::exists($imagePath . '/200x')) {
                     \SpoonDirectory::create($imagePath . '/200x');
                 }
                 if (!\SpoonDirectory::exists($imagePath . '/400x300')) {
                     \SpoonDirectory::create($imagePath . '/400x300');
                 }
                 //--image provided?
                 if ($filImage->isFilled()) {
                     //--upload the image & generate thumbnails
                     $filImage->generateThumbnails($imagePath, $item["filename"]);
                 }
                 //--Add item to the database
                 $idInsert = BackendGalleryModel::insert($item);
                 $item['id'] = $idInsert;
                 //--Create html for ajax
                 $tpl = new Template();
                 $txtDescription = $this->frm->addTextarea("description_" . $idInsert, $item['description']);
                 $item['field_description'] = $txtDescription->setAttribute('style', 'resize: none;')->parse();
                 //--Parse filename to get name
                 $path_parts = pathinfo(FRONTEND_FILES_PATH . '/Gallery/Images/Source/' . $item['filename']);
                 $item['name'] = $path_parts['filename'];
                 $folders = BackendModel::getThumbnailFolders(FRONTEND_FILES_PATH . '/Gallery/Images', true);
                 foreach ($folders as $folder) {
                     $item['image_' . $folder['dirname']] = $folder['url'] . '/' . $folder['dirname'] . '/' . $item['filename'];
                 }
                 $tpl->assign('images', array($item));
                 $html = $tpl->getContent(BACKEND_MODULES_PATH . '/Gallery/Layout/Templates/Ajax/Image.tpl');
                 //Send html (ajax response)
                 $this->output(self::OK, $html, BL::msg('Success'));
             }
         }
     }
 }
Beispiel #7
0
 /**
  * Generates the desired image formats based on the source image
  *
  * @param string $path The path we write the images to.
  * @param string $filename The name of the source file.
  * @param array[optional] $formats The formats of the images to generate based on the source.
  * @return bool	Returns true if every file succeeded
  */
 public static function generateImages($path, $filename, array $formats = null)
 {
     // check input
     if (empty($filename)) {
         return false;
     }
     // create the path up to the source dir
     if (!\SpoonDirectory::exists($path . '/source')) {
         \SpoonDirectory::create($path . '/source');
     }
     // source path
     $pathSource = $path . '/source';
     // formats found
     if (!empty($formats)) {
         // loop the formats
         foreach ($formats as $format) {
             // create the path for this product
             if (!\SpoonDirectory::exists($path . '/' . $format['size'])) {
                 \SpoonDirectory::create($path . '/' . $format['size']);
             }
             // exploded format
             $explodedFormat = explode('x', $format['size']);
             // set enlargement/aspect ratio
             $allowEnlargement = isset($format['allow_enlargement']) ? $format['allow_enlargement'] : true;
             $forceAspectRatio = isset($format['force_aspect_ratio']) ? $format['force_aspect_ratio'] : true;
             // get measurements of the source file
             $sourceDimensions = getimagesize($pathSource . '/' . $filename);
             // source width is bigger than what it should be
             $width = $sourceDimensions[0] > $explodedFormat[0] ? $explodedFormat[0] : $sourceDimensions[0];
             $height = isset($explodedFormat[1]) && $sourceDimensions[1] > $explodedFormat[1] ? $explodedFormat[1] : $sourceDimensions[1];
             // check if height is empty or not
             if (empty($height)) {
                 $height = null;
             }
             // make a thumbnail for the provided format
             $thumbnail = new \SpoonThumbnail($pathSource . '/' . $filename, $width, $forceAspectRatio ? null : $height);
             $thumbnail->setAllowEnlargement($allowEnlargement);
             $thumbnail->setForceOriginalAspectRatio($forceAspectRatio);
             $success[] = $thumbnail->parseToFile($path . '/' . $format['size'] . '/' . $filename);
             unset($thumbnail);
         }
     }
 }