Merge pull request #91 from unclecheese/pulls/4/unique-images

NEW: More complete block content, unique images with watermarks
This commit is contained in:
Ingo Schommer 2021-05-03 13:47:17 +12:00 committed by GitHub
commit e0d4faec11
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 126 additions and 12 deletions

View File

@ -9,6 +9,8 @@ use GuzzleHttp\Client;
use GuzzleHttp\Promise; use GuzzleHttp\Promise;
use SilverStripe\Security\Member; use SilverStripe\Security\Member;
use SilverStripe\Security\Security; use SilverStripe\Security\Security;
use SilverStripe\Core\Path;
use SilverStripe\Core\Manifest\ModuleResourceLoader;
/** /**
* Creates sample folder and file structure, useful to test performance, * Creates sample folder and file structure, useful to test performance,
@ -106,7 +108,14 @@ class FTFileMakerTask extends BuildTask
private static $depth = 2; 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[] * @var int[]
* @config * @config
*/ */
@ -301,6 +310,11 @@ class FTFileMakerTask extends BuildTask
$doSetOldCreationDate = (bool) self::config()->get('doSetOldCreationDate'); $doSetOldCreationDate = (bool) self::config()->get('doSetOldCreationDate');
$doRandomlyPublish = (bool) self::config()->get('doRandomlyPublish'); $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++) { for ($i = 1; $i <= $folderCount; $i++) {
$folder = new Folder([ $folder = new Folder([
'ParentID' => $parentID, 'ParentID' => $parentID,
@ -336,6 +350,16 @@ class FTFileMakerTask extends BuildTask
$class = $this->fixtureFileTypes[$randomFileName]; $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([ $file = new $class([
'ParentID' => $folder->ID, 'ParentID' => $folder->ID,
'Title' => $fileName, 'Title' => $fileName,
@ -343,6 +367,7 @@ class FTFileMakerTask extends BuildTask
]); ]);
$file->File->setFromLocalFile($randomFilePath, $folder->getFilename() . $fileName); $file->File->setFromLocalFile($randomFilePath, $folder->getFilename() . $fileName);
// Randomly set old created date (for testing) // Randomly set old created date (for testing)
if ($doSetOldCreationDate) { if ($doSetOldCreationDate) {
if (rand(0, 10) === 0) { 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;
}
} }

View File

@ -2,13 +2,13 @@
use DNADesign\Elemental\Models\ElementContent; use DNADesign\Elemental\Models\ElementContent;
use SilverStripe\Assets\File; use SilverStripe\Assets\File;
use SilverStripe\Assets\Image;
use SilverStripe\Dev\BuildTask; use SilverStripe\Dev\BuildTask;
use SilverStripe\Core\ClassInfo; use SilverStripe\Core\ClassInfo;
use DNADesign\Elemental\Models\BaseElement;
use SilverStripe\ElementalBannerBlock\Block\BannerBlock; use SilverStripe\ElementalBannerBlock\Block\BannerBlock;
use SilverStripe\ElementalFileBlock\Block\FileBlock; 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, * 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'); 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); $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) { foreach(range($range[0], array_rand(range($range[0], $range[1]))) as $i) {
$class = array_rand($classes); $class = array_rand($classes);
$callable = $classes[$class]; $callable = $classes[$class];
$block = call_user_func($callable); $block = call_user_func($callable, $page);
// Add block to page // Add block to page
$page->ElementalArea()->Elements()->add($block); $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([ $block = new ElementContent([
'HTML' => '<bold>test</bold> 123' 'Title' => sprintf('Block #%s (Content Block)', $count),
'ShowTitle' => rand(0,1) === 1,
'HTML' => sprintf('Content block for <bold>%s</bold>', $content),
]); ]);
$block->write(); $block->write();
return $block; 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 // Supports both images and files
$file = File::get()->shuffle()->First(); $file = File::get()->shuffle()->First();
if (!$file) { if (!$file) {
throw new \LogicException('No files found to associate with FileBlock'); 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->FileID = $file->ID;
$block->write(); $block->write();
return $block; 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([ $block = new BannerBlock([
'Content' => '<bold>test</bold> 123', 'Title' => sprintf('Block #%s (Banner Block)', $count),
'CallToActionLink' => 'http://example.com', 'ShowTitle' => rand(0,1) === 1,
'Content' => sprintf('Banner block for <bold>%s</bold>', $content),
'CallToActionLink' => json_encode([
'PageID' => SiteTree::get()->shuffle()->first()->ID,
'Text' => sprintf('Link for page %s', $page->Title),
]),
]); ]);
$block->write(); $block->write();

BIN
images/silverstripe.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 11 KiB