Skip to content

Instantly share code, notes, and snippets.

@adamsilverstein
Last active July 22, 2024 14:24
Show Gist options
  • Save adamsilverstein/b4e91c9ab1e6f546ec98e3dcc53afb7d to your computer and use it in GitHub Desktop.
Save adamsilverstein/b4e91c9ab1e6f546ec98e3dcc53afb7d to your computer and use it in GitHub Desktop.
test image processing
<?php
/**
* Profile image loading and resizing.
*
* @wordpress-plugin
* Plugin Name: Profile image processing
* Description: Profile image loading and resizing by mime format.
* Plugin URI: n/a
* Version: 1.0.0
* Author: Adam Silverstein, Google
* License: Apache License 2.0
* License URI: https://www.apache.org/licenses/LICENSE-2.0
*/
/**
* Function to get timing data for image format and size manipulation.
*/
function image_tester_get_profiling_data() {
error_log( 'starting tests at ' . date( 'Y-m-d H:i:s' ) );
$source_image = 'images/test-image.jpg';
// Filter image output to each format, measuring performance of image processing.
$image_formats = array (
'image/jpeg',
'image/webp',
'image/avif',
);
$image_sizes = array (
array( 300, 300 ),
array( 600, 600 ),
array( 1200, 1200 ),
array( 2400, 2400 ),
);
$profiling_data = array();
$total_time = array();
$image_path = plugin_dir_path( __FILE__ ) . $source_image;
error_log( 'Processing image... ' . $image_path );
foreach( $image_formats as $format ) {
// Log the format.
error_log( 'Processing format: ' . $format );
// Now, test converting the image to the output format.
$editor = wp_get_image_editor( $image_path );
// Skip with a warning if the editor is not available.
if ( is_wp_error( $editor ) ) {
echo $editor->get_error_message();
continue;
}
$total_time[ $format ]['start'] = microtime( true );
// For each size, record the time to resize and save an image.
foreach( $image_sizes as $size ) {
// Start timer.
$start = microtime( true );
// Resize the image.
$editor->resize( $size[0], $size[1], false );
// Save.
$result = $editor->save( null, $format );
// Stop the timer.
$end = microtime( true );
// Record the datapoint.
array_push(
$profiling_data,
array (
'format' => $format,
'size' => array (
'width' => $size[0],
'height' => $size[1]
),
'filesize' => $result['filesize'],
'time' => $end - $start,
)
);
}
$total_time[ $format ]['end'] = microtime( true );
$total_time[ $format ]['total'] = $total_time[ $format ]['end'] - $total_time[ $format ]['start'];
}
error_log( 'finished tests at ' . date( 'Y-m-d H:i:s' ) );
return array(
'profiling_data' => $profiling_data,
'total_time' => $total_time,
);
}
function image_tests_run_tests() {
$profiling_data = image_tester_get_profiling_data();
// Log the data out.
error_log( print_r( $profiling_data, true ) );
error_log( json_encode( $profiling_data ) );
error_log( '--------------------------------------');
error_log( 'format, size, time' );
// Loop thru the results and output csv data in the format: format, size_string, time
foreach( $profiling_data['profiling_data'] as $result ) {
error_log(
sprintf(
'%s, %sx%s, %s',
$result['format'],
$result['size']['width'],
$result['size']['height'],
$result['time']
)
);
}
}
add_action( 'init', 'image_tests_run_tests' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment