2015/04/11

画像をアップロードする際に自動的にリサイズする

tags : / / /

1024px以上のもをアップロードしても、アップロード時に縦横長い方を
【ダッシュボード→メディア設定】の大サイズ(1024pxがデフォルト)にリサイズします。

 

function.php

function otocon_resize_at_upload( $file ) {
 // $file contains file, url, type
 // array( 'file' => 'path to the image', 'url' => 'url of the image', 'type' => 'mime type' )

// resize only if the mime type is image
 if ( $file['type'] == 'image/jpeg' OR $file['type'] == 'image/gif' OR $file['type'] == 'image/png') {

// set width and height
 $w = intval(get_option( 'large_size_w' ) ); // get large size width
 $h = intval(get_option( 'large_size_h' ) ); // get large size height

// get the uploaded image
 $image = wp_get_image_editor( $file['file'] );

// if no error
 if ( ! is_wp_error( $image ) ){
 // get image width and height
 $size = getimagesize( $file['file'] ); // $size[0] = width, $size[1] = height

if ( $size[0] > $w || $size[1] > $h ){ // if the width or height is larger than the large-size
 $image->resize( $w, $h, false ); // resize the image
 $final_image = $image->save( $file['file'] ); // save the resized image
 }
 }

} // if mime type

return $file;

}
 add_action( 'wp_handle_upload', 'otocon_resize_at_upload' );

 

容量が大きすぎると、アップロード自体ができないので、アップロードのサイズの上限を上げておいた方がいいです。

アップロードファイルサイズの上限を変更|WordPress

 

  • このエントリーをはてなブックマークに追加

Profoile

coralqualia
HaraYohei http://coralqualia.com/

当サイトの運営者。 ビジネスを成功へ導くため、「思いをカタチに変える」お手伝いをします。 印刷物、WEBサイトの制作はもちろん、その他なんでもご相談ください。 少しでもお客様のお役に立てればと思っております。 【Service】 ロゴ/販促ツール制作/パッケージ/誌面/各種広告制作/看板サイン/各種印刷 WEBサイト企画立案/Webデザイン/Webサイト制作/CMS(WordPress)構築/ECサイト構築/サーバー運用管理/サイト運営サポート イラスト/キャラクターデザイン Wechatを活用したサービスの提供 /中国でのWEBサイト構築・運営・販促活動