Skip to content

Commit ba6c7cf

Browse files
Add replace original with scaled
Text review Review fixes Move to Media settings
1 parent 34dee90 commit ba6c7cf

4 files changed

Lines changed: 88 additions & 1 deletion

File tree

src/base.cls.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,7 @@ class Base extends Root {
218218
const O_MEDIA_VPI = 'media-vpi';
219219
const O_MEDIA_VPI_CRON = 'media-vpi_cron';
220220
const O_IMG_OPTM_JPG_QUALITY = 'img_optm-jpg_quality';
221+
const O_MEDIA_REP_W_SCALED = 'media-replace_w_scaled';
221222

222223
// -------------------------------------------------- ##
223224
// -------------- Image Optm ----------------- ##
@@ -500,6 +501,7 @@ class Base extends Root {
500501
self::O_MEDIA_LQIP_EXC => array(),
501502
self::O_MEDIA_VPI => false,
502503
self::O_MEDIA_VPI_CRON => false,
504+
self::O_MEDIA_REP_W_SCALED => false,
503505

504506
// Image Optm
505507
self::O_IMG_OPTM_AUTO => false,

src/lang.cls.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ public static function title( $id ) {
202202
self::O_MEDIA_ADD_MISSING_SIZES => __('Add Missing Sizes', 'litespeed-cache'),
203203
self::O_MEDIA_VPI => __('Viewport Images', 'litespeed-cache'),
204204
self::O_MEDIA_VPI_CRON => __('Viewport Images Cron', 'litespeed-cache'),
205+
self::O_MEDIA_REP_W_SCALED => __('Replace Original with Scaled', 'litespeed-cache'),
205206

206207
self::O_IMG_OPTM_AUTO => __('Auto Request Cron', 'litespeed-cache'),
207208
self::O_IMG_OPTM_ORI => __('Optimize Original Images', 'litespeed-cache'),

src/media.cls.php

Lines changed: 50 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_MEDIA_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,48 @@ 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+
// Test if create and image was resized.
119+
if( "create" === $context && isset($metadata['original_image']) && isset($metadata['file']) && false !== strstr($metadata['file'], '-scaled')){
120+
// Get rescaled file name.
121+
$path_exploded = explode( '/', strrev($metadata['file']), 2 );
122+
$rescaled_file_name = strrev($path_exploded[0]);
123+
124+
// Create paths for images: resized and original.
125+
$base_path = $this->_wp_upload_dir['basedir'] . $this->_wp_upload_dir['subdir'] . '/';
126+
$rescaled_path = $base_path . $rescaled_file_name;
127+
$new_path = $base_path . $metadata['original_image'];
128+
129+
if( file_exists( $rescaled_path ) && file_exists( $rescaled_path ) ){
130+
// Move rescaled to original.
131+
rename( $rescaled_path, $new_path );
132+
133+
// Change array file key.
134+
$metadata['file'] = $this->_wp_upload_dir['subdir'] . '/' . $metadata['original_image'];
135+
if( 0 === strpos( $metadata['file'], '/' ) ){
136+
$metadata['file'] = substr( $metadata['file'], 1 );
137+
}
138+
139+
// Delete array "original_image" key.
140+
unset($metadata['original_image']);
141+
142+
// Update meta "_wp_attached_file".
143+
update_post_meta( $attachment_id, '_wp_attached_file', $metadata['file'] );
144+
}
145+
}
146+
147+
return $metadata;
148+
}
149+
101150
/**
102151
* Add featured image to head
103152
*/

tpl/page_optm/settings_media.tpl.php

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

1919
$lqip_queue = $this->load_queue( 'lqip' );
2020

21+
$scaled_size = apply_filters( 'big_image_size_threshold', 2560 ) . 'px';
22+
2123
?>
2224

2325
<h3 class="litespeed-title-short">
@@ -274,5 +276,38 @@
274276
</div>
275277
</td>
276278
</tr>
279+
280+
<tr>
281+
<th>
282+
<?php $option_id = Base::O_MEDIA_REP_W_SCALED; ?>
283+
<?php $this->title( $option_id ); ?>
284+
</th>
285+
<td>
286+
<?php $this->build_switch( $option_id ); ?>
287+
<div class="litespeed-desc">
288+
<?php esc_html_e( 'Automatically replace large images with scaled versions.', 'litespeed-cache' ); ?>
289+
<br />
290+
<?php echo wp_kses_post( sprintf( __( 'Scaled size and threshold is: %s', 'litespeed-cache' ), '<code>' . $scaled_size . '</code>' ) ); ?>
291+
<br />
292+
<span class="litespeed-success">
293+
<?php
294+
printf(
295+
esc_html__( 'API: Filter %s available to change threshold.', 'litespeed-cache' ),
296+
'<code>big_image_size_threshold</code>'
297+
);
298+
?>
299+
<a href="https://developer.wordpress.org/reference/hooks/big_image_size_threshold/" target="_blank" class="litespeed-learn-more">
300+
<?php esc_html_e('Learn More', 'litespeed-cache'); ?>
301+
</a>
302+
</span>
303+
304+
<br />
305+
<font class="litespeed-danger">
306+
🚨
307+
<?php esc_html_e( 'This is irreversible.', 'litespeed-cache' ); ?>
308+
</font>
309+
</div>
310+
</td>
311+
</tr>
277312
</tbody>
278313
</table>

0 commit comments

Comments
 (0)