Pngcrush.php
Go to the documentation of this file.
1<?php
2
4
5class Pngcrush extends Optimizer
6{
7 public function __construct($options = array())
8 {
9 parent::__construct($options);
10
11 if (isset($this->options['pngcrush'])) {
12 $this->executable = $this->options['pngcrush'];
13 }
14
15 if (is_null($this->executable)) {
16 $this->executable = \Depage\Graphics\Graphics::which("pngcrush");
17 }
18 }
19
20 public function optimize($filename)
21 {
22 if (!$this->executable) {
23 return false;
24 }
25
26 $tmpfile = tempnam(sys_get_temp_dir(), 'pngcrush');
27
28 $this->command = "{$this->executable} ";
29
30 // remove all metadata
31 $this->command .= "-rem gAMA ";
32 $this->command .= "-rem cHRM ";
33 $this->command .= "-rem iCCP ";
34 $this->command .= "-rem sRGB ";
35
36 // blacken transparent areas
37 $this->command .= "-blacken ";
38
39 // add file arguments
40 $this->command .= " " . escapeshellarg($filename);
41 $this->command .= " " . escapeshellarg($tmpfile);
42
43 $success = $this->execCommand();
44
45 if ($success && filesize($filename) > filesize($tmpfile)) {
46 unlink($filename);
47 rename($tmpfile, $filename);
48 } else {
49 unlink($tmpfile);
50 }
51
52 return $success;
53 }
54}
55
56/* vim:set ft=php sw=4 sts=4 fdm=marker et : */