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
<?php
/* -----------------------------------------------------------------------------------------------------------
* [ getimagesizeとimagecreatetruecolor→imagecreatefromjpeg→imagecopyresized→imagejpeg ] のサンプル
* 画像の大きさ(縦、横)取得して、jpegファイルのリサイズ画像を出力する処理
* --------------
* imagecopyresized($thumb,$im_rsc, 0, 0, 0, 0, $newwidth,$newheight,$width,$height);
@$thumb : 出力画像のリソース
@im_src : 入力画像のリソース
* 2016.08.14 作成 Yoshi of CXMedia Inc.
* --------------------------------------------------------------------------------------------------------- */
// 入力画像と出力ファイル名の設定
$file_in_path = 'img_sample/Base_image.jpg';
$file_out_path = 'img_sample/Resize_out.jpg';
// 入力画像の大きさを取得
list($width, $height) = getimagesize($file_in_path);
// 縮小サイズを指定
$percent = 0.5;
$newwidth = (int)($width * $percent);
$newheight = (int)($height * $percent);
// 入力画像ファイル名の横x縦サイズを表示
echo "入力ファイル:{$file_in_path} : {$width}x{$height} を $percent 倍にリサイズ<br />\n";
// 出力画像リソースの大きさ設定
$thumb = imagecreatetruecolor($newwidth, $newheight);
// 入力画像リソース:jpegファイルを読み込み
$im_rsc = imagecreatefromjpeg($file_in_path);
// リサイズ
imagecopyresized($thumb,$im_rsc, 0, 0, 0, 0, $newwidth,$newheight,$width,$height);
// 画像ファイル出力(jpegファイルに変換)
imagejpeg($thumb,$file_out_path);
// 画像リソースを削除
imagedestroy($im_rsc);
// 出力画像の大きさを取得
list($out_width, $out_height) = getimagesize($file_out_path);
// 出力画像ファイル名の横x縦サイズを表示
echo "出力ファイル:{$file_out_path} : {$out_width}x{$out_height}<br />\n";
// リサイズした画像の表示
echo "<img src=\"{$file_out_path}\" alt=\"{$file_out_path}\"><br />\n";
?>