Skip to content

Commit 3201822

Browse files
authored
Merge branch 'add/image-optimization' into add/avatar-caching
2 parents 7ab2e4d + 3cd94fd commit 3201822

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

includes/class-attachments.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,7 @@ private static function save_attachment( $attachment_data, $post_id, $author_id
474474
}
475475

476476
// Optimize images before sideloading (resize and convert to WebP).
477-
$optimized = self::optimize_image( $tmp_file );
477+
$optimized = self::optimize_image( $tmp_file, self::MAX_IMAGE_DIMENSION );
478478
if ( $optimized['changed'] ) {
479479
$tmp_file = $optimized['path'];
480480
}
@@ -586,7 +586,7 @@ private static function save_file( $attachment_data, $object_id, $object_type )
586586
}
587587

588588
// Optimize images (resize and convert to WebP).
589-
$optimized = self::optimize_image( $file_path );
589+
$optimized = self::optimize_image( $file_path, self::MAX_IMAGE_DIMENSION );
590590
if ( $optimized['changed'] ) {
591591
$file_path = $optimized['path'];
592592
$file_name = \basename( $file_path );
@@ -609,11 +609,12 @@ private static function save_file( $attachment_data, $object_id, $object_type )
609609
* Uses WordPress image editor to resize large images and convert them
610610
* to WebP format for better compression while maintaining quality.
611611
*
612-
* @param string $file_path Path to the image file.
612+
* @param string $file_path Path to the image file.
613+
* @param int $max_dimension Maximum width/height in pixels.
613614
*
614615
* @return array{path: string, changed: bool}|\WP_Error The optimized file path and whether it changed, or WP_Error.
615616
*/
616-
private static function optimize_image( $file_path ) {
617+
private static function optimize_image( $file_path, $max_dimension ) {
617618
// Check if it's an image.
618619
$mime_type = \wp_check_filetype( $file_path )['type'] ?? '';
619620
if ( ! $mime_type || ! \str_starts_with( $mime_type, 'image/' ) ) {
@@ -639,9 +640,8 @@ private static function optimize_image( $file_path ) {
639640
);
640641
}
641642

642-
$size = $editor->get_size();
643-
$max_dimension = self::MAX_IMAGE_DIMENSION;
644-
$needs_resize = $size['width'] > $max_dimension || $size['height'] > $max_dimension;
643+
$size = $editor->get_size();
644+
$needs_resize = $size['width'] > $max_dimension || $size['height'] > $max_dimension;
645645

646646
// Resize if needed.
647647
if ( $needs_resize ) {

0 commit comments

Comments
 (0)