When I insert new images from Unsplash.com the image size "full" returns a scaled image with "-scaled.jpg" at the end of the name file and is always limited to 2560 pixels. So how can I retrieve the real "full" size of the image?
2 Answers
You have first to disable this "feature" introduced in WordPress 5.3 by adding this line of code in your functions.php:
add_filter( 'big_image_size_threshold', '__return_false' );
This will disable the scaling down for any FUTURE uploads. But for existing images you have to update the image sizes.
Unfortunately the WordPress team proves once more its incompetence - they dind't provide any functions to update the postmeta for existing attachments and obviously they don't give a single f... about this issue as you can see here, where dozens of people expressed there anger about this so called feature.
I have created today a site with several thousands of dummy posts with featured images from Unsplash.com and where I needed the full size of the images. It took a couple of hours to run the script which downloaded and created the blog posts and the attachments posts. So it wasn't an option to me to delete all the posts and run the dummy script again once I've found out about the new "feature" and how to disable it.
So I wrote another script which took me still a bunch of time...
You need to put the following code in a php file and run/call it from either the browser or terminal. Don't forget to replace the path to the wp-load.php file. On my local machine it took just a couple of seconds for several thousand attachment posts.
<?php
require_once( "/absolute/or/relative/path/to/wordpress/wp-load.php" );
ini_set( 'max_execution_time', 3600 );
set_time_limit( 3600 );
$pdo = new PDO( "mysql:dbname=" . DB_NAME . ";host=" . DB_HOST, DB_USER, DB_PASSWORD );
/**
* replace _wp_attached_file meta_key
**/
global $wpdb;
$wp_postmeta = $wpdb->prefix . "postmeta";
try {
$pdo->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );//Error Handling
$sql = "UPDATE $wp_postmeta SET meta_value = REPLACE(meta_value,'-scaled.jpg','.jpg') WHERE meta_key='_wp_attached_file' AND meta_value LIKE '%-scaled.jpg%'";
$result = $pdo->exec( $sql );
print_r( $result );
} catch ( PDOException $e ) {
print_r( $e->getMessage() );
}
/**
* replace _wp_attachment_metadata meta_key
**/
$image_metas = [];
try {
$pdo->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );//Error Handling
$sql = "SELECT * FROM $wp_postmeta WHERE meta_value LIKE '%-scaled.jpg%' AND meta_key='_wp_attachment_metadata'";
$statement = $pdo->query( $sql );
$image_metas = $statement->fetchAll();
foreach ( $image_metas as $meta ) {
$meta_value = unserialize( $meta["meta_value"] );
$file = $meta_value["file"];
$meta_value["file"] = str_replace( "-scaled.jpg", ".jpg", $file );
update_post_meta( $meta["post_id"], $meta["meta_key"], $meta_value );
$result = get_post_meta( $meta["post_id"], $meta["meta_key"] );
print_r( $result );
}
} catch ( PDOException $e ) {
print_r( $e->getMessage() );
}
As detailed above, by default Wordpress uses the 2560 -scaled.jpg
version when accessing with full
if the original file was bigger than 2560. However it retains a reference to the original unscaled file that can be accessed with this function:
$original_url = wp_get_original_image_url($img_id);