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); }
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)); }
public function testUpdateWpMetadataShouldUpdateWithResizedOriginal() { $tiny_meta = new Tiny_Metadata(150, $this->json("wp_meta_sizes_with_same_files")); $wp_metadata = array('width' => 2000, 'height' => 1000); $tiny_meta->add_request(); $tiny_meta->add_response(array('output' => array('width' => 200, 'height' => 100))); $this->assertEquals(array('width' => 200, 'height' => 100), $tiny_meta->update_wp_metadata($wp_metadata)); }