Embed meta data into generated screenshot file
authorChristian Weiske <[email protected]>
Tue, 12 Jul 2016 15:21:49 +0000 (17:21 +0200)
committerChristian Weiske <[email protected]>
Tue, 12 Jul 2016 15:21:54 +0000 (17:21 +0200)
See http://cweiske.de/tagebuch/exif-url.htm

src/phancap/Adapter/Cutycapt.php
src/phancap/Executor.php
src/phancap/MetaData.php [new file with mode: 0644]
src/phancap/Repository.php
www/setup.php

index 8d9693cbab028d8e9904d50a97617ea115551d44..6c2c41dfa1c0f25e2bcc4aceb67a710e5c1c865b 100644 (file)
@@ -115,7 +115,9 @@ class Adapter_Cutycapt
             . ' --server-num=' . $serverNumber;
         //cutycapt hangs sometimes - https://sourceforge.net/p/cutycapt/bugs/8/
         // we kill it if it does not exit itself
-        Executor::runForSomeTime($xvfbcmd . ' ' . $cmd, $maxWaitTime);
+        Executor::runForSomeTime(
+            $xvfbcmd . ' ' . $cmd, $maxWaitTime, 'cutycapt'
+        );
 
         //cutycapt does not report timeouts via exit status
         // https://sourceforge.net/p/cutycapt/bugs/11/
@@ -218,8 +220,7 @@ class Adapter_Cutycapt
             . ' -resize ' . $options->values['swidth']
             . $crop
             . ' ' . escapeshellarg($img->getPath());
-        Executor::run($convertcmd);
-        //var_dump($convertcmd);die();
+        Executor::run($convertcmd, 'convert');
         unlink($tmpPath);
     }
 
index ebe0d60ce7b87e2b77bb6a3c39d8817cd909c280..63931f898836a92c4cd234081732ea4d6cd9ba63 100644 (file)
@@ -29,18 +29,19 @@ class Executor
     /**
      * Run a shell command and check exit code.
      *
-     * @param string $cmd Full command including parameters and options
+     * @param string $cmd  Full command including parameters and options
+     * @param string $name Command name for exception
      *
      * @return void
      * @throws \Exception When the exit code is not 0
      */
-    public static function run($cmd)
+    public static function run($cmd, $name)
     {
         exec($cmd . ' 2>&1', $arOutput, $exitcode);
         if ($exitcode != 0) {
             //FIXME: do something with $arOutput
             //echo implode("\n", $arOutput) . "\n";
-            throw new \Exception('Error running cutycapt', $exitcode);
+            throw new \Exception('Error running ' . $name, $exitcode);
         }
     }
 
@@ -50,15 +51,18 @@ class Executor
      * We use the GNU coreutils "timeout" utility instead of the pcntl
      * extension since pcntl is disabled on mod_php.
      *
-     * @param string $cmd Full command including parameters and options
+     * @param string $cmd     Full command including parameters and options
+     * @param int    $seconds Number of seconds after which the cmd is killed
+     * @param string $name    Command name for exception
      *
      * @return void
      * @throws \Exception When the exit code is not 0
      */
-    public static function runForSomeTime($cmd, $seconds)
+    public static function runForSomeTime($cmd, $seconds, $name)
     {
         return static::run(
-            'timeout --signal=9 ' . $seconds . 's ' . $cmd
+            'timeout --signal=9 ' . $seconds . 's ' . $cmd,
+            $name
         );
     }
 }
diff --git a/src/phancap/MetaData.php b/src/phancap/MetaData.php
new file mode 100644 (file)
index 0000000..3cee3a3
--- /dev/null
@@ -0,0 +1,72 @@
+<?php
+/**
+ * Part of phancap
+ *
+ * PHP version 5
+ *
+ * @category  Tools
+ * @package   Phancap
+ * @author    Christian Weiske <[email protected]>
+ * @copyright 2014 Christian Weiske
+ * @license   http://www.gnu.org/licenses/agpl.html GNU AGPL v3
+ * @link      http://cweiske.de/phancap.htm
+ */
+namespace phancap;
+
+/**
+ * Embed meta data into the given file
+ *
+ * @category  Tools
+ * @package   Phancap
+ * @author    Christian Weiske <[email protected]>
+ * @copyright 2016 Christian Weiske
+ * @license   http://www.gnu.org/licenses/agpl.html GNU AGPL v3
+ * @version   Release: @package_version@
+ * @link      http://cweiske.de/phancap.htm
+ */
+class MetaData
+{
+    /**
+     * Add meta data to the given image
+     *
+     * @param Image   $img     Image object to render (contains name)
+     * @param Options $options Image rendering options
+     *
+     * @return void
+     * @throws \Exception When something goes wrong
+     */
+    public function embed(Image $img, Options $options)
+    {
+        $modes = array(
+            'screen' => 'browser window (screen)',
+            'page'   => 'full page height',
+        );
+
+        $title = "Screenshot of " . $options->values['url'];
+        $desc = "Capture options:"
+            . sprintf(
+                ' browser window: %dx%d',
+                $options->values['bwidth'],
+                $options->values['bheight']
+            )
+            . sprintf(
+                ', screenshot size: %dx%d',
+                $options->values['swidth'],
+                $options->values['sheight']
+            )
+            . ', format: ' . $options->values['sformat']
+            . ', mode: ' . $modes[$options->values['smode']];
+
+        Executor::run(
+            'exiftool'
+            . ' -XMP:title=' . escapeshellarg($title)
+            . ' -XMP:source=' . escapeshellarg($img->getUrl())
+            . ' -XMP:subject=' . escapeshellarg($options->values['url'])
+            . ' -XMP:creator=' . escapeshellarg('phancap')
+            . ' -XMP:description=' . escapeshellarg($desc)
+            . ' ' . escapeshellarg($img->getPath()),
+            'exiftool'
+        );
+    }
+}
+?>
index 9e7ad3214701ded63d3390e0052f4266c18f7aac..b5def74305330d17483b544485bcd136af6b2340 100644 (file)
@@ -111,6 +111,9 @@ class Repository
             $adapter->cleanup();
             throw $e;
         }
+
+        $meta = new MetaData();
+        $meta->embed($img, $options);
     }
 
     /**
index 7772bad12253307fc6887038334052d597d6893f..78cee18fbdab72ead3516dc55b62d16672be8725 100644 (file)
@@ -87,6 +87,11 @@ if (!function_exists('idn_to_ascii')) {
         'err', 'Function "idn_to_ascii" is not available'
     );
 }
+if (\System::which('exiftool') === false) {
+    $messages[][] = array(
+        'err', '"exiftool" is not installed'
+    );
+}
 
 $out = <<<HTM
 <!DOCTYPE html>