1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<?php
/* -------------------------------------------------------------------------
* [ ImageMagickを利用した拡張子変換 ] のサンプル
* 画像の拡張子を判断して、jpg→png形式に変換する内容
* --------------
* writeImageFile ( $filehandle );
@$filehandle : 出力画像のリソース
* writeImage ([ $filename = NULL ] );
@$filename : 出力画像のパス(ファイル名)
* 2016.08.19 作成 Yoshi of CXMedia Inc.
* ------------------------------------------------------------------------- */
// PHPバージョンにより処理を区別するための情報取得
$php_ver_ary = explode('.', PHP_VERSION); // PHP_VERSIONは定数
$php_version = sprintf("%d%02d",$php_ver_ary[0],$php_ver_ary[1]);
// 入力画像のパス設定
$img_path = 'img_sample/Base_image.jpg';
// 入力画像パスから出力ファイル名の設定(PNG形式)
list($base_path,$img_exit) = file2path_ext_get($img_path);
$img_out = $base_path.'_cvt.png';
// ImageMagickのコンストラクタからインスタンスの作成
$img_obj = new Imagick( realpath($img_path) );
// 指定した名前で画像の出力:PHP5.3以降とPHP5.2の処理分岐
if($php_version >= '503'){
//--PHP5.3以降は、 writeImageFileで対応--
$img_obj->writeImageFile(fopen($img_out,'wb'));
} else {
// --PHP5.2は、OK →PHP5.3以降 writeImage()のエラー
$img_obj->writeImage($img_out);
}
// 変換した画像の表示処理
echo "<img src=\"$img_out\" width='720' alt='変換したPNG画像'>";
exit;
/* ------------------------------------------#
* ファイル名(パス)と拡張子を分離してget
* ----------------------------------------- */
function file2path_ext_get($str){
preg_match("/(.*)\.(.+?)$/",$str,$result);
if(isset($result[2])){return array($result[1],strtolower($result[2]));}
}
?>