Skip to content

Commit

Permalink
Merge pull request #25 from wp-media/1.5.7/feature/resize-wp-api
Browse files Browse the repository at this point in the history
Resize Images using WP Image Editor
  • Loading branch information
GeekPress authored Aug 19, 2016
2 parents f041f2d + 7ef261b commit 674bf6a
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 21 deletions.
38 changes: 30 additions & 8 deletions inc/3rd-party/nextgen-gallery/inc/classes/class-attachment.php
Original file line number Diff line number Diff line change
Expand Up @@ -243,8 +243,9 @@ public function optimize( $optimization_level = null, $metadata = array() ) {
);

// Get file path & URL for original image
$attachment_path = $this->get_original_path();
$attachment_url = $this->get_original_url();
$attachment_path = $this->get_original_path();
$attachment_url = $this->get_original_url();
$attachment_original_size = $this->get_original_size( false );

// Check if the full size is already optimized
if ( $this->is_optimized() && ( $this->get_optimization_level() == $optimization_level ) ) {
Expand All @@ -269,21 +270,42 @@ public function optimize( $optimization_level = null, $metadata = array() ) {
set_transient( 'imagify-ngg-async-in-progress-' . $id, true, 10 * MINUTE_IN_SECONDS );

// Get the resize values for the original size
$resize = array();
$resized = false;
$do_resize = get_imagify_option( 'resize_larger', false );
$resize_width = get_imagify_option( 'resize_larger_w' );
$attachment_size = @getimagesize( $attachment_path );

if ( $do_resize && isset( $attachment_size[0] ) && $resize_width < $attachment_size[0] ) {
$resize['width'] = $resize_width;
}
$resized_attachment_path = $this->resize( $attachment_path, $attachment_size, $resize_width );

if ( ! is_wp_error( $resized_attachment_path ) ) {
$backup_path = get_imagify_attachment_backup_path( $attachment_path );
$backup_path_info = pathinfo( $backup_path );

wp_mkdir_p( $backup_path_info['dirname'] );

// TO DO - check and send a error message if the backup can't be created
@copy( $attachment_path, $backup_path );
imagify_chmod_file( $backup_path );

@rename( $resized_attachment_path, $attachment_path );
imagify_chmod_file( $attachment_path );

// If resized temp file still exists, delete it
if ( file_exists( $resized_attachment_path ) ) {
unlink( $resized_attachment_path );
}

$resized = true;
}
}

// Optimize the original size
$response = do_imagify( $attachment_path, array(
'optimization_level' => $optimization_level,
'resize' => $resize,
'context' => 'ngg',
'original_size' => $this->get_original_size( false )
'resized' => $resized,
'original_size' => $attachment_original_size,
) );
$data = $this->fill_data( $data, $response, $id, $attachment_url );

Expand All @@ -303,7 +325,7 @@ public function optimize( $optimization_level = null, $metadata = array() ) {

// If we resized the original with success, we have to update the attachment metadata
// If not, WordPress keeps the old attachment size.
if ( $do_resize && isset( $resize['width'] ) ) {
if ( $do_resize && $resized ) {
$this->update_metadata_size();
}

Expand Down
57 changes: 57 additions & 0 deletions inc/classes/abstracts/abstract-attachment.php
Original file line number Diff line number Diff line change
Expand Up @@ -380,4 +380,61 @@ public function optimize( $optimization_level = null, $metadata = array() ) {}
* @return void
*/
public function restore() {}

/**
* Resize an image if bigger than the maximum width defined in the settings.
*
* @since 1.5.7
* @author Remy Perona
*
* @param string $attachment_path Path to the image
* @param array $attachment_sizes Array of original image dimensions
* @param int $max_width Maximum width defined in the settings
* @return string Path the the resized image or the original image if the resize failed
*/
function resize( $attachment_path, $attachment_sizes, $max_width ) {
$new_sizes = wp_constrain_dimensions( $attachment_sizes[0], $attachment_sizes[1], $max_width );

$editor = wp_get_image_editor( $attachment_path );

if ( is_wp_error( $editor ) ) {
return $editor;
}

$image_type = pathinfo( $attachment_path, PATHINFO_EXTENSION );

// try to correct for auto-rotation if the info is available
if ( function_exists( 'exif_read_data' ) && ( $image_type == 'jpg' || $image_type == 'jpeg' ) ) {
$exif = @exif_read_data( $attachment_path );
$orientation = is_array( $exif ) && array_key_exists( 'Orientation', $exif ) ? $exif['Orientation'] : 0;

switch( $orientation ) {
case 3:
$editor->rotate( 180 );
break;
case 6:
$editor->rotate( -90 );
break;
case 8:
$editor->rotate( 90 );
break;
}
}

$resized = $editor->resize( $new_sizes[0], $new_sizes[1], false );

if ( is_wp_error( $resized ) ) {
return $resized;
}

$resized_image_path = $editor->generate_filename( null );

$resized_image_saved = $editor->save( $resized_image_path );

if ( is_wp_error( $resized_image_saved ) ) {
return $resized_image_saved;
}

return $resized_image_path;
}
}
38 changes: 30 additions & 8 deletions inc/classes/class-attachment.php
Original file line number Diff line number Diff line change
Expand Up @@ -199,8 +199,9 @@ public function optimize( $optimization_level = null, $metadata = array() ) {
);

// Get file path & URL for original image
$attachment_path = $this->get_original_path();
$attachment_url = $this->get_original_url();
$attachment_path = $this->get_original_path();
$attachment_url = $this->get_original_url();
$attachment_original_size = $this->get_original_size( false );

// Check if the attachment extension is allowed
if ( ! $id || ! wp_attachment_is_image( $id ) ) {
Expand Down Expand Up @@ -230,21 +231,42 @@ public function optimize( $optimization_level = null, $metadata = array() ) {
set_transient( 'imagify-async-in-progress-' . $id, true, 10 * MINUTE_IN_SECONDS );

// Get the resize values for the original size
$resize = array();
$resized = false;
$do_resize = get_imagify_option( 'resize_larger', false );
$resize_width = get_imagify_option( 'resize_larger_w' );
$attachment_size = @getimagesize( $attachment_path );

if ( $do_resize && isset( $attachment_size[0] ) && $resize_width < $attachment_size[0] ) {
$resize['width'] = $resize_width;
}
$resized_attachment_path = $this->resize( $attachment_path, $attachment_size, $resize_width );

if ( ! is_wp_error( $resized_attachment_path ) ) {
$backup_path = get_imagify_attachment_backup_path( $attachment_path );
$backup_path_info = pathinfo( $backup_path );

wp_mkdir_p( $backup_path_info['dirname'] );

// TO DO - check and send a error message if the backup can't be created
@copy( $attachment_path, $backup_path );
imagify_chmod_file( $backup_path );

@rename( $resized_attachment_path, $attachment_path );
imagify_chmod_file( $attachment_path );

// If resized temp file still exists, delete it
if ( file_exists( $resized_attachment_path ) ) {
unlink( $resized_attachment_path );
}

$resized = true;
}
}

// Optimize the original size
$response = do_imagify( $attachment_path, array(
'optimization_level' => $optimization_level,
'resize' => $resize,
'context' => 'wp',
'original_size' => $this->get_original_size( false )
'resized' => $resized,
'original_size' => $attachment_original_size,
) );
$data = $this->fill_data( $data, $response, $id, $attachment_url );

Expand All @@ -258,7 +280,7 @@ public function optimize( $optimization_level = null, $metadata = array() ) {

// If we resized the original with success, we have to update the attachment metadata
// If not, WordPress keeps the old attachment size.
if ( $do_resize && isset( $resize['width'] ) ) {
if ( $do_resize && $resized ) {
$this->update_metadata_size();
}

Expand Down
8 changes: 3 additions & 5 deletions inc/functions/process.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
* @param string $file_path Absolute path to the image file.
* @param bool $backup Force a backup of the original file.
* @param int $optimization_level The optimization level (2=ultra, 1=aggressive, 0=normal).
* @param array $resize The resize parameters (with & height).
* @param bool $keep_exif To keep exif data or not
* @return obj|array Error message | Optimized image data
*/
Expand All @@ -19,10 +18,10 @@ function do_imagify( $file_path, $args = array() ) {
array(
'backup' => get_imagify_option( 'backup', false ),
'optimization_level' => get_imagify_option( 'optimization_level', 1 ),
'resize' => array(),
'keep_exif' => get_imagify_option( 'exif', false ),
'context' => 'wp',
'original_size' => 0
'resized' => false,
'original_size' => 0,
),
$args
);
Expand Down Expand Up @@ -92,7 +91,6 @@ function do_imagify( $file_path, $args = array() ) {
array(
'aggressive' => ( 1 === (int) $args['optimization_level'] ) ? true : false,
'ultra' => ( 2 === (int) $args['optimization_level'] ) ? true : false,
'resize' => $args['resize'],
'keep_exif' => $args['keep_exif'],
'context' => $args['context'],
'original_size' => $args['original_size']
Expand All @@ -108,7 +106,7 @@ function do_imagify( $file_path, $args = array() ) {
}

// Create a backup file
if ( 'wp' === $args['context'] && $args['backup'] ) {
if ( 'wp' === $args['context'] && $args['backup'] && ! $args['resized'] ) {
$backup_path = get_imagify_attachment_backup_path( $file_path );
$backup_path_info = pathinfo( $backup_path );

Expand Down

0 comments on commit 674bf6a

Please sign in to comment.