Skip to content

Commit de84bf1

Browse files
Always update metadata with filesize and dimensions after resulting file is downloaded.
1 parent 27ae6e7 commit de84bf1

6 files changed

Lines changed: 48 additions & 5 deletions

File tree

src/class-tiny-compress.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,9 @@ public function compress_file($file, $resize_options, $preserve_options) {
8585
list($output, $details) = $this->compress(file_get_contents($file), $resize_options, $preserve_options);
8686
file_put_contents($file, $output);
8787

88+
$details['output'] = self::update_details($file, $details) + $details['output'];
8889
if ($resize_options) {
89-
$details['output'] = self::update_details($file, $details) + $details['output'];
90+
$details['output']['resized'] = true;
9091
}
9192

9293
return $details;
@@ -139,8 +140,7 @@ protected static function update_details($file, $details) {
139140
'size' => $size,
140141
'width' => $width,
141142
'height' => $height,
142-
'ratio' => round($size / $details['input']['size'], 4),
143-
'resized' => true
143+
'ratio' => round($size / $details['input']['size'], 4)
144144
);
145145
}
146146
}

test/fixtures/input-copyright.jpg

108 KB
Loading

test/integration/CompressIntegrationTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,4 +163,13 @@ public function testResizeDisabledShouldDisplayOriginalDimensionsInEditScreen()
163163
$this->assertContains('Dimensions: 1080 × 720',
164164
self::$driver->findElement(WebDriverBy::cssSelector('div.misc-pub-dimensions'))->getText());
165165
}
166+
167+
public function testPreserveCopyrightShouldDisplayCorrectImageSizeInMediaLibrary()
168+
{
169+
$this->set_api_key('PRESERVEJPG123');
170+
$this->enable_preserve_copyright();
171+
$this->upload_image(dirname(__FILE__) . '/../fixtures/input-copyright.jpg');
172+
$this->assertNotContains('files modified after compression',
173+
self::$driver->findElement(WebDriverBy::cssSelector('div#tinify-compress-details'))->getText());
174+
}
166175
}

test/integration/IntegrationTestCase.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,18 @@ protected function disable_resize() {
9494
self::$driver->findElement(WebDriverBy::tagName('form'))->submit();
9595
}
9696

97+
protected function enable_preserve_copyright() {
98+
$url = wordpress('/wp-admin/options-media.php');
99+
if (self::$driver->getCurrentUrl() != $url) {
100+
self::$driver->get($url);
101+
}
102+
$element = self::$driver->findElement(WebDriverBy::id('tinypng_preserve_data_copyright'));
103+
if (!$element->getAttribute('checked')) {
104+
$element->click();
105+
}
106+
self::$driver->findElement(WebDriverBy::tagName('form'))->submit();
107+
}
108+
97109
protected function view_edit_image($image_title = 'input-example') {
98110
$url = wordpress('/wp-admin/upload.php');
99111
if (self::$driver->getCurrentUrl() != $url) {
95.6 KB
Loading

test/mock-tinypng-webservice/shrink.php

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,24 @@ function mock_jpg_response() {
2828
header("Compression-Count: {$session['Compression-Count']}");
2929

3030
$response = array(
31-
"input" => array("size" => 15391, "type" => "image/jpg"),
32-
"output" => array("size" => 13910, "type" => "image/jpg", "ratio" => 0.904)
31+
"input" => array("size" => 15391, "type" => "image/jpeg"),
32+
"output" => array("size" => 13910, "type" => "image/jpeg", "ratio" => 0.904)
33+
);
34+
return json_encode($response);
35+
}
36+
37+
function mock_preserve_jpg_copyright_response() {
38+
global $session;
39+
40+
$session['Compression-Count'] += 1;
41+
header('HTTP/1.1 201 Created');
42+
header("Location: http://webservice/output/copyright.jpg");
43+
header("Content-Type: application/json; charset=utf-8");
44+
header("Compression-Count: {$session['Compression-Count']}");
45+
46+
$response = array(
47+
"input" => array("size" => 110329, "type" => "image/jpeg"),
48+
"output" => array("size" => 97835, "type" => "image/jpeg", "ratio" => 0.8868)
3349
);
3450
return json_encode($response);
3551
}
@@ -86,6 +102,12 @@ function mock_invalid_json_response() {
86102
} else {
87103
echo mock_jpg_response();
88104
}
105+
} else if ($api_key == 'PRESERVEJPG123') {
106+
if (intval($_SERVER['CONTENT_LENGTH']) == 0) {
107+
echo mock_empty_response();
108+
} else {
109+
echo mock_preserve_jpg_copyright_response();
110+
}
89111
} else if ($api_key == 'JSON1234') {
90112
if (intval($_SERVER['CONTENT_LENGTH']) == 0) {
91113
echo mock_empty_response();

0 commit comments

Comments
 (0)