Skip to content

Commit

Permalink
Merge pull request #58 from mindkomm/1.x-fix-resize
Browse files Browse the repository at this point in the history
  • Loading branch information
gchtr committed Apr 4, 2023
2 parents f80ab98 + d43666f commit c238af2
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 30 deletions.
49 changes: 31 additions & 18 deletions lib/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,15 +167,13 @@ public function full_src() {
* @return string|null
*/
public function auto_full_src() : ?string {
if ( 'full' === $this->size_key || $this->is_svg() ) {
// Deliberately return the attachment URL, which can be a 'scaled'
// version of an image.
return wp_get_attachment_url( $this->id );
} elseif ( 'original' === $this->size_key ) {
if ( 'original' === $this->size_key ) {
return Helper::get_original_attachment_url( $this->id );
}

return null;
// Deliberately return the attachment URL, which can be a 'scaled'
// version of an image.
return wp_get_attachment_url( $this->id );
}

/**
Expand Down Expand Up @@ -213,15 +211,22 @@ public function src( $args = [] ) {
'max_height' => $this->max_height(),
] );

// Resize the image for that size.
$src = Timmy::resize(
$this->size,
$this->full_src(),
$width,
$height,
$this->resize_crop,
$this->resize_force
);
if ( $this->upscale['allow']
|| ( $this->max_width() !== $width )
|| ( 0 === $width && $height !== $this->max_height() )
) {
// Resize the image for that size.
$src = Timmy::resize(
$this->size,
$this->full_src(),
$width,
$height,
$this->resize_crop,
$this->resize_force
);
} else {
$src = $this->auto_full_src();
}

if ( $args['webp'] ) {
$src = Timmy::to_webp( $src, $this->size );
Expand Down Expand Up @@ -421,7 +426,7 @@ public function srcset( $args = [] ) {
list(
$width_intermediate,
$height_intermediate
) = Helper::get_dimensions_for_srcset_size( $this->size['resize'], $srcset_src );
) = Helper::get_dimensions_for_srcset_size( $this->size['resize'], $srcset_src );

$max_width = $this->max_width();

Expand Down Expand Up @@ -532,13 +537,17 @@ public function sizes() {
}

public function max_width() {
if ( ! empty( $this->max_width ) ) {
return $this->max_width;
}

if ( $this->is_svg() ) {
$dimensions = $this->svg_dimensions();

if ( $dimensions['width'] > 0 ) {
$this->max_width = round( $dimensions['width'] );
}
} elseif ( empty( $this->max_width ) ) {
} else {
$this->load_attachment_meta_data();

if ( ! empty( $this->meta['width'] ) ) {
Expand All @@ -550,13 +559,17 @@ public function max_width() {
}

public function max_height() {
if ( ! empty( $this->max_height ) ) {
return $this->max_height;
}

if ( $this->is_svg() ) {
$dimensions = $this->svg_dimensions();

if ( $dimensions['height'] > 0 ) {
$this->max_height = round( $dimensions['height'] );
}
} elseif ( empty( $this->max_height ) ) {
} else {
$this->load_attachment_meta_data();

if ( ! empty( $this->meta['height'] ) ) {
Expand Down
50 changes: 42 additions & 8 deletions tests/test-functions.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

use Timmy\Timmy;

class TestFunctions extends TimmyUnitTestCase {
public function test_get_timber_image_src() {
$attachment = $this->create_image();
Expand All @@ -10,17 +12,27 @@ public function test_get_timber_image_src() {
$this->assertEquals( $image, $result );
}

public function test_get_timber_image_src_small_image() {
$attachment = $this->create_image( [ 'file' => 'test-200px.jpg' ] );

$image = Timmy::get_image( $attachment, 'large' );
$result = $image->src();

$image = $this->get_upload_url() . '/test-200px.jpg';

$this->assertEquals( $image, $result );
}

public function test_get_timber_image_src_without_metadata() {
$attachment = $this->create_image();

// Remove attachment metadata.
wp_update_attachment_metadata( $attachment->ID, [] );

$result = get_timber_image_src( $attachment, 'large' );
$result = get_timber_image_src( $attachment, 'large' );
$expected = $this->get_upload_url() . '/test-1400x0-c-default.jpg';

$image = $this->get_upload_url() . '/test-1400x0-c-default.jpg';

$this->assertEquals( $image, $result );
$this->assertEquals( $expected, $result );
}

public function test_get_timber_image_src_non_image() {
Expand Down Expand Up @@ -276,6 +288,28 @@ public function test_get_timber_image_full_with_gif() {
$this->assertEquals( $image, $result );
}

public function test_get_timber_image_full_with_gif_apiv1() {
$attachment = $this->create_image( [ 'file' => 'logo-small.gif' ] );

$image = Timmy::get_image( $attachment, 'full' );
$result = $image->src();

$expected = $this->get_upload_url() . '/logo-small.gif';

$this->assertEquals( $expected, $result );
}

public function test_get_timber_image_large_with_gif() {
$attachment = $this->create_image( [ 'file' => 'logo-small.gif' ] );

$image = Timmy::get_image( $attachment, 'large' );
$result = $image->src();

$expected = $this->get_upload_url() . '/logo-small.gif';

$this->assertEquals( $expected, $result );
}

public function test_get_timber_image_full_with_gif_without_metadata() {
$attachment = $this->create_image( [ 'file' => 'logo-small.gif' ] );

Expand Down Expand Up @@ -332,7 +366,7 @@ public function test_get_timber_image_srcset_x_descriptors() {
public function test_get_timber_image_width() {
$attachment = $this->create_image();

$image = Timmy\Timmy::get_image( $attachment, 'large' );
$image = Timmy::get_image( $attachment, 'large' );
$result = $image->width();

$this->assertEquals( 1400, $result );
Expand All @@ -344,7 +378,7 @@ public function test_get_timber_image_width_without_metadata() {
// Remove attachment metadata.
wp_update_attachment_metadata( $attachment->ID, [] );

$image = Timmy\Timmy::get_image( $attachment, 'large' );
$image = Timmy::get_image( $attachment, 'large' );
$result = $image->width();

$this->assertEquals( 1400, $result );
Expand All @@ -353,7 +387,7 @@ public function test_get_timber_image_width_without_metadata() {
public function test_get_timber_image_height() {
$attachment = $this->create_image();

$image = Timmy\Timmy::get_image( $attachment, 'large' );
$image = Timmy::get_image( $attachment, 'large' );
$result = $image->height();

$this->assertEquals( 933, $result );
Expand All @@ -365,7 +399,7 @@ public function test_get_timber_image_height_without_metadata() {
// Remove attachment metadata.
wp_update_attachment_metadata( $attachment->ID, [] );

$image = Timmy\Timmy::get_image( $attachment, 'large' );
$image = Timmy::get_image( $attachment, 'large' );
$result = $image->height();

// Can’t calculate a height.
Expand Down
8 changes: 4 additions & 4 deletions tests/test-webp.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public function test_picture_webp_with_small_image() {
$result = get_timber_picture_responsive( $attachment, 'picture-webp-with-small-image' );

$expected = sprintf(
'<source type="image/webp" srcset="%1$s/test-200px-200x0-c-default.webp">%2$s<source type="image/jpeg" srcset="%1$s/test-200px-200x0-c-default.jpg">%2$s<img src="%1$s/test-200px-200x0-c-default.jpg" width="200" height="133" alt="" loading="lazy">',
'<source type="image/webp" srcset="%1$s/test-200px.webp">%2$s<source type="image/jpeg" srcset="%1$s/test-200px.jpg">%2$s<img src="%1$s/test-200px.jpg" width="200" height="133" alt="" loading="lazy">',
$this->get_upload_url(),
PHP_EOL
);
Expand Down Expand Up @@ -171,9 +171,9 @@ public function test_timmy_ignores_gif_when_using_webp() {

$result = $image->picture_responsive();
$expected = sprintf(
'<source srcset="%1$s/logo-small-100x0-c-default.gif">
<img src="http://example.org/wp-content/uploads/2023/04/logo-small-100x0-c-default.gif" width="100" height="50" alt="" loading="lazy">',
$this->get_upload_url()
'<source srcset="%1$s/logo-small.gif">%2$s<img src="http://example.org/wp-content/uploads/2023/04/logo-small.gif" width="100" height="50" alt="" loading="lazy">',
$this->get_upload_url(),
PHP_EOL
);

$this->assertSame( $expected, $result );
Expand Down

0 comments on commit c238af2

Please sign in to comment.