end0tknr's kipple - web写経開発

太宰府天満宮の狛犬って、妙にカワイイ

convert HEIC/HEIF image file to jpeg by libheif & GD & php

最近の iphone(ios)では、画像ファイルに HEIC/HEIF を使用するようですが、 windows10では、これに対応していません。

なので、rhel8 に libheif をインストールし、 GD & phpjpeg化しました。

install libheif & libde265

libheif は、rhel8 の標準パッケージ(yum install)として 提供されていないらしく、sourceからinstallを行います。

また、libheif は libde265 に依存するようですので、併せて installします。

$ wget https://github.com/strukturag/libde265/archive/refs/tags/v1.0.8.tar.gz
$ ./autogen.sh
$ ./configure
$ make
$ sudo make install

$ wget https://github.com/strukturag/libheif/archive/refs/tags/v1.12.0.tar.gz

## 単純な configure ; make では、libde265 を認識しない為
## 以下の 環境変数を設定

$ export libde265_CFLAGS="-I/usr/local/include"
$ export libde265_LIBS="-L/usr/local/lib -lde265 -lstdc++"

$ ./autogen.sh
$ ./configure
$ make
$ sudo make install

## すると、以下の 4コマンドがインストールされます。

$ ls -l /usr/local/bin/heif-*
-rwxr-xr-x 1 404088 Feb 15 /usr/local/bin/heif-convert
-rwxr-xr-x 1 460576 Feb 15 /usr/local/bin/heif-enc
-rwxr-xr-x 1 172840 Feb 15 /usr/local/bin/heif-info
-rwxr-xr-x 1 232872 Feb 15 /usr/local/bin/heif-thumbnailer

convert heic->jpeg by heif-convert

libheifに付属する heif-convert コマンドでの heic->jpeg 変換も可能です。

$ /usr/local/bin/heif-convert IMG_0829.HEIC IMG_0829.JPG
File contains 1 images
Written to IMG_0829.JPG

convert heic->jpeg by GD & php

$ sudo yum install php-gd

$ php -r 'phpinfo();' | grep GD
GD Support => enabled
GD headers Version => 2.2.5
GD library Version => 2.2.5
<?php

$org_file = 'IMG_0829.JPG';
//$org_file = 'IMG_0829.HEIC';

echo filesize($org_file) , " bytes\n";

$org_img_info = getimagesize($org_file);
echo print_r($org_img_info,true);

$org_image = imagecreatefromjpeg($org_file);
$new_image = imagecreatetruecolor(
    $org_img_info[0] * 0.8,
    $org_img_info[1] * 0.8 );

imagecopyresampled(
    $new_image,
    $org_image,
    0,0, // copy先の座標
    0,0, // copy元の座標
    $org_img_info[0] * 0.8, // copy先のsize
    $org_img_info[1] * 0.8, //  〃
    $org_img_info[0],       // copy元のsize
    $org_img_info[1]);      //  〃

imagejpeg($new_image , 'NEW.JPG');