Skip to content

Commit

Permalink
Merge pull request #57 from humanmade/fix-reported-image-size
Browse files Browse the repository at this point in the history
Fix reported size dimensions in downsize filter
  • Loading branch information
roborourke authored Feb 20, 2020
2 parents 485ea71 + 81fe468 commit 0ace90d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 13 deletions.
33 changes: 20 additions & 13 deletions inc/class-tachyon.php
Original file line number Diff line number Diff line change
Expand Up @@ -548,7 +548,7 @@ public function filter_image_downsize( $image, $attachment_id, $size ) {
$image_meta = image_get_intermediate_size( $attachment_id, $size );

// 'full' is a special case: We need consistent data regardless of the requested size.
if ( 'full' == $size ) {
if ( 'full' === $size ) {
$image_meta = $full_size_meta;
} elseif ( ! $image_meta ) {
// If we still don't have any image meta at this point, it's probably from a custom thumbnail size
Expand All @@ -566,40 +566,47 @@ public function filter_image_downsize( $image, $attachment_id, $size ) {
$is_intermediate = true;
}

// Expose determined arguments to a filter before passing to Tachyon
$transform = $image_args['crop'] ? 'resize' : 'fit';

// If we can't get the width from the image size args, use the width of the
// image metadata. We only do this is image_args['width'] is not set, because
// we don't want to lose this data. $image_args is used as the Tachyon URL param
// args, so we want to keep the original image sizes args. For example, if the image
// size is 300x300px, non-cropped, we want to pass `fit=300,300` to Tachyon, instead
// of say `resize=300,225`, because semantically, the image size is registered as
// 300x300 un-cropped, not 300x225 cropped.
if ( empty( $image_args['width'] ) ) {
if ( empty( $image_args['width'] ) && $transform !== 'resize' ) {
$image_args['width'] = isset( $image_meta['width'] ) ? $image_meta['width'] : 0;
}

if ( empty( $image_args['height'] ) ) {
if ( empty( $image_args['height'] ) && $transform !== 'resize' ) {
$image_args['height'] = isset( $image_meta['height'] ) ? $image_meta['height'] : 0;
}

list( $image_args['width'], $image_args['height'] ) = image_constrain_size_for_editor( $image_args['width'], $image_args['height'], $size, 'display' );
// Prevent upscaling.
$image_args['width'] = min( (int) $image_args['width'], (int) $full_size_meta['width'] );
$image_args['height'] = min( (int) $image_args['height'], (int) $full_size_meta['height'] );

// Expose determined arguments to a filter before passing to Tachyon
$transform = $image_args['crop'] ? 'resize' : 'fit';
// Respect $content_width settings.
list( $width, $height ) = image_constrain_size_for_editor( $image_meta['width'], $image_meta['height'], $size, 'display' );

// Check specified image dimensions and account for possible zero values; tachyon fails to resize if a dimension is zero.
if ( 0 == $image_args['width'] || 0 == $image_args['height'] ) {
if ( ( 0 == $image_args['width'] || 0 == $image_args['height'] ) && $transform !== 'fit' ) {
if ( 0 == $image_args['width'] && 0 < $image_args['height'] ) {
$tachyon_args['h'] = $image_args['height'];
} elseif ( 0 == $image_args['height'] && 0 < $image_args['width'] ) {
$tachyon_args['w'] = $image_args['width'];
}
} else {
if ( 'resize' === $transform || ! $image_meta ) {
$image_meta = $full_size_meta;
// Fit accepts a zero value for either dimension so we allow that.
// If resizing:
// Both width & height are required, image args should be exact dimensions.
if ( $transform === 'resize' ) {
$image_args['width'] = $image_args['width'] ?: $width;
$image_args['height'] = $image_args['height'] ?: $height;
}

$image_args['width'] = min( (int) $image_args['width'], (int) $image_meta['width'] );
$image_args['height'] = min( (int) $image_args['height'], (int) $image_meta['height'] );
$is_intermediate = ( $image_args['width'] < $full_size_meta['width'] || $image_args['height'] < $full_size_meta['height'] );

// Add transform args if size is intermediate.
Expand Down Expand Up @@ -642,8 +649,8 @@ public function filter_image_downsize( $image, $attachment_id, $size ) {
// Generate Tachyon URL.
$image = array(
tachyon_url( $image_url, $tachyon_args ),
! empty( $image_args['width'] ) ? $image_args['width'] : false,
! empty( $image_args['height'] ) ? $image_args['height'] : false,
$width,
$height,
$is_intermediate,
);
} elseif ( is_array( $size ) ) {
Expand Down
2 changes: 2 additions & 0 deletions tests/tests/class-resizing.php
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ function data_filtered_url() {
'large',
[
'http://tachy.on/u/tachyon.jpg?fit=1024,575',
'http://tachy.on/u/tachyon.jpg?fit=1024,719',
'http://tachy.on/u/tachyon.jpg?resize=1024,575',
'http://tachy.on/u/tachyon.jpg?fit=1024,1024',
'http://tachy.on/u/tachyon.jpg?w=1024&h=575',
Expand Down Expand Up @@ -371,6 +372,7 @@ function data_filtered_url() {
'tachyon-large',
'oversize2d-early',
[
'http://tachy.on/u/tachyon-large-scaled.jpg?fit=2500,1440',
'http://tachy.on/u/tachyon-large-scaled.jpg?fit=2500,1406',
'http://tachy.on/u/tachyon-large-scaled.jpg?resize=2500,1406',
'http://tachy.on/u/tachyon-large-scaled.jpg?fit=2500,1500',
Expand Down

0 comments on commit 0ace90d

Please sign in to comment.