Ejemplo n.º 1
0
 /**
  * @param $file
  * @param $commad_prefix
  *
  * @throws Exception
  */
 private function _optimize_png($file, $commad_prefix)
 {
     //pngquant lossy compresssion
     if ($this->mode == self::MODE_LOSSY) {
         $pngquant = self::_find_tool('pngquant');
         $min_quality = 50;
         $max_quality = max($min_quality, $this->lossy_quality);
         $levels = array(self::LEVEL_FAST => 10, self::LEVEL_NORMAL => 3, self::LEVEL_HIGH => 1, self::LEVEL_EXTREME => 1);
         //compress using a temp file because pngquant dont allow direct writing of input file
         $i = 0;
         do {
             $tmp = sys_get_temp_dir() . '/pngquant_' . $i . '.png';
             $i++;
         } while (file_exists($tmp));
         copy($file, $tmp);
         $this->_exec("{$commad_prefix}{$pngquant} --speed {$levels[$this->level]} --quality={$min_quality}-{$max_quality} --ext _pq.png :file", array(':file' => $tmp));
         $out = preg_replace('/\\.png$/', '_pq.png', $tmp);
         if (file_exists($out)) {
             if (!$this->check_output || Tinyfier_Image_Tool::is_valid_image($out)) {
                 rename($out, $file);
             } else {
                 if ($this->verbose) {
                     echo "<h5>Ignored invalid image '{$out}' generated by pngquant</h5>";
                 }
                 unlink($out);
             }
         }
         if (file_exists($tmp)) {
             unlink($tmp);
         }
     }
     if ($this->check_lossless_output) {
         $original_image = new Tinyfier_Image_Tool($file);
     }
     //Optipng Lossless compression
     $optipng = self::_find_tool('optipng');
     $metadata_copy = $this->remove_metadata ? '-strip all' : '';
     $levels = array(self::LEVEL_FAST => 2, self::LEVEL_NORMAL => 3, self::LEVEL_HIGH => 4, self::LEVEL_EXTREME => 6);
     $this->_exec("{$commad_prefix}{$optipng} -o{$levels[$this->level]} -quiet {$metadata_copy} :file", array(':file' => $file));
     //Pngout Lossless compression
     $pngout = self::_find_tool('pngout');
     $levels = array(self::LEVEL_FAST => 3, self::LEVEL_NORMAL => 2, self::LEVEL_HIGH => 1, self::LEVEL_EXTREME => 0);
     $this->_exec("{$commad_prefix}{$pngout} -s{$levels[$this->level]} -q :file", array(':file' => $file));
     if (isset($original_image) && !Tinyfier_Image_Tool::equal($original_image, $file)) {
         throw new Exception('Lossless compression output was different to original');
     }
     return true;
 }
Ejemplo n.º 2
0
 /**
  * Apply a filter on the selected image
  * @see http://php.net/manual/function.imagefilter.php
  */
 public function lib_resize($arg)
 {
     //Get parameters
     $url = $this->_remove_quotes(trim($arg[2][0][2][0]));
     $width = $this->_remove_quotes(trim($arg[2][1][1]));
     $width_unit = trim($arg[2][1][2]);
     $height = isset($arg[2][2]) ? $this->_remove_quotes(trim($arg[2][2][1])) : null;
     $height_unit = isset($arg[2][2]) ? trim($arg[2][2][2]) : 'px';
     $keep_aspect = isset($arg[2][3]) ? $this->_remove_quotes(trim($arg[2][3][1])) : true;
     if (strtolower($keep_aspect) == 'false') {
         $keep_aspect = false;
     }
     //Find local file
     $local_path = $this->_local_path($url);
     //Apply filter
     $image = new Tinyfier_Image_Tool($local_path);
     if ($width_unit == '%') {
         $width = round($image->width() * ($width / 100.0));
     }
     if ($height_unit == '%') {
         $height = round($image->height() * ($height / 100.0));
     }
     if ($width == $image->width() && $height == $image->height()) {
         return array('string', '', array("url('{$url}')"));
     }
     $image->resize($width, $height, $keep_aspect);
     //Save image and generate CSS
     $format = in_array($image->format(), array('gif', 'png', 'jpg')) ? $image->format() : 'png';
     $path = $this->_get_cache_path('resize', $format);
     $image->save($path, $this->_settings['lossy_quality'], $this->_settings['optimize_images']);
     return array('string', '', array("url('{$this->_get_cache_url($path)}')"));
 }