开发者

Automatically use resized images instead of originals in Wordpress aspect ratio?

开发者 https://www.devze.com 2023-03-07 15:43 出处:网络
I would appriciate Your help. I used custom field in Wordpress and created the form to upload the image in the post. Everything works fine. I also placed this code to replace the original image ( if s

I would appriciate Your help. I used custom field in Wordpress and created the form to upload the image in the post. Everything works fine. I also placed this code to replace the original image ( if someone posted huge image size ) which automatically resizes it. It in fact resizes the image but it doesn't keep the aspect ratio meaning max 500px width, max 800px height. It takes that walues and becomes croped to that size. I want the height to be proportional not croped! this goes to functions.php

function replace_uploaded_image($image_data) {
// if there is no large image : return
if (!isset($image_data['sizes']['large'])) return $image_data;

// paths 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'] . '/'.$i开发者_如何转开发mage_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');

http://pastie.org/1928349


Correct me if I'm wrong, but you seem to be rewriting a function that already exists in Wordpress - http://codex.wordpress.org/Function_Reference/add_image_size

0

精彩评论

暂无评论...
验证码 换一张
取 消