diff --git a/code/tasks/FTFileMakerTask.php b/code/tasks/FTFileMakerTask.php index ab34a5e..82ddfd9 100644 --- a/code/tasks/FTFileMakerTask.php +++ b/code/tasks/FTFileMakerTask.php @@ -9,6 +9,8 @@ use GuzzleHttp\Client; use GuzzleHttp\Promise; use SilverStripe\Security\Member; use SilverStripe\Security\Security; +use SilverStripe\Core\Path; +use SilverStripe\Core\Manifest\ModuleResourceLoader; /** * Creates sample folder and file structure, useful to test performance, @@ -106,7 +108,14 @@ class FTFileMakerTask extends BuildTask private static $depth = 2; /** - * Number of folders to create certain hierachy. + * When true, watermark images for unique image binary per Image record + * @var bool + * @config + */ + private static $uniqueImages = true; + + /** + * Number of folders to create certain hierarchy. * @var int[] * @config */ @@ -301,6 +310,11 @@ class FTFileMakerTask extends BuildTask $doSetOldCreationDate = (bool) self::config()->get('doSetOldCreationDate'); $doRandomlyPublish = (bool) self::config()->get('doRandomlyPublish'); + $uniqueImages = (bool) self::config()->get('uniqueImages'); + $watermarkPath = ModuleResourceLoader::singleton()->resolvePath( + 'silverstripe/frameworktest: images/silverstripe.png' + ); + for ($i = 1; $i <= $folderCount; $i++) { $folder = new Folder([ 'ParentID' => $parentID, @@ -336,6 +350,16 @@ class FTFileMakerTask extends BuildTask $class = $this->fixtureFileTypes[$randomFileName]; + // If we're uniquifying images, copy the path and watermark it. + if ($class === Image::class && $uniqueImages) { + $copyPath = Path::join(dirname($randomFilePath), $fileName); + copy($randomFilePath, $copyPath); + $newPath = $this->watermarkImage($watermarkPath, $copyPath); + if ($newPath) { + $randomFilePath = $newPath; + } + } + $file = new $class([ 'ParentID' => $folder->ID, 'Title' => $fileName, @@ -343,6 +367,7 @@ class FTFileMakerTask extends BuildTask ]); $file->File->setFromLocalFile($randomFilePath, $folder->getFilename() . $fileName); + // Randomly set old created date (for testing) if ($doSetOldCreationDate) { if (rand(0, 10) === 0) { @@ -391,4 +416,52 @@ class FTFileMakerTask extends BuildTask } } + /** + * @param string $stampPath + * @param string $targetPath + * @return null + */ + protected function watermarkImage(string $stampPath, string $targetPath): ?string + { + // Load the stamp and the photo to apply the watermark to + $ext = strtolower(pathinfo($targetPath, PATHINFO_EXTENSION)); + $functions = null; + if (in_array($ext, ['jpeg', 'jpg'])) { + $functions = ['imagecreatefromjpeg', 'imagejpeg']; + } elseif ($ext === 'png') { + $functions = ['imagecreatefrompng', 'imagepng']; + } + if (!$functions) { + return null; + } + + $stamp = imagecreatefrompng($stampPath); + $targetImage = call_user_func($functions[0], $targetPath); + + // Set the margins for the stamp and get the height/width of the stamp image + $targetX = imagesx($targetImage); + $targetY = imagesy($targetImage); + $stampX = imagesx($stamp); + $stampY = imagesy($stamp); + + $marge_right = rand($stampX, $targetX - $stampX); + $marge_bottom = rand($stampY, $targetY - $stampY); + + // Copy the stamp image onto our photo using the margin offsets and the photo + // width to calculate positioning of the stamp. + imagecopy( + $targetImage, + $stamp, + $targetX - $stampX - $marge_right, + $targetY - $stampY - $marge_bottom, + 0, + 0, + $stampX, + $stampY + ); + call_user_func($functions[1], $targetImage, $targetPath); + + return $targetPath; + } + } diff --git a/code/tasks/FTPageMakerTask.php b/code/tasks/FTPageMakerTask.php index 958a87c..f7f93df 100644 --- a/code/tasks/FTPageMakerTask.php +++ b/code/tasks/FTPageMakerTask.php @@ -2,13 +2,13 @@ use DNADesign\Elemental\Models\ElementContent; use SilverStripe\Assets\File; -use SilverStripe\Assets\Image; use SilverStripe\Dev\BuildTask; use SilverStripe\Core\ClassInfo; -use DNADesign\Elemental\Models\BaseElement; use SilverStripe\ElementalBannerBlock\Block\BannerBlock; use SilverStripe\ElementalFileBlock\Block\FileBlock; - +use SilverStripe\CMS\Model\SiteTree; +use DNADesign\Elemental\Extensions\ElementalPageExtension; +use DNADesign\Elemental\Models\BaseElement; /** * Creates sample page structure, useful to test tree performance, @@ -56,6 +56,15 @@ class FTPageMakerTask extends BuildTask throw new \LogicException('withBlocks requested, but BaseElement class not found'); } + // Allow pageCountByDepth to be passed as comma-separated value, e.g. pageCounts=5,100,1,1 + $pageCounts = $request->getVar('pageCounts'); + if ($pageCounts) { + $counts = explode(',', $pageCounts); + $this->pageCountByDepth = array_map(function ($int) { + return (int) trim($int); + }, $counts); + } + $this->generatePages(0, "", 0, $withBlocks); } @@ -102,7 +111,7 @@ class FTPageMakerTask extends BuildTask foreach(range($range[0], array_rand(range($range[0], $range[1]))) as $i) { $class = array_rand($classes); $callable = $classes[$class]; - $block = call_user_func($callable); + $block = call_user_func($callable, $page); // Add block to page $page->ElementalArea()->Elements()->add($block); @@ -116,36 +125,68 @@ class FTPageMakerTask extends BuildTask } } - public static function generateContentBlock() + /** + * @param SiteTree&ElementalPageExtension|null $page + * @return ElementContent + * @throws \SilverStripe\ORM\ValidationException + */ + public static function generateContentBlock(?SiteTree $page = null) { + $count = $page ? $page->ElementalArea()->Elements()->count() : ''; + $content = $page ? "Page {$page->Title}" : "Page"; $block = new ElementContent([ - 'HTML' => 'test 123' + 'Title' => sprintf('Block #%s (Content Block)', $count), + 'ShowTitle' => rand(0,1) === 1, + 'HTML' => sprintf('Content block for %s', $content), ]); $block->write(); return $block; } - public static function generateFileBlock() + /** + * @param SiteTree&ElementalPageExtension|null $page + * @return FileBlock + * @throws \SilverStripe\ORM\ValidationException + */ + public static function generateFileBlock(?SiteTree $page = null): FileBlock { + $count = $page ? $page->ElementalArea()->Elements()->count() : ''; + // Supports both images and files $file = File::get()->shuffle()->First(); if (!$file) { throw new \LogicException('No files found to associate with FileBlock'); } - $block = new FileBlock(); + $block = new FileBlock([ + 'Title' => sprintf('Block #%s (File Block)', $count), + 'ShowTitle' => rand(0,1) === 1, + ]); $block->FileID = $file->ID; $block->write(); return $block; } - public static function generateBannerBlock() + /** + * @param SiteTree&ElementalPageExtension|null $page + * @return BannerBlock + * @throws \SilverStripe\ORM\ValidationException + */ + public static function generateBannerBlock(?SiteTree $page = null): BannerBlock { + $count = $page ? $page->ElementalArea()->Elements()->count() : ''; + $content = $page ? "Page {$page->Title}" : "Page"; + $block = new BannerBlock([ - 'Content' => 'test 123', - 'CallToActionLink' => 'http://example.com', + 'Title' => sprintf('Block #%s (Banner Block)', $count), + 'ShowTitle' => rand(0,1) === 1, + 'Content' => sprintf('Banner block for %s', $content), + 'CallToActionLink' => json_encode([ + 'PageID' => SiteTree::get()->shuffle()->first()->ID, + 'Text' => sprintf('Link for page %s', $page->Title), + ]), ]); $block->write(); diff --git a/images/silverstripe.png b/images/silverstripe.png new file mode 100644 index 0000000..16951d9 Binary files /dev/null and b/images/silverstripe.png differ