<?php
    
/* ImageMagick composite method tester, build 2
        * (C) krzysiu.net (Krzysztof Blachnicki) 2016
        * You are free to use this tool either as: 
        * 1) LGPL 3.0 (https://opensource.org/licenses/LGPL-3.0)
        * 2) CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/)
        * 3) It's fine with me if you'd ignore licenses and just keep attribution 
    */
    
    /* SETTINGS */
    
$config = [
    
'watermarkImage' => 'water.png'// (str) name of the watermark image, must be format supporting transparency
    
'bgImage' => 'bg.jpg'// (str) name of the background image, any format
    
'outputDirectory' => 'temp'// (str) directory to output files (will be created if doesn't exist)
    
'hWaterCount' => 3// (int) watermark count horizontally 
    
'vWaterCount' => 2// (int) watermark count vertically 
    
'saveToHtml' => 'result.html'// (str/bool) if string given, the result will be additionaly saved as static HTML file; if false, it won't be saved
    
'displayInfo' => true // (bool) whether to display additional info (IM versions etc.)
    
];
    
/* END OF SETTINGS */
    
    // Get all available composite methods
    
$methodList = (new ReflectionClass('IMagick'))->getConstants();
    
$methodList array_filter($methodList, function ($str) { return !strncmp('COMPOSITE'$str9); }, ARRAY_FILTER_USE_KEY);
    
    
    
$waterim = new IMagick();
    
$waterim->readImage($config['watermarkImage']);
    
$waterSize $waterim->getImageGeometry();
    
    
$im = new IMagick();
    
$im->readImage($config['bgImage']);    
    
$im->setImageResolution(72,72); // set 72 dpi for weird browsers
    
    // Generate array of watermarks positions
    
$pos = [];
    for (
$x 0$x <= $config['hWaterCount']-1$x++) for ($y 0$y <= $config['vWaterCount']-1$y++) $pos[] = [$x $waterSize['width'], $y $waterSize['height']];
    unset(
$x$y$waterSize);
    
    if (!(
file_exists($config['outputDirectory']) && is_dir($config['outputDirectory']))) mkdir($config['outputDirectory']); // attemp to create output directory, if doesn't exist
    
    
$html '<!doctype html><html lang="en"><head><title>IMagick Composite Generator output</title><meta name="generator" content="IMagick Composite Generator by krzysiu.net"></head><body>';
    
    
// Generate debug info
    
if ($config['displayInfo'] === true) {
        
$imVersion Imagick::getVersion()['versionString'];
        
preg_match('/[a-zA-Z]+ (.*?) /'$imVersion$versionParseResult);
        
$versionParseResult $versionParseResult $versionParseResult[1] : $imVersion// if can't be parsed, output raw version
        
$moreInfo = [
        [
'title' => 'ImageMagick version''value' => $versionParseResult],
        [
'title' => 'IMagick version''value' => phpversion('IMagick')],
        [
'title' => 'PHP version''value' => phpversion()],
        [
'title' => 'Composite method count''value' => (string)count($methodList)],
        [
'title' => 'Input files and output''value' => sprintf('<em>%s</em> / <em>%s</em> => <em>%s</em>'$config['watermarkImage'], $config['bgImage'], $config['outputDirectory'])],
        [
'title' => 'Generated on''value' => sprintf('<time datetime="%s">%s</time>'date('c'), date('j M Y H:i:s (U)'))]        
        ];
        
$html .= '<ul style="font-size:80%">';
        foreach (
$moreInfo as $info$html .= vsprintf('<li><strong>%s</strong>: %s'$info);
        
$html .= '</ul><hr>';
        unset(
$moreInfo$imVersion$versionParseResult$info);
    }
    
    foreach (
$methodList as $method) {
        
$imClone = clone $im// cloning object is critical, as we need to put watermarks on the original image!
        
$methodName array_search($method$methodList);
        
$outname sprintf('%s/test_%s.jpg'$config['outputDirectory'], $methodName);
        
        foreach (
$pos as $ps$imClone->compositeImage($waterim$method$ps[0], $ps[1], imagick::CHANNEL_DEFAULT);
        
        
$imClone->setImageCompression(Imagick::COMPRESSION_JPEG);
        
$imClone->writeImage($outname);
        
$html .= sprintf('<h2>%s</h2><div><img src="%s"></div><hr>'$methodName$outname);
    }    
    
    
$html .= '</body></html>';
    if (
$config['saveToHtml'] !== falsefile_put_contents($config['saveToHtml'], $html);
    echo 
$html;