Skip to content

Commit 5191fc9

Browse files
Add replace original with scaled
Text review
1 parent 34dee90 commit 5191fc9

5 files changed

Lines changed: 102 additions & 1 deletion

File tree

src/base.cls.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,7 @@ class Base extends Root {
226226
const O_IMG_OPTM_CRON = 'img_optm-cron'; // @Deprecated since v7.0 TODO: remove after v7.5
227227
const O_IMG_OPTM_ORI = 'img_optm-ori';
228228
const O_IMG_OPTM_RM_BKUP = 'img_optm-rm_bkup';
229+
const O_IMG_OPTM_REP_W_SCALED = 'img_optm-replace_w_scaled';
229230
const O_IMG_OPTM_WEBP = 'img_optm-webp';
230231
const O_IMG_OPTM_LOSSLESS = 'img_optm-lossless';
231232
const O_IMG_OPTM_EXIF = 'img_optm-exif';
@@ -505,6 +506,7 @@ class Base extends Root {
505506
self::O_IMG_OPTM_AUTO => false,
506507
self::O_IMG_OPTM_ORI => false,
507508
self::O_IMG_OPTM_RM_BKUP => false,
509+
self::O_IMG_OPTM_REP_W_SCALED => false,
508510
self::O_IMG_OPTM_WEBP => false,
509511
self::O_IMG_OPTM_LOSSLESS => false,
510512
self::O_IMG_OPTM_EXIF => false,

src/img-optm.cls.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2192,4 +2192,47 @@ public function handler() {
21922192

21932193
Admin::redirect();
21942194
}
2195+
2196+
/**
2197+
* Replace original with scaled image.
2198+
*
2199+
* @since 7.4
2200+
* @param array $meta_value Current meta array.
2201+
* @param int $post_id Attachment ID.
2202+
* @param string $context Can be "create" or "update".
2203+
* @return array $meta_value
2204+
* @access public
2205+
*/
2206+
public function replace_original_with_scaled( $meta_value, $post_id, $context ) {
2207+
// Test if create and image was resized.
2208+
if( "create" === $context && isset($meta_value['original_image']) && isset($meta_value['file']) && false !== strstr($meta_value['file'], '-scaled')){
2209+
$media_path = wp_upload_dir();
2210+
// Get rescaled file name.
2211+
$path_exploded = explode( DIRECTORY_SEPARATOR, strrev($meta_value['file']), 2 );
2212+
$rescaled_file_name = strrev($path_exploded[0]);
2213+
2214+
// Create paths for images: resized and original.
2215+
$rescaled_path = $media_path['basedir'] . $media_path['subdir'] . DIRECTORY_SEPARATOR . $rescaled_file_name;
2216+
$new_path = $media_path['basedir'] . $media_path['subdir'] . DIRECTORY_SEPARATOR . $meta_value['original_image'];
2217+
2218+
if( is_file( $rescaled_path ) && is_file( $rescaled_path ) ){
2219+
// Move rescaled to original.
2220+
rename( $rescaled_path, $new_path );
2221+
2222+
// Change array file key.
2223+
$meta_value['file'] = $media_path['subdir'] . DIRECTORY_SEPARATOR . $meta_value['original_image'];
2224+
if( 0 === strpos( $meta_value['file'], DIRECTORY_SEPARATOR ) ){
2225+
$meta_value['file'] = substr( $meta_value['file'], 1 );
2226+
}
2227+
2228+
// Delete array "original_image" key.
2229+
unset($meta_value['original_image']);
2230+
2231+
// Update meta "_wp_attached_file".
2232+
update_post_meta( $post_id, '_wp_attached_file', $meta_value['file'] );
2233+
}
2234+
}
2235+
2236+
return $meta_value;
2237+
}
21952238
}

src/lang.cls.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,7 @@ public static function title( $id ) {
206206
self::O_IMG_OPTM_AUTO => __('Auto Request Cron', 'litespeed-cache'),
207207
self::O_IMG_OPTM_ORI => __('Optimize Original Images', 'litespeed-cache'),
208208
self::O_IMG_OPTM_RM_BKUP => __('Remove Original Backups', 'litespeed-cache'),
209+
self::O_IMG_OPTM_REP_W_SCALED => __('Replace Original with Scaled', 'litespeed-cache'),
209210
self::O_IMG_OPTM_WEBP => __('Next-Gen Image Format', 'litespeed-cache'),
210211
self::O_IMG_OPTM_LOSSLESS => __('Optimize Losslessly', 'litespeed-cache'),
211212
self::O_IMG_OPTM_EXIF => __('Preserve EXIF/XMP data', 'litespeed-cache'),

src/media.cls.php

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,18 @@ public function __construct() {
5252
/**
5353
* Hooks after user init
5454
*
55-
* @since 7.2
55+
* @since 7.2
56+
* @since 7.4 Add media replace original with scaled.
5657
*/
5758
public function after_user_init() {
5859
// Hook to attachment delete action (PR#844, Issue#841) for AJAX del compatibility
5960
add_action('delete_attachment', array( $this, 'delete_attachment' ), 11, 2);
61+
62+
// For big images, allow to replace original with scaled image.
63+
if( $this->conf(Base::O_IMG_OPTM_REP_W_SCALED) ){
64+
// Added priority 9 to happen before other functions added.
65+
add_filter('wp_generate_attachment_metadata', array( $this, 'replace_original_with_scaled' ), 9, 3);
66+
}
6067
}
6168

6269
/**
@@ -98,6 +105,19 @@ public function init() {
98105
add_filter('litespeed_optm_html_head', array( $this, 'finalize_head' ));
99106
}
100107

108+
/**
109+
* Handle attachment create
110+
*
111+
* @param array $metadata Current meta array.
112+
* @param int $attachment_id Attachment ID.
113+
* @param string $context Can be "create" or "update".
114+
* @return array $metadata
115+
* @since 7.4
116+
*/
117+
public function replace_original_with_scaled( $metadata, $attachment_id, $context ) {
118+
return $this->cls('Img_Optm')->replace_original_with_scaled( $metadata, $attachment_id, $context );
119+
}
120+
101121
/**
102122
* Add featured image to head
103123
*/

tpl/img_optm/settings.tpl.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212

1313
defined( 'WPINC' ) || exit;
1414

15+
$scaled_size = apply_filters( 'big_image_size_threshold', 2560 ) . 'px';
16+
1517
$this->form_action();
1618
?>
1719

@@ -69,6 +71,39 @@
6971
</td>
7072
</tr>
7173

74+
<tr>
75+
<th>
76+
<?php $option_id = Base::O_IMG_OPTM_REP_W_SCALED; ?>
77+
<?php $this->title( $option_id ); ?>
78+
</th>
79+
<td>
80+
<?php $this->build_switch( $option_id ); ?>
81+
<div class="litespeed-desc">
82+
<?php esc_html_e( 'Automatically replace large images with scaled versions.', 'litespeed-cache' ); ?>
83+
<br />
84+
<?php echo wp_kses_post( __( 'Scaled size and threshold is: %s.', 'litespeed-cache' ), '<code>' . $scaled_size . '</code>'); ?>
85+
<br />
86+
<span class="litespeed-success">
87+
<?php
88+
printf(
89+
esc_html__( 'API: Filter %s available to change threshold.', 'litespeed-cache' ),
90+
'<code>big_image_size_threshold</code>'
91+
);
92+
?>
93+
<a href="https://developer.wordpress.org/reference/hooks/big_image_size_threshold/" target="_blank" class="litespeed-learn-more">
94+
<?php esc_html_e('Learn More', 'litespeed-cache'); ?>
95+
</a>
96+
</span>
97+
98+
<br />
99+
<font class="litespeed-danger">
100+
🚨
101+
<?php esc_html_e( 'This is irreversible.', 'litespeed-cache' ); ?>
102+
</font>
103+
</div>
104+
</td>
105+
</tr>
106+
72107
<tr>
73108
<th>
74109
<?php $option_id = Base::O_IMG_OPTM_LOSSLESS; ?>

0 commit comments

Comments
 (0)