Skip to content

Commit

Permalink
cleanup php notices and add unit tests
Browse files Browse the repository at this point in the history
Cleanup some PHP notices by setting the path/file variables earlier.
Add unit tests for BMP to JPG.
Do a preemptive BMP to JPG option check to avoid backing up a BMP when not needed.
Prevent queuing BMP images when conversion is disabled in async handler.
  • Loading branch information
nosilver4u committed Sep 4, 2024
1 parent c1d94b9 commit 4ce6277
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 4 deletions.
4 changes: 4 additions & 0 deletions classes/class-background-process-media.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,10 @@ protected function should_optimize_size( $file_path, $size, $item, $already_opti
\ewwwio_debug_message( "file skipped due to PNG filesize: $file_path" );
return false;
}
if ( 'image/bmp' === $mime && ! ewww_image_optimizer_get_option( 'ewww_image_optimizer_bmp_convert' ) && empty( $item['convert_once'] ) ) {
\ewwwio_debug_message( "BMP skipped, no conversion enabled: $file_path" );
return false;
}
$compression_level = \ewww_image_optimizer_get_level( $mime );
$smart_reopt = false;
if ( ! empty( $item['force_smart'] ) && ! \ewww_image_optimizer_level_mismatch( $already_optimized['level'], $compression_level ) ) {
Expand Down
3 changes: 3 additions & 0 deletions classes/class-base.php
Original file line number Diff line number Diff line change
Expand Up @@ -911,6 +911,9 @@ public function is_lazy_placeholder( $image ) {
* @return bool True if the file exists and is local, false otherwise.
*/
public function is_file( $file ) {
if ( empty( $file ) ) {
return false;
}
if ( false !== \strpos( $file, '://' ) ) {
return false;
}
Expand Down
21 changes: 17 additions & 4 deletions classes/class-ewww-image.php
Original file line number Diff line number Diff line change
Expand Up @@ -717,6 +717,7 @@ public function convert( $file, $replace_url = true, $check_size = false, $newfi
ewwwio_debug_message( "cannot convert mimetype: $type" );
return false;
}
$started = microtime( true );
switch ( $type ) {
case 'image/jpeg':
$png_size = 0;
Expand Down Expand Up @@ -954,11 +955,12 @@ public function convert( $file, $replace_url = true, $check_size = false, $newfi
if ( 'jpg' === $pathinfo['extension'] ) {
$convert_to_png = false;
}
$jpg_size = 0;
$png_size = 0;
$jpg_size = 0;
$png_size = 0;
$newjpgfile = ! empty( $newfile ) && ! ewwwio_is_file( $newfile ) ? $newfile : $this->unique_filename( $file, '.jpg' );
$newpngfile = ! empty( $newfile ) && ! ewwwio_is_file( $newfile ) ? $newfile : $this->unique_filename( $file, '.png' );
if ( $convert_to_jpg ) {
// Convert the BMP to JPG.
$newjpgfile = ! empty( $newfile ) && ! ewwwio_is_file( $newfile ) ? $newfile : $this->unique_filename( $file, '.jpg' );
ewwwio_debug_message( "attempting to convert BMP to JPG: $newjpgfile" );
// If the user manually set the JPG quality.
$quality = ewww_image_optimizer_jpg_quality();
Expand Down Expand Up @@ -992,7 +994,6 @@ public function convert( $file, $replace_url = true, $check_size = false, $newfi
}
if ( $convert_to_png ) {
// Convert the BMP to PNG.
$newpngfile = ! empty( $newfile ) && ! ewwwio_is_file( $newfile ) ? $newfile : $this->unique_filename( $file, '.png' );
ewwwio_debug_message( "attempting to convert BMP to PNG: $newpngfile" );
if ( \ewwwio()->imagick_support() ) {
try {
Expand Down Expand Up @@ -1057,9 +1058,13 @@ public function convert( $file, $replace_url = true, $check_size = false, $newfi
default:
return false;
} // End switch().
$elapsed = microtime( true ) - $started;
\ewwwio_debug_message( "converting image took $elapsed seconds" );
if ( $replace_url ) {
$this->replace_url( $newfile, $file );
}
$elapsed = microtime( true ) - $started;
\ewwwio_debug_message( "converting and replacing URL took $elapsed seconds" );
return $newfile;
}

Expand Down Expand Up @@ -1345,6 +1350,14 @@ public function time_estimate() {
}
}
switch ( $type ) {
case 'image/bmp':
++$time;
if ( $image_size > 1000000 ) { // greater than 1MB.
++$time;
} elseif ( $image_size > 5000000 ) { // greater than 5MB.
$time += 9;
}
break;
case 'image/jpeg':
if ( $image_size > 10000000 ) { // greater than 10MB.
$time += 20;
Expand Down
44 changes: 44 additions & 0 deletions tests/test-convert.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ class EWWWIO_Convert_Tests extends WP_UnitTestCase {
*/
public static $test_gif = '';

/**
* The location of the test BMP image.
*
* @var string $test_bmp
*/
public static $test_bmp = '';

/**
* The API key used for API-based tests.
*
Expand Down Expand Up @@ -59,6 +66,10 @@ public static function set_up_before_class() {
rename( $test_gif, $temp_upload_dir . wp_basename( $test_gif ) );
self::$test_gif = $temp_upload_dir . wp_basename( $test_gif );

$test_bmp = download_url( 'https://ewwwio-test.sfo2.digitaloceanspaces.com/unit-tests/IMG_0391.bmp' );
rename( $test_bmp, $temp_upload_dir . wp_basename( $test_bmp ) );
self::$test_bmp = $temp_upload_dir . wp_basename( $test_bmp );

self::$api_key = getenv( 'EWWWIO_API_KEY' );

ewwwio()->set_defaults();
Expand Down Expand Up @@ -135,6 +146,22 @@ protected function optimize_gif( $original = false ) {
return $results;
}

/**
* Copies the test BMP to a temp file, optimizes it, and returns the results.
*
* @return array The results of the ewww_image_optimizer() function.
*/
protected function optimize_bmp( $original = false ) {
if ( ! $original ) {
$original = self::$test_bmp;
}
ewwwio()->force = true;
$filename = $original . ".bmp";
copy( $original, $filename );
$results = ewww_image_optimizer( $filename, 1, false, false, true );
return $results;
}

/**
* Test local JPG to PNG conversion.
*/
Expand Down Expand Up @@ -400,6 +427,23 @@ function test_api_convert_gif_to_png() {
update_site_option( 'ewww_image_optimizer_cloud_key', '' );
}

/**
* Test local BMP to JPG conversion.
*/
function test_local_convert_bmp_to_jpg() {
update_option( 'ewww_image_optimizer_jpg_level', 10 );
update_option( 'ewww_image_optimizer_bmp_convert', true );
update_site_option( 'ewww_image_optimizer_jpg_level', 10 );
update_site_option( 'ewww_image_optimizer_bmp_convert', true );

$results = $this->optimize_bmp();
$this->assertEquals( 'image/jpeg', ewww_image_optimizer_mimetype( $results[0], 'i' ) );
unlink( $results[0] );

update_option( 'ewww_image_optimizer_bmp_convert', '' );
update_site_option( 'ewww_image_optimizer_bmp_convert', '' );
}

/**
* Cleans up ewwwio_images table.
*/
Expand Down
4 changes: 4 additions & 0 deletions unique.php
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,10 @@ function ewww_image_optimizer( $file, $gallery_type = 4, $converted = false, $ne
// Tell the user optimization was skipped.
return array( false, __( 'Optimization skipped', 'ewww-image-optimizer' ), $converted, $file );
}
if ( 'image/bmp' === $type && ! ewww_image_optimizer_get_option( 'ewww_image_optimizer_bmp_convert' ) && empty( $ewww_convert ) ) {
ewwwio_debug_message( "BMP skipped, no conversion enabled: $file" );
return array( false, __( 'Optimization skipped', 'ewww-image-optimizer' ), $converted, $file );
}
$backup_hash = '';
$new_size = 0;
// Set the optimization process to OFF.
Expand Down

0 comments on commit 4ce6277

Please sign in to comment.