LoginSignup
0
1

More than 3 years have passed since last update.

使おうぜ php 画像加工 Intervention\Image\Facades\Image;

Last updated at Posted at 2020-10-14

とりあえず、表題の通り、phpを使って画像を加工したい。
しかし、できることが多すぎるので、よく使う機能だけをピックアップしていく。

下準備

インストール方法は、省く

HogeController.php

use Intervention\Image\Facades\Image;

背景に画像を重ねる

コロまるアイコンを背景画像に重ねる。
背景画像は whitebg.png (600x381) として保存しておく。

HogeController.php

//コロまる アイコン
$icon = 'https://pbs.twimg.com/profile_images/378800000552767745/fb8f309e621d078cfe46250d73d38047_normal.png';

//600 x 381 pixels
//https://twi.ski/img/whitebg.png
$bg = Image::make(public_path('/img/whitebg.png'));
$icon = Image::make($icon);

//bg に 対し真ん中に 重ねる
$bg->insert($icon,'center');

//nakayoshi.png として保存
$bg
->save(public_path('/tmp/nakayoshi.png'));

echo '<img src="/tmp/nakayoshi.png?'.time().'">';

nakayoshi.png

うーん♪キュート。

丸く切り抜き (マスク)

HogeContoller.php

$bg = Image::make(public_path('/img/blackbg.png'));

//コロまる アイコン 200 x 200 のサイズ
$icon = 'https://pbs.twimg.com/profile_images/378800000552767745/fb8f309e621d078cfe46250d73d38047_200x200.png';
$icon = Image::make($icon);
//背景は透過、真っ黒の円を 200 x 200 で作成
$mask = Image::make(public_path('img/mask200.png'));
$icon->mask($mask,true);
$bg->insert($icon, 'center');
//       nakayoshi.png として保存
$bg
->save(public_path('/tmp/nakayoshi.png'));

echo '<img src="/tmp/nakayoshi.png?'.time().'">';


nakayoshi (1).png

うーん!セクシー!

テキストを重ねよう

日本語ファイルを用意する必要がある。
今回は uzura フォント を使う
http://azukifont.com/font/uzura.html

HogeContoller.php

$x = 300;
$y = 190;

$font_size = 30;

$bg->text($key, $x, $y, function($font) use ($font_size) {

    $font->file(public_path('font/uzura.ttf')); // フォントファイル
    $font->size($font_size);        // 文字サイズ
    $font->color('#000');   // 文字の色
    $font->align('center'); // 横の揃え方(left, center, right)
    $font->valign('top');   // 縦の揃え方(top, middle, bottom)
    $font->angle(30);       // 回転(フォントが指定されていないと無視されます。)

});

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1