private function compress($metadata, $attachment_id)
 {
     $mime_type = get_post_mime_type($attachment_id);
     $tiny_metadata = new Tiny_Metadata($attachment_id, $metadata);
     if ($this->settings->get_compressor() === null || strpos($mime_type, 'image/') !== 0) {
         return $metadata;
     }
     $success = 0;
     $failed = 0;
     $compressor = $this->settings->get_compressor();
     $sizes = $this->settings->get_tinify_sizes();
     $missing = $tiny_metadata->get_missing_sizes($sizes);
     foreach ($missing as $size) {
         try {
             $tiny_metadata->add_request($size);
             $tiny_metadata->update();
             $response = $compressor->compress_file($tiny_metadata->get_filename($size));
             $tiny_metadata->add_response($response, $size);
             $success++;
         } catch (Tiny_Exception $e) {
             $tiny_metadata->add_exception($e, $size);
             $failed++;
         }
     }
     $tiny_metadata->update();
     return array($tiny_metadata, array('success' => $success, 'failed' => $failed));
 }
示例#2
0
 public function testCompressShouldNotCompressTwice()
 {
     $this->wp->stub('get_post_mime_type', create_function('$i', 'return "image/png";'));
     $meta = new Tiny_Metadata(1);
     $meta->add_response(compressTestFile('test.png'));
     $meta->add_response(compressTestFile('test-large.png'), 'large');
     $meta->update();
     $this->compressor->expects($this->once())->method('compress_file')->withConsecutive(array($this->equalTo('/root/wp-content/upload/14/01/test-post-thumbnail.png')))->will($this->returnCallback('compressTestFile'));
     $this->subject->compress_attachment(getTestMetadata(), 1);
 }
示例#3
0
 public function testCompressWhenFileChanged()
 {
     $this->wp->stub('get_post_mime_type', create_function('$i', 'return "image/png";'));
     $testmeta = $this->wp->getTestMetadata();
     $meta = new Tiny_Metadata(1, $testmeta);
     $meta->add_request();
     $meta->add_response(self::successCompress('vfs://root/wp-content/uploads/14/01/test.png'));
     $meta->add_request('large');
     $meta->add_response(self::successCompress('vfs://root/wp-content/uploads/14/01/test-large.png'), 'large');
     $meta->add_request('post-thumbnail');
     $meta->add_response(self::successCompress('vfs://root/wp-content/uploads/14/01/test-post-thumbnail.png'), 'post-thumbnail');
     $meta->update();
     $this->vfs->getChild('wp-content/uploads/14/01/test-large.png')->truncate(100000);
     $this->compressor->expects($this->once())->method('compress_file')->withConsecutive(array($this->equalTo('vfs://root/wp-content/uploads/14/01/test-large.png')))->will($this->returnCallback(array($this, 'successCompress')));
     $this->subject->compress_attachment($testmeta, 1);
 }
示例#4
0
 public function compress_attachment($metadata, $attachment_id)
 {
     $mime_type = get_post_mime_type($attachment_id);
     $tiny_metadata = new Tiny_Metadata($attachment_id);
     if ($this->compressor === null || strpos($mime_type, 'image/') !== 0) {
         return $metadata;
     }
     $path_info = pathinfo($metadata['file']);
     $upload_dir = wp_upload_dir();
     $prefix = $upload_dir['basedir'] . '/' . $path_info['dirname'] . '/';
     if (!$tiny_metadata->is_compressed()) {
         try {
             $response = $this->compressor->compress_file("{$prefix}{$path_info['basename']}");
             $tiny_metadata->add_response($response);
         } catch (Tiny_Exception $e) {
             $tiny_metadata->add_exception($e);
         }
     }
     $settings = $this->settings->get_sizes();
     foreach ($metadata['sizes'] as $size => $info) {
         if (isset($settings[$size]) && $settings[$size]['tinify'] && !$tiny_metadata->is_compressed($size)) {
             try {
                 $response = $this->compressor->compress_file("{$prefix}{$info['file']}");
                 $tiny_metadata->add_response($response, $size);
             } catch (Tiny_Exception $e) {
                 $tiny_metadata->add_exception($e, $size);
             }
         }
     }
     $tiny_metadata->update();
     return $metadata;
 }