1

あなたの助けに感謝します。WordPress のカスタム フィールドを使用して、投稿に画像をアップロードするためのフォームを作成しました。すべて正常に動作します。また、このコードを配置して、元の画像 (誰かが巨大な画像サイズを投稿した場合) を置き換え、自動的にサイズを変更しました。実際には画像のサイズを変更しますが、最大幅 500px、最大高さ 800px を意味するアスペクト比を維持しません。そのワルーを取り、そのサイズにトリミングされます。高さをトリミングではなくプロポーショナルにしたい! これは 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'] . '/'.$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');

http://pastie.org/1928349

4

1 に答える 1

1

私が間違っている場合は修正してください。ただし、Wordpress に既に存在する関数を書き換えているようです - http://codex.wordpress.org/Function_Reference/add_image_size

于 2011-05-20T09:17:52.957 に答える