2017-06-04 9 views
3

私は好きな正方形のサイズを持っていて、この場合は幅と高さが2236 pxです。Laravel 5の介入画像を使用して画像に空白を追加して正方形の画像を作成する

php intervention packageを使用して、このサイズの画像をサーバーに保存する必要があります。

それは、ユーザーの画像サイズですが、ポイントは画像が新しいサイズが、ユーザーの画像を保存しなければならないことは何か絵がより小さければ、正方形の中心部と中央部でなければならないとすることが重要ではありません私の好きな大きさ、それはストレッチする必要があり、画像が大きい場合、それは私の次元に圧縮する必要があります

この絵を見てみてください。 my plan

そして、これらは、いくつかの実際の例です: example 1 example 2

は、このような状況では誰の経験をして、私はそれをどのように行うことができます知っていますか?

アドバンスに感謝

+0

http://image.intervention.io/api/resizeCanvas – mkaatman

答えて

1

まあ、彼のヒントの@Antonのおかげで、私は私の問題を解決するためにこれをした:

画像は横長方形、縦長の長方形または正方形です。

は、私はすべての状況に次の行のコードを書いて、それが私の場合のために素晴らしい作品

$img = Image::make($image->getRealPath()); 

    $width = $img->width(); 
    $height = $img->height(); 


    /* 
    * canvas 
    */ 
    $dimension = 2362; 

    $vertical = (($width < $height) ? true : false); 
    $horizontal = (($width > $height) ? true : false); 
    $square  = (($width = $height) ? true : false); 


    if ($vertical) { 
     $top = $bottom = 245; 
     $newHeight = ($dimension) - ($bottom + $top); 
     $img->resize(null, $newHeight, function ($constraint) { 
      $constraint->aspectRatio(); 
     }); 

    } else if ($horizontal) { 
     $right = $left = 245; 
     $newWidth = ($dimension) - ($right + $left); 
     $img->resize($newWidth, null, function ($constraint) { 
      $constraint->aspectRatio(); 
     }); 

    } else if ($square) { 
     $right = $left = 245; 
     $newWidth = ($dimension) - ($left + $right); 
     $img->resize($newWidth, null, function ($constraint) { 
      $constraint->aspectRatio(); 
     }); 

    } 

    $img->resizeCanvas($dimension, $dimension, 'center', false, '#ffffff'); 
    $img->save(public_path("storage/{$token}/{$origFilename}")); 
    /* 
    * canvas 
    */ 
1
<?php 
$width = 2236; 
$height = 2236; 

$img = Image::make('image.jpg'); 

// we need to resize image, otherwise it will be cropped 
if ($img->width() > $width) { 
    $img->resize($width, null, function ($constraint) { 
     $constraint->aspectRatio(); 
    }); 
} 

if ($img->height() > $height) { 
    $img->resize(null, $height, function ($constraint) { 
     $constraint->aspectRatio(); 
    }); 
} 

$img->resizeCanvas($width, $height, 'center', false, '#ffffff'); 
$img->save('out.jpg'); 
+0

お返事のおかげで、しかし、このスクリプトはしないでください元の画像サイズを変更するだけで、元の画像を2236ピクセルの中心に置きます。 ''もし画像の幅が '$ width'より小さければどうでしょうか? – Kiyarash

+0

@Kiyarashと画像が小さすぎる場合は、サイズに何が起こるはずですか?あなたはそれをストレッチしたいですか? – Anton

+0

質問のサンプル画像をクリックしてください。 – Kiyarash

関連する問題