Skip to content

Commit

Permalink
Merge pull request #850 from Codeinwp/fix/resize
Browse files Browse the repository at this point in the history
Fix/resize
  • Loading branch information
selul authored Nov 5, 2024
2 parents bf8b18d + ed150d2 commit b20eb1a
Show file tree
Hide file tree
Showing 8 changed files with 128 additions and 191 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
82 changes: 29 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,18 +625,20 @@ 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 +648,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 @@ -707,7 +678,7 @@ public function alter_img_tag_w_h( $dimensions, $image_src, $image_meta, $attach
$width = $incoming_size[0];
$height = $incoming_size[1];

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

// If this is an image size. Return its dimensions.
foreach ( $sizes as $size => $args ) {
Expand Down Expand Up @@ -741,11 +712,11 @@ public function alter_img_tag_w_h( $dimensions, $image_src, $image_meta, $attach
* @return string
*/
public function replace_dam_url_args( $args, $subject ) {
$args = wp_parse_args( $args, [ 'width' => 'auto', 'height' => 'auto', 'crop' => false, 'dam' => true ] );
$args = wp_parse_args( $args, [ 'width' => 'auto', 'height' => 'auto', 'dam' => true ] );

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

$gravity = Position::CENTER;

Expand All @@ -764,8 +735,13 @@ 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
80 changes: 17 additions & 63 deletions inc/media_offload.php
Original file line number Diff line number Diff line change
Expand Up @@ -2221,7 +2221,6 @@ public function alter_attachment_image_src( $image, $attachment_id, $size, $icon

$url = get_post( $attachment_id );
$url = $url->guid;
$image_url = $this->get_new_offloaded_attachment_url( $url, $attachment_id );
$metadata = wp_get_attachment_metadata( $attachment_id );

// Use the original size if the requested size is full.
Expand All @@ -2244,65 +2243,26 @@ public function alter_attachment_image_src( $image, $attachment_id, $size, $icon
];
}

$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,
$metadata['width'],
$metadata['height'],
false,
];
}

$width = $sizes[ $size ]['width'];
$height = $sizes[ $size ]['height'];
$crop = is_array( $sizes[ $size ]['crop'] ) ? $sizes[ $size ]['crop'] : (bool) $sizes[ $size ]['crop'];
}

$sizes2crop = self::size_to_crop();

if ( wp_attachment_is( 'video', $attachment_id ) && doing_action( 'wp_insert_post_data' ) ) {
return $image;
}

$resize = apply_filters( 'optml_default_crop', [] );
$data = image_get_intermediate_size( $attachment_id, $size );

if ( is_array( $data ) && isset( $data['width'] ) && isset( $data['height'] ) ) { // @phpstan-ignore-line - these both exist.
if ( isset( $sizes2crop[ $data['width'] . $data['height'] ] ) ) {
$resize = $this->to_optml_crop( $sizes2crop[ $data['width'] . $data['height'] ] );
}
}

if ( $crop !== false ) {
$resize = $this->to_optml_crop( $crop );
}

$sizes = $this->size_to_dimension( $size, $metadata );
$image_url = $this->get_new_offloaded_attachment_url(
$url,
$attachment_id,
[
'width' => $width,
'height' => $height,
'resize' => $resize,
'width' => $sizes['width'],
'height' => $sizes['height'],
'resize' => $sizes['resize'] ?? [],
'attachment_id' => $attachment_id,
]
);

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

Expand All @@ -2320,19 +2280,20 @@ public function alter_attachment_for_js( $response, $attachment, $meta ) {
return $response;
}

$sizes = $this->get_all_image_sizes();
$meta = [];
if ( isset( $response['width'] ) ) {
$meta['width'] = $response['width'];
}
if ( isset( $response['height'] ) ) {
$meta['height'] = $response['height'];
}
$sizes = Optml_App_Replacer::image_sizes();

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 @@ -2341,14 +2302,7 @@ public function alter_attachment_for_js( $response, $attachment, $meta ) {
]
);
}

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

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

return $response;
}
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
21 changes: 6 additions & 15 deletions inc/traits/dam_offload_utils.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php

trait Optml_Dam_Offload_Utils {
use Optml_Normalizer;

/**
* Checks that the attachment is a DAM image.
*
Expand Down Expand Up @@ -157,7 +159,6 @@ private function is_attachment_edit_page( $attachment_id ) {
* @return mixed
*/
private function get_altered_metadata_for_remote_images( $metadata, $id ) {
$sizes = $this->get_all_image_sizes();

$post = get_post( $id );

Expand All @@ -174,24 +175,14 @@ private function get_altered_metadata_for_remote_images( $metadata, $id ) {
if ( ! isset( $metadata['height'] ) || ! isset( $metadata['width'] ) ) {
return $metadata;
}

$sizes = Optml_App_Replacer::image_sizes();
foreach ( $sizes as $size => $args ) {
// check if the image is portrait or landscape using attachment metadata.
$is_portrait = $metadata['height'] > $metadata['width'];

// proportionally set the width/height based on this if image is uncropped.
if ( ! (bool) $args['crop'] ) {
if ( $is_portrait ) {
$args['width'] = (int) ( $args['height'] * round( $metadata['width'] / $metadata['height'] ) );
} else {
$args['height'] = (int) ( $args['width'] * round( $metadata['height'] / $metadata['width'] ) );
}
}

$dimensions = $this->size_to_dimension( $size, $metadata );
$sizes_meta[ $size ] = [
'file' => $metadata['file'],
'width' => $args['width'],
'height' => $args['height'],
'width' => $dimensions['width'],
'height' => $dimensions['height'],
'mime-type' => $post->post_mime_type,
];
}
Expand Down
Loading

0 comments on commit b20eb1a

Please sign in to comment.