Common\Core\Model::generateThumbnails PHP Метод

generateThumbnails() публичный статический Метод

- x128 as foldername to generate an image where the height will be 128px, the width will be calculated based on the aspect ratio.
public static generateThumbnails ( string $path, string $sourceFile )
$path string The path wherein the thumbnail-folders will be stored.
$sourceFile string 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);
        }
    }

Usage Example

Пример #1
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);
 }