Skip to content

Commit

Permalink
Merge pull request #91 from unclecheese/pulls/4/unique-images
Browse files Browse the repository at this point in the history
NEW: More complete block content, unique images with watermarks
  • Loading branch information
chillu authored May 3, 2021
2 parents c91459f + 9988c9a commit e0d4fae
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 12 deletions.
75 changes: 74 additions & 1 deletion code/tasks/FTFileMakerTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
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,
Expand Down Expand Up @@ -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
*/
Expand Down Expand Up @@ -301,6 +310,11 @@ protected function generateFiles($fixtureFilePaths, $depth = 0, $prefix = "0", $
$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,
Expand Down Expand Up @@ -336,13 +350,24 @@ protected function generateFiles($fixtureFilePaths, $depth = 0, $prefix = "0", $

$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,
'Name' => $fileName,
]);
$file->File->setFromLocalFile($randomFilePath, $folder->getFilename() . $fileName);


// Randomly set old created date (for testing)
if ($doSetOldCreationDate) {
if (rand(0, 10) === 0) {
Expand Down Expand Up @@ -391,4 +416,52 @@ protected function putProtectedFilesInPublicAssetStore()
}
}

/**
* @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;
}

}
63 changes: 52 additions & 11 deletions code/tasks/FTPageMakerTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -56,6 +56,15 @@ public function run($request)
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);
}

Expand Down Expand Up @@ -102,7 +111,7 @@ protected function generateBlocksForPage(Page $page)
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);
Expand All @@ -116,36 +125,68 @@ protected function generateBlocksForPage(Page $page)
}
}

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' => '<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();

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' => '<bold>test</bold> 123',
'CallToActionLink' => 'http://example.com',
'Title' => sprintf('Block #%s (Banner Block)', $count),
'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();

Expand Down
Binary file added images/silverstripe.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit e0d4fae

Please sign in to comment.