Skip to content

Commit

Permalink
unify image size to dimension logic in dam/non dam context
Browse files Browse the repository at this point in the history
  • Loading branch information
selul committed Nov 5, 2024
1 parent bf8b18d commit 26699fe
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 96 deletions.
2 changes: 1 addition & 1 deletion inc/app_replacer.php
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ public static function add_size( $width = null, $height = null, $crop = null ) {
* @return array
* @global $wp_additional_image_sizes
*/
protected static function image_sizes() {
public static function image_sizes() {
if ( ! empty( self::$image_sizes ) ) {
return self::$image_sizes;
}
Expand Down
79 changes: 26 additions & 53 deletions inc/dam.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
*/

use Optimole\Sdk\Resource\ImageProperty\GravityProperty;
use Optimole\Sdk\Resource\ImageProperty\ResizeTypeProperty;
use Optimole\Sdk\ValueObject\Position;

/**
Expand Down Expand Up @@ -215,10 +216,9 @@ public function alter_attachment_image_src( $image, $attachment_id, $size, $icon
];
}

$metadata = wp_get_attachment_metadata( $attachment_id );
// Use the original size if the requested size is full.
if ( $size === 'full' ) {
$metadata = wp_get_attachment_metadata( $attachment_id );

$image_url = $this->replace_dam_url_args(
[
'width' => $metadata['width'],
Expand All @@ -235,45 +235,21 @@ public function alter_attachment_image_src( $image, $attachment_id, $size, $icon
false,
];
}

$crop = false;

// Size can be int [] containing width and height.
if ( is_array( $size ) ) {
$width = $size[0];
$height = $size[1];
$crop = true;
} else {
$sizes = $this->get_all_image_sizes();

if ( ! isset( $sizes[ $size ] ) ) {
return [
$image_url,
$width,
$height,
false,
];
}

$width = $sizes[ $size ]['width'];
$height = $sizes[ $size ]['height'];
$crop = (bool) $sizes[ $size ]['crop'];
}
$sizes = $this->size_to_dimension( $size, $metadata );

$image_url = $this->replace_dam_url_args(
[
'width' => $width,
'height' => $height,
'crop' => $crop,
'width' => $sizes['width'],
'height' => $sizes['height'],
'crop' => $sizes['resize'] ?? false,
],
$image_url
);

return [
$image_url,
$width,
$height,
$crop,
$sizes['width'],
$sizes['height'],
$size === 'full',
];
}

Expand Down Expand Up @@ -626,7 +602,7 @@ public function alter_elementor_image_size( $html, $settings, $image_size_key, $
return $this->replace_dam_url_args( $custom_dimensions, $html );
}

$all_sizes = $this->get_all_image_sizes();
$all_sizes = Optml_App_Replacer::image_sizes();

if ( ! isset( $all_sizes[ $image_size_key ] ) ) {
return $html;
Expand All @@ -649,19 +625,19 @@ public function alter_attachment_for_js( $response, $attachment, $meta ) {
return $response;
}

$sizes = $this->get_all_image_sizes();

$sizes = Optml_App_Replacer::image_sizes();
$meta = [];
if ( isset( $response['width'] ) ) {
$meta['width'] = $response['width'];
}
if ( isset( $response['height'] ) ) {
$meta['height'] = $response['height'];
}
foreach ( $sizes as $size => $args ) {
if ( isset( $response['sizes'][ $size ] ) ) {
continue;
}

$args = [
'height' => $args['height'],
'width' => $args['width'],
'crop' => true,
];

$args = $this->size_to_dimension( $size, $meta );
$response['sizes'][ $size ] = array_merge(
$args,
[
Expand All @@ -670,14 +646,7 @@ public function alter_attachment_for_js( $response, $attachment, $meta ) {
]
);
}

$url_args = [
'height' => $response['height'],
'width' => $response['width'],
'crop' => false,
];

$response['url'] = $this->replace_dam_url_args( $url_args, $response['url'] );
$response['url'] = $this->replace_dam_url_args( $meta, $response['url'] );

return $response;
}
Expand Down Expand Up @@ -745,7 +714,7 @@ public function replace_dam_url_args( $args, $subject ) {

$width = $args['width'];
$height = $args['height'];
$crop = (bool) $args['crop'];
$crop = $args['crop'] ?? $args['resize'] ?? false;

$gravity = Position::CENTER;

Expand All @@ -764,8 +733,12 @@ public function replace_dam_url_args( $args, $subject ) {
// Use the proper replacement for the image size.
$replacement = '/w:' . $width . '/h:' . $height;

if ( $crop ) {
if ( $crop === true ) {
$replacement .= '/g:' . $gravity . '/rt:fill';
} elseif ( is_array( $crop ) && ! empty( $crop ) ) {
$replacement .= '/' . ( new GravityProperty( $crop['gravity'] ) ) .
'/' . new ResizeTypeProperty( $crop['type'] ) .
( $crop['enlarge'] ? '/el:1' : '' );
}

$replacement .= '/q:';
Expand Down
43 changes: 1 addition & 42 deletions inc/tag_replacer.php
Original file line number Diff line number Diff line change
Expand Up @@ -459,48 +459,7 @@ public function filter_image_downsize( $image, $attachment_id, $size ) {
}

$image_meta = wp_get_attachment_metadata( $attachment_id );
$image_args = self::image_sizes();
// default size
$sizes = [
'width' => isset( $image_meta['width'] ) ? intval( $image_meta['width'] ) : false,
'height' => isset( $image_meta['height'] ) ? intval( $image_meta['height'] ) : false,
];

switch ( $size ) {
case is_array( $size ):
$width = isset( $size[0] ) ? (int) $size[0] : false;
$height = isset( $size[1] ) ? (int) $size[1] : false;
if ( ! $width || ! $height ) {
break;
}
$image_resized = image_resize_dimensions( $sizes['width'], $sizes['height'], $width, $height );
if ( $image_resized ) {
$width = $image_resized[6];
$height = $image_resized[7];
} else {
$width = $image_meta['width'];
$height = $image_meta['height'];
}
list( $sizes['width'], $sizes['height'] ) = image_constrain_size_for_editor( $width, $height, $size );

break;
case 'full' !== $size && isset( $image_args[ $size ] ):
$image_resized = image_resize_dimensions( $sizes['width'], $sizes['height'], $image_args[ $size ]['width'], $image_args[ $size ]['height'], $image_args[ $size ]['crop'] );

if ( $image_resized ) { // This could be false when the requested image size is larger than the full-size image.
$sizes['width'] = $image_resized[6];
$sizes['height'] = $image_resized[7];
}
// There are cases when the image meta is missing and image size is non existent, see SVG image handling.
if ( ! $sizes['width'] || ! $sizes['height'] ) {
break;
}
list( $sizes['width'], $sizes['height'] ) = image_constrain_size_for_editor( $sizes['width'], $sizes['height'], $size, 'display' );

$sizes['resize'] = $this->to_optml_crop( $image_args[ $size ]['crop'] );

break;
}
$sizes = $this->size_to_dimension( $size, $image_meta );
$image_url = $this->strip_image_size_from_url( $image_url );

$new_url = apply_filters( 'optml_content_url', $image_url, $sizes );
Expand Down
56 changes: 56 additions & 0 deletions inc/traits/normalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,62 @@ public function to_bound_integer( $value, $min, $max ) {
return $integer;
}

/**
* Convert image size to dimensions.
*
* This function takes an image size and its metadata, and returns the dimensions
* of the image based on the specified size. It handles different cases such as
* custom sizes, predefined sizes, and full size.
*
* @param mixed $size The size of the image. Can be an array of width and height, a predefined size, or 'full'.
* @param array $image_meta Metadata of the image, including width and height.
*
* @return array The dimensions of the image, including width, height, and optional resize parameters.
*/
public function size_to_dimension( $size, $image_meta ) {
// default size
$sizes = [
'width' => isset( $image_meta['width'] ) ? intval( $image_meta['width'] ) : false,
'height' => isset( $image_meta['height'] ) ? intval( $image_meta['height'] ) : false,
];
$image_args = self::image_sizes();
switch ( $size ) {
case is_array( $size ):
$width = isset( $size[0] ) ? (int) $size[0] : false;
$height = isset( $size[1] ) ? (int) $size[1] : false;
if ( ! $width || ! $height ) {
break;
}
$image_resized = image_resize_dimensions( $sizes['width'], $sizes['height'], $width, $height );
if ( $image_resized ) {
$width = $image_resized[6];
$height = $image_resized[7];
} else {
$width = $image_meta['width'];
$height = $image_meta['height'];
}
list( $sizes['width'], $sizes['height'] ) = image_constrain_size_for_editor( $width, $height, $size );

break;
case 'full' !== $size && isset( $image_args[ $size ] ):
$image_resized = image_resize_dimensions( $sizes['width'], $sizes['height'], $image_args[ $size ]['width'], $image_args[ $size ]['height'], $image_args[ $size ]['crop'] );

if ( $image_resized ) { // This could be false when the requested image size is larger than the full-size image.
$sizes['width'] = $image_resized[6];
$sizes['height'] = $image_resized[7];
}
// There are cases when the image meta is missing and image size is non existent, see SVG image handling.
if ( ! $sizes['width'] || ! $sizes['height'] ) {
break;
}
list( $sizes['width'], $sizes['height'] ) = image_constrain_size_for_editor( $sizes['width'], $sizes['height'], $size, 'display' );

$sizes['resize'] = $this->to_optml_crop( $image_args[ $size ]['crop'] );

break;
}
return $sizes;
}
/**
* Normalize arguments for crop.
*
Expand Down

0 comments on commit 26699fe

Please sign in to comment.