Created
August 13, 2012 03:35
-
-
Save pedroelsner/3336770 to your computer and use it in GitHub Desktop.
CHANGE - Regenerete Thumbnail Wordpress
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Process a single image ID (this is an AJAX handler) | |
function ajax_process_image() { | |
@error_reporting( 0 ); // Don't break the JSON result | |
header( 'Content-type: application/json' ); | |
$id = (int) $_REQUEST['id']; | |
$image = get_post( $id ); | |
if ( ! $image || 'attachment' != $image->post_type || 'image/' != substr( $image->post_mime_type, 0, 6 ) ) { | |
die( json_encode( array( 'error' => sprintf( __( 'Failed resize: %s is an invalid image ID.', 'regenerate-thumbnails' ), esc_html( $_REQUEST['id'] ) ) ) ) ); | |
return; | |
} | |
if ( ! current_user_can( $this->capability ) ) { | |
$this->die_json_error_msg( $image->ID, __( "Your user account doesn't have permission to resize images", 'regenerate-thumbnails' ) ); | |
return; | |
} | |
$fullsizepath = get_attached_file( $image->ID ); | |
if ( false === $fullsizepath || ! file_exists( $fullsizepath ) ) { | |
$this->die_json_error_msg( $image->ID, sprintf( __( 'The originally uploaded image file cannot be found at %s', 'regenerate-thumbnails' ), '<code>' . esc_html( $fullsizepath ) . '</code>' ) ); | |
return; | |
} | |
@set_time_limit( 900 ); // 5 minutes per image should be PLENTY | |
// ############ | |
// DELETE ALL THUMBNAIL, TO REALLY REGENERETE =D | |
// By: Pedro Elsner | |
// ############ | |
global $_wp_additional_image_sizes; | |
$imagePath = substr($fullsizepath, 0, strlen($fullsizepath) - 4); | |
$imageFormat = substr($fullsizepath, -4); | |
foreach($_wp_additional_image_sizes as $key => $value) { | |
$thumb = $imagePath . '-' . $value['width'] . 'x' . $value['height'] . $imageFormat; | |
unlink($thumb); | |
} | |
// ############ | |
$metadata = wp_generate_attachment_metadata( $image->ID, $fullsizepath ); | |
if ( is_wp_error( $metadata ) ) { | |
$this->die_json_error_msg( $image->ID, $metadata->get_error_message() ); | |
return; | |
} | |
if ( empty( $metadata ) ) { | |
$this->die_json_error_msg( $image->ID, __( 'Unknown failure reason.', 'regenerate-thumbnails' ) ); | |
return; | |
} | |
wp_update_attachment_metadata( $image->ID, $metadata ); | |
die( json_encode( array( 'success' => sprintf( __( '"%1$s" (ID %2$s) was successfully resized in %3$s seconds.', 'regenerate-thumbnails' ), esc_html( get_the_title( $image->ID ) ), $image->ID, timer_stop() ) ) ) ); | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment