Skip to content

Instantly share code, notes, and snippets.

@MrJSdev
Last active August 11, 2020 12:51
Show Gist options
  • Save MrJSdev/03795211b1299985ef79a10843e81977 to your computer and use it in GitHub Desktop.
Save MrJSdev/03795211b1299985ef79a10843e81977 to your computer and use it in GitHub Desktop.
The guide for this code is available at http://extracatchy.net/resize-wordpress-images-on-upload
<?php
/**resize on upload to the largest size in media setting */
function replace_uploaded_image($image_data) {
// if there is no large image : return
if (!isset($image_data['sizes']['large'])) return $image_data;
// path to the uploaded image and the large image
$upload_dir = wp_upload_dir();
$uploaded_image_location = $upload_dir['basedir'] . '/' .$image_data['file'];
$large_image_location = $upload_dir['path'] . '/'.$image_data['sizes']['large']['file'];
// delete the uploaded image
unlink($uploaded_image_location);
// rename the large image
rename($large_image_location,$uploaded_image_location);
// update image metadata and return them
$image_data['width'] = $image_data['sizes']['large']['width'];
$image_data['height'] = $image_data['sizes']['large']['height'];
unset($image_data['sizes']['large']);
return $image_data;
}
add_filter('wp_generate_attachment_metadata','replace_uploaded_image');
@nnagewad
Copy link

How do you use that code while keeping the original uploaded image?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment