Skip to content

Instantly share code, notes, and snippets.

@CaelanStewart
Last active November 24, 2016 16:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CaelanStewart/3ff32d9ac3d8b4f0b11e4ac8443a2b00 to your computer and use it in GitHub Desktop.
Save CaelanStewart/3ff32d9ac3d8b4f0b11e4ac8443a2b00 to your computer and use it in GitHub Desktop.
AutoWatermark - Automatically add watermarks to images using ProcessWire
<?php
/**
* Automatically adds a watermark to uploaded images.
*
* by PROGRESS: A Creative Agency | https://www.progress.agency/
*
* @author Caelan Stewart <caelan@progress.agency>
*/
namespace ProcessWire;
/**
* AutoWatermark.module - Automatically applies watermarks to images uploaded to pages with template 'album'
*/
class AutoWatermark extends WireData implements Module {
private $moduleDir = __DIR__ . DIRECTORY_SEPARATOR;
private $watermarkSuffix = '___watermark';
private $noDebug = true;
private $ignoreNextAfterPageSaveHook = false;
static public function getModuleInfo() {
return [
// The module'ss title, typically a little more descriptive than the class name
'title' => 'AutoWatermark',
// version number
'version' => 100,
// summary is brief description of what this module is
'summary' => 'Automatically watermarks images uploaded to pages with a particular template.',
'author' => 'Caelan Stewart / PROGRESS: A Creative Agency',
// Optional URL to more information about the module
'href' => 'https://www.progress.agency/',
// singular=true: indicates that only one instance of the module is allowed.
// This is usually what you want for modules that attach hooks.
'singular' => true,
// autoload=true: indicates the module should be started with ProcessWire.
// This is necessary for any modules that attach runtime hooks, otherwise those
// hooks won't get attached unless some other code calls the module on it's own.
// Note that autoload modules are almost always also 'singular' (seen above).
'autoload' => true,
// Optional font-awesome icon name, minus the 'fa-' part
'icon' => 'picture-o'
];
}
public function init() {
$this->pages->addHookAfter('save', $this, 'afterPageSave');
}
/**
* Gets the watermark image from the site meta page.
*
* @returns Pageimage|null
*/
public function getWatermarkImage() {
$siteMetaPage = $this->pages->get('template=site-meta');
foreach($siteMetaPage->site_meta as $item)
if($item->key === 'watermark_logo')
return $item->{
['text', 'textarea', 'image', 'file', 'pw_page'][$item->data_type->id - 1]
};
return null;
}
/**
* Runs after a page has saved.
*
* @param HookEvent $event
*/
public function afterPageSave(HookEvent $event) {
if($this->ignoreNextAfterPageSaveHook) {
return;
}
$page = $event->arguments[0];
if($page->template->name === 'album' && $page->album_photos->count) {
$watermarkImage = $this->getWatermarkImage();
$watermarkImage = $watermarkImage ? $watermarkImage->first : null;
if(!$watermarkImage) {
$this->error('In order to watermark the image, please upload a watermark logo to the Site Meta page, then come back here and save the page again.');
return;
}
$count = $this->applyWatermark($page, $watermarkImage);
if($count) {
$plural = $count === 1 ? 'image' : 'images';
$this->message("Applied watermark to $count $plural");
}
}
}
/**
* @param Page $page - The page to apply water marks to
* @param Pageimage $watermark
* @return int
*/
public function applyWatermark(Page $page, Pageimage $watermark) {
$page->of(false);
$watermarksAppliedCount = 0;
foreach($page->album_photos as $album_photo) {
$images = $album_photo->images;
$watermarkedImages = [ ];
foreach($images as $image) {
$filePath = $image->filename;
$dirPath = dirname($filePath);
$fileName = basename($filePath);
$fileExt = substr($fileName, (int) strrpos($fileName, '.'));
$fileNameNoExt = substr($fileName, 0, strlen($fileName) - strlen($fileExt));
// Determine if file is already watermarked
$watermarked = substr($fileNameNoExt, (int) strrpos($fileNameNoExt, $this->watermarkSuffix)) === $this->watermarkSuffix;
$this->noDebug || var_dump($filePath, $fileName, $dirPath, $fileExt, $fileNameNoExt, $watermarked);
if(!$watermarked) {
$this->noDebug || var_dump('Applying watermark');
$watermarkedFileName = "{$fileNameNoExt}{$this->watermarkSuffix}{$fileExt}";
$watermarkedDirPath = "$dirPath";
$watermarkedFilePath = "$watermarkedDirPath/$watermarkedFileName";
$this->noDebug || var_dump($watermarkedFileName, $watermarkedDirPath, $watermarkedFilePath);
// Apply watermark
$pimModule = $this->modules->getModule('PageImageManipulator02');
$pim = $pimModule->imLoad($filePath);
$pim->watermarkLogo($watermark, 'center', 10)->pimSave();
// Duplicate file with $this->watermark
copy($filePath, $watermarkedFilePath);
$watermarkedImages[$watermarkedFileName] = $image;
++$watermarksAppliedCount;
} else {
$this->noDebug || var_dump('Watermark already applied');
}
}
foreach($watermarkedImages as $path => $image) {
$images->add($path);
$images->remove($image);
}
$album_photo->save();
}
// Ignore next event to avoid infinite look
$this->ignoreNextAfterPageSaveHook = true;
$page->save();
$this->ignoreNextAfterPageSaveHook = false;
return $watermarksAppliedCount;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment