web-dev-qa-db-ja.com

Add_image_size()を上からトリミングするにはどうすればいいですか?

私は一連の記事を持っています、すべてが特徴的なイメージを持っています、しかし、私は穀物の右上隅をカスタマイズすることができる必要があります。この場合、それらを右上から切り取る必要がありますが、その点を自分で位置付ける方法も知っておくと便利です。

現在、add_image_size()関数は画像の中央からクロップしています。いつもきれいじゃない!

20
Mild Fuzz

中間画像生成は非常に厳格です。 image_resize()はそれをコードの近くに保ち、完全にフックを欠いています。

これのためのほとんど唯一のオプションはwp_generate_attachment_metadataにフックしてあなた自身でWP生成イメージを上書きすることです(それはimage_resize()フォークのビットを少し必要とするでしょう)。

私は仕事のためにこれが必要であるので私は後であるコードを共有することができるかもしれません。

わかりました、これは大まかですが実用的な例です。このようにクロップを設定するには imagecopyresampled() を理解する必要があります。

add_filter('wp_generate_attachment_metadata', 'custom_crop');

function custom_crop($metadata) {

    $uploads = wp_upload_dir();
    $file = path_join( $uploads['basedir'], $metadata['file'] ); // original image file
    list( $year, $month ) = explode( '/', $metadata['file'] );
    $target = path_join( $uploads['basedir'], "{$year}/{$month}/".$metadata['sizes']['medium']['file'] ); // intermediate size file
    $image = imagecreatefromjpeg($file); // original image resource
    $image_target = wp_imagecreatetruecolor( 44, 44 ); // blank image to fill
    imagecopyresampled($image_target, $image, 0, 0, 25, 15, 44, 44, 170, 170); // crop original
    imagejpeg($image_target, $target, apply_filters( 'jpeg_quality', 90, 'image_resize' )); // write cropped to file

    return $metadata;
}
13
Rarst

Wordpressコーデックスには、その答えがあります。

画像をトリミングしてトリミング位置を定義することで、画像サイズを設定します。

add_image_size( 'custom-size', 220, 220, array( 'left', 'top' ) ); // Hard crop left top

切り取り位置を設定するとき、配列の最初の値はx軸の切り取り位置、2番目の値はy軸の切り取り位置です。

x_crop_positionは 'left'、 'center'、または 'right'を受け入れます。 y_crop_positionは 'top'、 'center'、または 'bottom'を受け入れます。デフォルトでは、ハードクロップモードを使用すると、これらの値はデフォルトで 'center'になります。

また、コーデックスはクロップ位置がどのように機能するかを示すページを参照します。

http://havecamerawilltravel.com/photographer/wordpress-thumbnail-crop

10
ewroman

私はコアをハッキングする必要がないこの問題の解決策を開発しました: http://bradt.ca/archives/image-crop-position-in-wordpress/ /

私はまたコアにパッチを提出しました: http://core.trac.wordpress.org/ticket/19393

チケットにCcとして自分自身を追加して、それをcoreに追加するためのサポートを表示します。

7
bradt

あなたはあなたのサムネイルのトリミング位置を選択するために サムネイルトリミング位置 を使用することができます。

3
PoseLab

ここでの代替ソリューション: http://pixert.com/blog/cropping-post-featured-thumbnails-from-top-instead-of-center-in-wordpress-with-native-cropping-tool/ /

このコードをfunctions.phpに追加してから、「サムネイルの再生成」プラグイン( https://wordpress.org/plugins/regenerate-thumbnails/ /)を使用します。

function px_image_resize_dimensions( $payload, $orig_w, $orig_h, $dest_w, $dest_h, $crop ){

// Change this to a conditional that decides whether you want to override the defaults for this image or not.
if( false )
return $payload;

if ( $crop ) {
// crop the largest possible portion of the original image that we can size to $dest_w x $dest_h
$aspect_ratio = $orig_w / $orig_h;
$new_w = min($dest_w, $orig_w);
$new_h = min($dest_h, $orig_h);

if ( !$new_w ) {
$new_w = intval($new_h * $aspect_ratio);
}

if ( !$new_h ) {
$new_h = intval($new_w / $aspect_ratio);
}

$size_ratio = max($new_w / $orig_w, $new_h / $orig_h);

$crop_w = round($new_w / $size_ratio);
$crop_h = round($new_h / $size_ratio);

$s_x = 0; // [[ formerly ]] ==> floor( ($orig_w - $crop_w) / 2 );
$s_y = 0; // [[ formerly ]] ==> floor( ($orig_h - $crop_h) / 2 );
} else {
// don't crop, just resize using $dest_w x $dest_h as a maximum bounding box
$crop_w = $orig_w;
$crop_h = $orig_h;

$s_x = 0;
$s_y = 0;

list( $new_w, $new_h ) = wp_constrain_dimensions( $orig_w, $orig_h, $dest_w, $dest_h );
}

// if the resulting image would be the same size or larger we don't want to resize it
if ( $new_w >= $orig_w && $new_h >= $orig_h )
return false;

// the return array matches the parameters to imagecopyresampled()
// int dst_x, int dst_y, int src_x, int src_y, int dst_w, int dst_h, int src_w, int src_h
return array( 0, 0, (int) $s_x, (int) $s_y, (int) $new_w, (int) $new_h, (int) $crop_w, (int) $crop_h );

}
add_filter( 'image_resize_dimensions', 'px_image_resize_dimensions', 10, 6 );
0
Niente0