Skip to content

Commit eb1afa9

Browse files
committed
Refactor image dimension handling in Optimole
- Removed the `wp_image_src_get_dimensions` filter from `Optml_Dam` class. - Added a new `filter_image_src_get_dimensions` method in `Optml_Tag_Replacer` class to handle image dimensions based on optimized URLs. - Updated `size_to_dimension` method in `Optml_Normalizer` trait to accept an optional `attachment_id` parameter for improved image size retrieval.
1 parent 0b317f8 commit eb1afa9

File tree

3 files changed

+47
-11
lines changed

3 files changed

+47
-11
lines changed

inc/dam.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ public function __construct() {
6565
add_filter( 'wp_get_attachment_metadata', [ $this, 'alter_attachment_metadata' ], 10, 2 );
6666
add_filter( 'image_downsize', [ $this, 'catch_downsize' ], 10, 3 );
6767
add_filter( 'wp_prepare_attachment_for_js', [ $this, 'alter_attachment_for_js' ], 10, 3 );
68-
add_filter( 'wp_image_src_get_dimensions', [ $this, 'alter_img_tag_w_h' ], 10, 4 );
6968
add_filter( 'get_attached_file', [ $this, 'alter_attached_file_response' ], 10, 2 );
7069
add_filter( 'wp_calculate_image_srcset', [ $this, 'disable_dam_images_srcset' ], 1, 5 );
7170

inc/tag_replacer.php

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,46 @@ public function init() {
5858
add_filter( 'image_downsize', [ $this, 'filter_image_downsize' ], PHP_INT_MAX, 3 );
5959
add_filter( 'wp_calculate_image_srcset', [ $this, 'filter_srcset_attr' ], PHP_INT_MAX - 1, 5 );
6060
add_filter( 'wp_calculate_image_sizes', [ $this, 'filter_sizes_attr' ], 1, 2 );
61+
add_filter( 'wp_image_src_get_dimensions', [ $this, 'filter_image_src_get_dimensions' ], 99, 4 );
6162
if ( $this->settings->get( 'retina_images' ) === 'enabled' ) {
6263
add_filter( 'wp_get_attachment_image_attributes', [ $this, 'filter_attachment_image_attributes' ], 99, 3 );
6364
}
6465
}
66+
67+
/**
68+
* Get the dimensions of the image based on the optimized url fro Optimole.
69+
*
70+
* @param mixed $dimensions The dimensions of the image.
71+
* @param mixed $image_src The source of the image.
72+
* @param mixed $image_meta The meta of the image.
73+
* @param mixed $attachment_id The ID of the attachment.
74+
*/
75+
public function filter_image_src_get_dimensions( $dimensions, $image_src, $image_meta, $attachment_id ) {
76+
77+
$incoming_size = $this->parse_dimension_from_optimized_url( $image_src );
78+
list($width, $height) = $incoming_size;
79+
80+
$sizes = Optml_App_Replacer::image_sizes();
81+
82+
// If this is an image size. Return its dimensions.
83+
foreach ( $sizes as $size => $args ) {
84+
if ( (int) $args['width'] !== (int) $width ) {
85+
continue;
86+
}
87+
88+
if ( (int) $args['height'] !== (int) $height ) {
89+
continue;
90+
}
91+
92+
return [
93+
$args['width'],
94+
$args['height'],
95+
];
96+
}
97+
98+
// Fall-through with the original dimensions.
99+
return $dimensions;
100+
}
65101
/**
66102
* Filter the attachment image attributes to add the srcset attribute with retina support.
67103
*
@@ -511,7 +547,8 @@ public function filter_image_downsize( $image, $attachment_id, $size ) {
511547
}
512548

513549
$image_meta = wp_get_attachment_metadata( $attachment_id );
514-
$sizes = $this->size_to_dimension( $size, $image_meta );
550+
$sizes = $this->size_to_dimension( $size, $image_meta, $attachment_id );
551+
515552
$image_url = $this->strip_image_size_from_url( $image_url );
516553

517554
$new_url = apply_filters( 'optml_content_url', $image_url, $sizes );

inc/traits/normalizer.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -195,10 +195,11 @@ public function to_bound_integer( $value, $min, $max ) {
195195
*
196196
* @param mixed $size The size of the image. Can be an array of width and height, a predefined size, or 'full'.
197197
* @param array $image_meta Metadata of the image, including width and height.
198+
* @param int $attachment_id The ID of the attachment.
198199
*
199200
* @return array The dimensions of the image, including width, height, and optional resize parameters.
200201
*/
201-
public function size_to_dimension( $size, $image_meta ) {
202+
public function size_to_dimension( $size, $image_meta, $attachment_id = null ) {
202203
// default size
203204
$sizes = [
204205
'width' => isset( $image_meta['width'] ) ? intval( $image_meta['width'] ) : false,
@@ -216,16 +217,15 @@ public function size_to_dimension( $size, $image_meta ) {
216217
if ( isset( self::$dimension_cache[ $cache_key ] ) ) {
217218
return self::$dimension_cache[ $cache_key ];
218219
}
219-
$image_resized = image_resize_dimensions( $sizes['width'], $sizes['height'], $width, $height );
220-
if ( $image_resized ) {
221-
$width = $image_resized[6];
222-
$height = $image_resized[7];
223-
} else {
224-
$width = $image_meta['width'];
225-
$height = $image_meta['height'];
220+
if ( $attachment_id ) {
221+
$intermediate = image_get_intermediate_size( $attachment_id, $size );
222+
if ( $intermediate ) {
223+
$sizes['width'] = $intermediate['width'];
224+
$sizes['height'] = $intermediate['height'];
225+
}
226226
}
227-
list( $sizes['width'], $sizes['height'] ) = image_constrain_size_for_editor( $width, $height, $size );
228227

228+
list( $sizes['width'], $sizes['height'] ) = image_constrain_size_for_editor( $sizes['width'], $sizes['height'], $size );
229229
self::$dimension_cache[ $cache_key ] = $sizes;
230230
break;
231231
case 'full' !== $size && isset( $image_args[ $size ] ):

0 commit comments

Comments
 (0)