Skip to content

Commit

Permalink
conversion implemented
Browse files Browse the repository at this point in the history
still needs further testing and unit tests
  • Loading branch information
nosilver4u committed Aug 31, 2024
1 parent 03a8bc9 commit c1d94b9
Show file tree
Hide file tree
Showing 12 changed files with 357 additions and 271 deletions.
21 changes: 4 additions & 17 deletions aux-optimize.php
Original file line number Diff line number Diff line change
Expand Up @@ -1611,22 +1611,9 @@ function ewww_image_optimizer_image_scan( $dir, $started = 0 ) {
if ( ewww_image_optimizer_stl_check() ) {
set_time_limit( 0 );
}
$enabled_types = array();
if ( ewww_image_optimizer_get_option( 'ewww_image_optimizer_jpg_level' ) ) {
$enabled_types[] = 'image/jpeg';
}
if ( ewww_image_optimizer_get_option( 'ewww_image_optimizer_png_level' ) ) {
$enabled_types[] = 'image/png';
}
if ( ewww_image_optimizer_get_option( 'ewww_image_optimizer_gif_level' ) ) {
$enabled_types[] = 'image/gif';
}
if ( ewww_image_optimizer_get_option( 'ewww_image_optimizer_pdf_level' ) ) {
$enabled_types[] = 'application/pdf';
}
if ( ewww_image_optimizer_get_option( 'ewww_image_optimizer_svg_level' ) ) {
$enabled_types[] = 'image/svg+xml';
}

$supported_types = ewwwio()->get_supported_types();

foreach ( $iterator as $file ) {
if ( get_transient( 'ewww_image_optimizer_aux_iterator' ) && get_transient( 'ewww_image_optimizer_aux_iterator' ) > $file_counter ) {
continue;
Expand Down Expand Up @@ -1714,7 +1701,7 @@ function ewww_image_optimizer_image_scan( $dir, $started = 0 ) {
} else {
$mime = ewww_image_optimizer_quick_mimetype( $path );
}
if ( ! in_array( $mime, $enabled_types, true ) ) {
if ( ! in_array( $mime, $supported_types, true ) ) {
continue;
}
if ( apply_filters( 'ewww_image_optimizer_bypass', false, $path ) === true ) {
Expand Down
30 changes: 13 additions & 17 deletions bulk.php
Original file line number Diff line number Diff line change
Expand Up @@ -1385,22 +1385,7 @@ function ewww_image_optimizer_media_scan( $hook = '' ) {

$disabled_sizes = ewww_image_optimizer_get_option( 'ewww_image_optimizer_disable_resizes_opt', false, true );

$enabled_types = array();
if ( ewww_image_optimizer_get_option( 'ewww_image_optimizer_jpg_level' ) ) {
$enabled_types[] = 'image/jpeg';
}
if ( ewww_image_optimizer_get_option( 'ewww_image_optimizer_png_level' ) ) {
$enabled_types[] = 'image/png';
}
if ( ewww_image_optimizer_get_option( 'ewww_image_optimizer_gif_level' ) ) {
$enabled_types[] = 'image/gif';
}
if ( ewww_image_optimizer_get_option( 'ewww_image_optimizer_pdf_level' ) ) {
$enabled_types[] = 'application/pdf';
}
if ( ewww_image_optimizer_get_option( 'ewww_image_optimizer_svg_level' ) ) {
$enabled_types[] = 'image/svg+xml';
}
$supported_types = ewwwio()->get_supported_types();

ewww_image_optimizer_debug_log();
$starting_memory_usage = memory_get_usage( true );
Expand Down Expand Up @@ -1496,7 +1481,7 @@ function ewww_image_optimizer_media_scan( $hook = '' ) {
ewwwio_debug_message( "missing mime for $selected_id" );
}

if ( ! in_array( $mime, $enabled_types, true ) && empty( ewwwio()->webp_only ) ) {
if ( ! in_array( $mime, $supported_types, true ) && empty( ewwwio()->webp_only ) ) {
$skipped_ids[] = $selected_id;
continue;
}
Expand Down Expand Up @@ -2136,6 +2121,17 @@ function ewww_image_optimizer_bulk_counter_measures( $image, $error_counter = 0
ewww_image_optimizer_bulk_skip_image( $image );
}
}
if ( 'image/bmp' === ewww_image_optimizer_quick_mimetype( $image->file ) ) {
if ( empty( $previous_countermeasures['bmp2png'] ) && ! defined( 'EWWW_IMAGE_OPTIMIZER_BMP_TO_PNG' ) && ewww_image_optimizer_get_option( 'ewww_image_optimizer_bmp_convert' ) ) {
ewwwio_debug_message( 'bmp2png' );
// If the file is a BMP and GIF2PNG is enabled.
define( 'EWWW_IMAGE_OPTIMIZER_BMP_TO_PNG', false );
$previous_countermeasures['bmp2png'] = true;
} else {
// If all else fails, skip it.
ewww_image_optimizer_bulk_skip_image( $image );
}
}
if ( 'application/pdf' === ewww_image_optimizer_quick_mimetype( $image->file ) ) {
if ( empty( $previous_countermeasures['pdf20'] ) && ! defined( 'EWWW_IMAGE_OPTIMIZER_PDF_LEVEL' ) && 20 === (int) ewww_image_optimizer_get_option( 'ewww_image_optimizer_pdf_level' ) ) {
ewwwio_debug_message( 'pdf20' );
Expand Down
31 changes: 11 additions & 20 deletions classes/class-background-process-media.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,25 +99,21 @@ protected function task( $item ) {
sleep( 4 );
return $item;
}

ewwwio_debug_message( "background processing $id, type: " . $type );
$image_types = array(
'image/jpeg',
'image/png',
'image/gif',
);
$supported_types = ewwwio()->get_supported_types();

if ( in_array( $type, $image_types, true ) && $item['new'] && class_exists( 'wpCloud\StatelessMedia\EWWW' ) ) {
if ( in_array( $type, $supported_types, true ) && $item['new'] && class_exists( 'wpCloud\StatelessMedia\EWWW' ) ) {
$meta = wp_get_attachment_metadata( $id );
} else {
// This is unfiltered for performance, because we don't often need filtered meta.
$meta = wp_get_attachment_metadata( $id, true );
}
if ( in_array( $type, $image_types, true ) && empty( $meta ) ) {
if ( in_array( $type, $supported_types, true ) && empty( $meta ) ) {
ewwwio_debug_message( "metadata is missing, requeueing {$item['attempts']}" );
sleep( 4 );
return $item;
}
/* $meta = ewww_image_optimizer_resize_from_meta_data( $meta, $id, true, $item['new'] ); */
$this->process_attachment( $meta, $item, $id );

return false;
Expand All @@ -134,7 +130,7 @@ protected function task( $item ) {
*/
protected function should_optimize_size( $file_path, $size, $item, $already_optimized ) {
\ewwwio_debug_message( '<b>' . __METHOD__ . '()</b>' );
if ( apply_filters( 'ewww_image_optimizer_bypass', false, $file_path ) ) {
if ( \apply_filters( 'ewww_image_optimizer_bypass', false, $file_path ) ) {
\ewwwio_debug_message( "skipping $file_path as instructed" );
return false;
}
Expand Down Expand Up @@ -190,7 +186,7 @@ protected function queue_single_size( $id, $size, $file_path, $item ) {
if ( is_array( $optimized_list ) && isset( $optimized_list[ $file_path ] ) ) {
$already_optimized = $optimized_list[ $file_path ];
} else {
$already_optimized = ewww_image_optimizer_find_already_optimized( $file_path );
$already_optimized = \ewww_image_optimizer_find_already_optimized( $file_path );
}

if ( strpos( $wpdb->charset, 'utf8' ) === false ) {
Expand All @@ -201,7 +197,7 @@ protected function queue_single_size( $id, $size, $file_path, $item ) {
}

$image_size = \ewww_image_optimizer_filesize( $file_path );
ewwwio_debug_message( "(maybe) queuing optimization for $id/$size" );
\ewwwio_debug_message( "(maybe) queuing optimization for $id/$size" );
if ( ! $this->should_optimize_size( $file_path, $size, $item, $already_optimized ) ) {
\ewwwio_debug_message( 'already optimized, not forcing or webp-only, so skipping' );
return 0;
Expand Down Expand Up @@ -235,10 +231,10 @@ protected function queue_single_size( $id, $size, $file_path, $item ) {
)
);
$id_to_queue = $ewwwdb->insert_id;
ewwwio_debug_message( 'inserted db record' );
\ewwwio_debug_message( 'inserted db record' );
}
if ( ! $id_to_queue ) {
ewwwio_debug_message( 'failed to update/insert record, no ID to queue' );
\ewwwio_debug_message( 'failed to update/insert record, no ID to queue' );
return 0;
}
ewwwio()->background_image->push_to_queue(
Expand Down Expand Up @@ -320,14 +316,9 @@ protected function process_attachment( $meta, $item, $id ) {
}
}
ewwwio_debug_message( "retrieved file path: $file_path" );

$supported_types = ewwwio()->get_supported_types();
$type = ewww_image_optimizer_mimetype( $file_path, 'i' );
$supported_types = array(
'image/jpeg',
'image/png',
'image/gif',
'application/pdf',
'image/svg+xml',
);
if ( ! in_array( $type, $supported_types, true ) ) {
ewwwio_debug_message( "mimetype not supported: $id" );
return;
Expand Down
40 changes: 38 additions & 2 deletions classes/class-base.php
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,35 @@ public function imagick_supports_webp() {
return $this->imagick_supports_webp;
}

/**
* Get a list of which image/file types are supported.
*
* @param string $select Defaults to 'enabled' to only list those types which have optimization enabled. Specify 'all' to return all possible types.
* @return array A list of file/mime types.
*/
public function get_supported_types( $select = 'enabled' ) {
$supported_types = array();
if ( $this->get_option( 'ewww_image_optimizer_jpg_level' ) || $this->get_option( 'ewww_image_optimizer_webp' ) || 'all' === $select ) {
$supported_types[] = 'image/jpeg';
}
if ( $this->get_option( 'ewww_image_optimizer_png_level' ) || $this->get_option( 'ewww_image_optimizer_webp' ) || 'all' === $select ) {
$supported_types[] = 'image/png';
}
if ( $this->get_option( 'ewww_image_optimizer_gif_level' ) || 'all' === $select ) {
$supported_types[] = 'image/gif';
}
if ( $this->get_option( 'ewww_image_optimizer_pdf_level' ) || 'all' === $select ) {
$supported_types[] = 'application/pdf';
}
if ( $this->get_option( 'ewww_image_optimizer_svg_level' ) || 'all' === $select ) {
$supported_types[] = 'image/svg+xml';
}
if ( $this->get_option( 'ewww_image_optimizer_bmp_convert' ) || $this->get_option( 'ewww_image_optimizer_jpg_level' ) || 'all' === $select ) {
$supported_types[] = 'image/bmp';
}
return $supported_types;
}

/**
* Checks if the S3 Uploads plugin is installed and active.
*
Expand Down Expand Up @@ -972,11 +1001,11 @@ public function get_filesystem() {
* Check the mimetype of the given file with magic mime strings/patterns.
*
* @param string $path The absolute path to the file.
* @param string $category The type of file we are checking. Accepts 'i' for
* @param string $category The type of file we are checking. Default 'i' for
* images/pdfs or 'b' for binary.
* @return bool|string A valid mime-type or false.
*/
public function mimetype( $path, $category ) {
public function mimetype( $path, $category = 'i' ) {
$this->debug_message( '<b>' . __METHOD__ . '()</b>' );
$this->debug_message( "testing mimetype: $path" );
$type = false;
Expand All @@ -1000,6 +1029,11 @@ public function mimetype( $path, $category ) {
// Read first 12 bytes, which equates to 24 hex characters.
$magic = \bin2hex( \substr( $file_contents, 0, 12 ) );
$this->debug_message( $magic );
if ( '424d' === \substr( $magic, 0, 4 ) ) {
$type = 'image/bmp';
$this->debug_message( "ewwwio type: $type" );
return $type;
}
if ( 0 === \strpos( $magic, '52494646' ) && 16 === \strpos( $magic, '57454250' ) ) {
$type = 'image/webp';
$this->debug_message( "ewwwio type: $type" );
Expand Down Expand Up @@ -1077,6 +1111,8 @@ public function mimetype( $path, $category ) {
public function quick_mimetype( $path ) {
$pathextension = \strtolower( \pathinfo( $path, PATHINFO_EXTENSION ) );
switch ( $pathextension ) {
case 'bmp':
return 'image/bmp';
case 'jpg':
case 'jpeg':
case 'jpe':
Expand Down
Loading

0 comments on commit c1d94b9

Please sign in to comment.