Tag: 自動的にリサイズ


アップロードする画像のサイズを追加|WordPress

2016年10月14日 wordpressアイキャッチ画像画像

WordPressでは、画像をアップロードすると、
サムネイルサイズ・中サイズ・大サイズ・フルサイズが自動的にアップされます。
サイズの変更はダッシュボード>設定>メディアからできます。

毎回設定から変更するのは面倒くさい、他のサイズも使いたいという時に役に立ちます。

しかも簡単です。function.phpに以下を追加するだけ。

 

function.php

横200px、縦125pxの画像を追加する時。
【thumb_name】は自分でわかるように入れてください。

/** 好きな画像サイズを追加する
add_image_size( 'thumb_name', 200, 125, true );

表示させるときは、

テンプレートファイル

<?php the_post_thumbnail('thumb_name'); ?>

アイキャッチ画像の出力|WordPressの要領です。

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

2015年04月11日 wordpress画像

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