読者です 読者をやめる 読者になる 読者になる

PHP7でGDを使う

ネットサーフィンしていたらPHPで画像を表示を試してるHPがあったので自分も試してみた

毎回、yumの仕方を探してる・・・

また、あちこちさがさないで済むようにメモしておく

GDのインストール

[vagrant@localhost ~]$ sudo yum -y install php-gd.x86_64 --enablerepo=remi-php70
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: ftp.riken.jp
 * epel: ftp.riken.jp
 * extras: ftp.riken.jp
 * remi-php70: mirrors.mediatemple.net
 * remi-safe: mirrors.mediatemple.net
 * updates: ftp.riken.jp
Resolving Dependencies
--> Running transaction check
---> Package php-gd.x86_64 0:7.0.18-1.el7.remi will be installed
--> Processing Dependency: gd-last(x86-64) >= 2.1.1 for package: php-gd-7.0.18-1.el7.remi.x86_64
--> Processing Dependency: libgd.so.3()(64bit) for package: php-gd-7.0.18-1.el7.remi.x86_64
--> Running transaction check
---> Package gd-last.x86_64 0:2.2.4-1.el7.remi will be installed
--> Finished Dependency Resolution

Dependencies Resolved

==============================================================================================================================================================
 Package                           Arch                             Version                                        Repository                            Size
==============================================================================================================================================================
Installing:
 php-gd                            x86_64                           7.0.18-1.el7.remi                              remi-php70                            69 k
Installing for dependencies:
 gd-last                           x86_64                           2.2.4-1.el7.remi                               remi-safe                            131 k

Transaction Summary
==============================================================================================================================================================
Install  1 Package (+1 Dependent package)

Total download size: 200 k
Installed size: 606 k
Downloading packages:
(1/2): php-gd-7.0.18-1.el7.remi.x86_64.rpm                                                                                             |  69 kB  00:00:00
(2/2): gd-last-2.2.4-1.el7.remi.x86_64.rpm                                                                                             | 131 kB  00:00:00
--------------------------------------------------------------------------------------------------------------------------------------------------------------
Total                                                                                                                         301 kB/s | 200 kB  00:00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : gd-last-2.2.4-1.el7.remi.x86_64                                                                                                            1/2
  Installing : php-gd-7.0.18-1.el7.remi.x86_64                                                                                                            2/2
  Verifying  : php-gd-7.0.18-1.el7.remi.x86_64                                                                                                            1/2
  Verifying  : gd-last-2.2.4-1.el7.remi.x86_64                                                                                                            2/2

Installed:
  php-gd.x86_64 0:7.0.18-1.el7.remi

Dependency Installed:
  gd-last.x86_64 0:2.2.4-1.el7.remi

Complete!

インストールしたらhttpdを再起動させる

[vagrant@localhost ~]$sudo systemctl restart httpd

GDのインストールの確認

[vagrant@localhost ~]$ yum list installed | grep gd
gd-last.x86_64                   2.2.4-1.el7.remi               @remi-safe
gdbm.x86_64                      1.10-8.el7                     @anaconda
php-gd.x86_64                    7.0.18-1.el7.remi              @remi-php70

phpinfoでの確認

GD SupportがenableになっていればOK

f:id:nyan4:20170424195247p:plain

簡単なサンプルを試す

imagecreatetruecolorで高さ、幅を指定してイメージの作成をする

imagecolorallocateで上で作ったイメージと色を指定して色IDを取得する

ImageFilledRectangleで指定した範囲を色IDで塗りつぶす

<?php
    $img = imagecreatetruecolor(100,100);

    $white = imagecolorallocate($img, 0xF0, 0xF0, 0xF0);
    ImageFilledRectangle($img, 10,10, 20,20, $white);

    header('Content-type: image/png');
    imagepng($img);
    imagedestroy($img);
?>

うまく表示できれば黒色の四角の中に10,10の白色の四角が表示される