Esempio n. 1
0
function ewww_image_optimizer_update_table($attachment, $opt_size, $orig_size, $preserve_results = false)
{
    ewwwio_debug_message('<b>' . __FUNCTION__ . '()</b>');
    global $wpdb;
    $query = $wpdb->prepare("SELECT id,orig_size,results,path,updates FROM {$wpdb->ewwwio_images} WHERE path = %s", $attachment);
    $optimized_query = $wpdb->get_results($query, ARRAY_A);
    if (!empty($optimized_query)) {
        foreach ($optimized_query as $image) {
            if ($image['path'] != $attachment) {
                ewwwio_debug_message("{$image['path']} does not match {$attachment}, continuing our search");
            } else {
                $already_optimized = $image;
            }
        }
    }
    ewwwio_debug_message("savings: {$opt_size} (new) vs. {$orig_size} (orig)");
    if (!empty($already_optimized['results']) && $preserve_results && $opt_size === $orig_size) {
        $results_msg = $already_optimized['results'];
    } elseif ($opt_size >= $orig_size) {
        ewwwio_debug_message("original and new file are same size (or something weird made the new one larger), no savings");
        $results_msg = __('No savings', EWWW_IMAGE_OPTIMIZER_DOMAIN);
    } else {
        // calculate how much space was saved
        $savings = intval($orig_size) - intval($opt_size);
        // convert it to human readable format
        $savings_str = size_format($savings, 1);
        // replace spaces and extra decimals with proper html entity encoding
        $savings_str = preg_replace('/\\.0 B /', ' B', $savings_str);
        $savings_str = str_replace(' ', '&nbsp;', $savings_str);
        // determine the percentage savings
        $percent = 100 - 100 * ($opt_size / $orig_size);
        // use the percentage and the savings size to output a nice message to the user
        $results_msg = sprintf(__("Reduced by %01.1f%% (%s)", EWWW_IMAGE_OPTIMIZER_DOMAIN), $percent, $savings_str);
        ewwwio_debug_message("original and new file are different size: {$results_msg}");
    }
    if (empty($already_optimized)) {
        ewwwio_debug_message("creating new record, path: {$attachment}, size: {$opt_size}");
        // store info on the current image for future reference
        $wpdb->insert($wpdb->ewwwio_images, array('path' => $attachment, 'image_size' => $opt_size, 'orig_size' => $orig_size, 'results' => $results_msg, 'updated' => date('Y-m-d H:i:s'), 'updates' => 1));
    } else {
        if (ewww_image_optimizer_get_option('ewww_image_optimizer_debug')) {
            $trace = ewwwio_debug_backtrace();
        } else {
            $trace = '';
        }
        ewwwio_debug_message("updating existing record ({$already_optimized['id']}), path: {$attachment}, size: {$opt_size}");
        // store info on the current image for future reference
        $wpdb->update($wpdb->ewwwio_images, array('image_size' => $opt_size, 'results' => $results_msg, 'updates' => $already_optimized['updates'] + 1, 'trace' => $trace), array('id' => $already_optimized['id']));
    }
    ewwwio_memory(__FUNCTION__);
    $wpdb->flush();
    ewwwio_memory(__FUNCTION__);
    return $results_msg;
}
Esempio n. 2
0
function ewww_image_optimizer_update_table( $attachment, $opt_size, $orig_size, $preserve_results = false ) {
	ewwwio_debug_message( '<b>' . __FUNCTION__ . '()</b>' );
	global $wpdb;
	$already_optimized = ewww_image_optimizer_find_already_optimized( $attachment );
	if ( $already_optimized && $opt_size >= $orig_size ) {
		$prev_string = ' - ' . __( 'Previously Optimized', EWWW_IMAGE_OPTIMIZER_DOMAIN );
	} else {
		$prev_string = '';
	}
	if ( is_array( $already_optimized ) && ! empty( $already_optimized['orig_size'] ) && $already_optimized['orig_size'] > $orig_size ) {
		$orig_size = $already_optimized['orig_size'];
	}
	ewwwio_debug_message( "savings: $opt_size (new) vs. $orig_size (orig)" );
	if ( is_array( $already_optimized ) && ! empty( $already_optimized['results'] ) && $preserve_results && $opt_size == $orig_size) {
		$results_msg = $already_optimized['results'];
	} elseif ( $opt_size >= $orig_size ) {
		ewwwio_debug_message( "original and new file are same size (or something weird made the new one larger), no savings" );
		$results_msg = __( 'No savings', EWWW_IMAGE_OPTIMIZER_DOMAIN );
	} else {
		// calculate how much space was saved
		$savings = intval( $orig_size ) - intval( $opt_size );
		// convert it to human readable format
		$savings_str = size_format( $savings, 1 );
		// replace spaces and extra decimals with proper html entity encoding
		$savings_str = preg_replace( '/\.0 B /', ' B', $savings_str );
		$savings_str = str_replace( ' ', '&nbsp;', $savings_str );
		// determine the percentage savings
		$percent = 100 - ( 100 * ( $opt_size / $orig_size ) );
		// use the percentage and the savings size to output a nice message to the user
		$results_msg = sprintf( __( "Reduced by %01.1f%% (%s)", EWWW_IMAGE_OPTIMIZER_DOMAIN ),
			$percent,
			$savings_str
		) . $prev_string;
		ewwwio_debug_message( "original and new file are different size: $results_msg" );
	}
	if ( empty( $already_optimized ) ) {
		ewwwio_debug_message( "creating new record, path: $attachment, size: $opt_size" );
		// store info on the current image for future reference
		$wpdb->insert( $wpdb->ewwwio_images, array(
			'path' => $attachment,
			'image_size' => $opt_size,
			'orig_size' => $orig_size,
			'results' => $results_msg,
			'updated' => date( 'Y-m-d H:i:s' ),
			'updates' => 1,
		) );
	} else {
		if ( ewww_image_optimizer_get_option( 'ewww_image_optimizer_debug' ) ) {
			$trace = ewwwio_debug_backtrace();
		} else {
			$trace = '';
		}
		ewwwio_debug_message( "updating existing record ({$already_optimized['id']}), path: $attachment, size: $opt_size" );
		// store info on the current image for future reference
		$wpdb->update( $wpdb->ewwwio_images,
			array(
				'image_size' => $opt_size,
				'results' => $results_msg,
				'updates' => $already_optimized['updates'] + 1,
				'trace' => $trace,
			),
			array(
				'id' => $already_optimized['id'],
			)
		);
	}
	ewwwio_memory( __FUNCTION__ );
	$wpdb->flush();
	ewwwio_memory( __FUNCTION__ );
	return $results_msg;
}