PHPで画像をリサイズする方法【現役エンジニアが解説】
今回は、PHPで画像をリサイズする方法について、PHP側の関数を紹介し、具体的な使い方をHTML側とPHP側に分け、簡単に解説していきます。
PHP側の関数
今回はimage関数をベースにリサイズの機能をPHPで実装します。
その際に前提としてGDやExifの拡張モジュールが必要となります。
function resizeImage($file_path, $resize_width){ list($width, $height, $image_type) = getimagesize($file_path); $base_image = getImage($file_path, $image_type); if($base_image === false) return false; $base_image = adjustOrientation($base_image, $file_path); $width = imagesx($base_image); $height = imagesy($base_image); $ratio = $resize_width / $width; $new_width = $width * $ratio; $new_height = $height * $ratio; $new_image = imagecreatetruecolor($new_width, $new_height); if ($image_type == 1 || $image_type == 3) { imagealphablending($new_image, false); imagesavealpha($new_image, true); $transparent = imagecolorallocatealpha($new_image, 255, 255, 255, 127); imagefilledrectangle($new_image, 0, 0, $new_width, $new_height, $transparent); } $result = imagecopyresampled($new_image, $base_image, 0, 0, 0, 0, $new_width, $new_height, $width, $height); $result = saveImage($new_image, $image_type, $file_path); if($result === false) return false; return true; } function getImage($file_path, $image_type){ switch($image_type){ case 1: $base_image = imagecreatefromgif($file_path); break; case 2: $base_image = imagecreatefromjpeg($file_path); break; case 3: $base_image = imagecreatefrompng($file_path); break; case 6: $base_image = imagecreatefrombmp($file_path); break; default: $base_image = false; break; } return $base_image; } function adjustOrientation($image, $file_path){ $exif_data = @exif_read_data($file_path); $degrees = 0; $mode = 0; switch(@$exif_data['Orientation']){ case 2: $mode = IMG_FLIP_VERTICAL; break; case 3: $degrees = 180; break; case 4: $mode = IMG_FLIP_HORIZONTAL; break; case 5: $degrees = 270; $mode = IMG_FLIP_VERTICAL; break; case 6: $degrees = 270; break; case 7: $degrees = 90; $mode = IMG_FLIP_VERTICAL; break; case 8: $degrees = 90; break; } if ($degrees > 0) $image = imagerotate($image, $degrees, 0); if ($mode > 0) imageflip($image, $mode); return $image; } function saveImage($image, $image_type, $file_path){ switch($image_type){ case 1: $result = imagegif($image, $file_path); break; case 2: $result = imagejpeg($image, $file_path); break; case 3: $result = imagepng($image, $file_path); break; case 6: $result = imagebmp($image, $file_path); break; default: $result = false; break; } return $result; }
上記のコードでは、回転や透過対応も含め、gifとjepg、pngとbmpの4つの画像のリサイズを行うことができます。
HTML側から画像をPOST送信する方法
HTML側から画像をPHP側に送る方法は非常にシンプルです。
form要素を使って、リサイズを行うphpファイルに向けてPOST送信するだけです。
<form action="resizeandupload.php" method="POST" enctype="multipart/form-data"> <input type="file" name="image_file"> <input type="submit" value="送信"> </form>
上記のコードは、resizeandupload.phpに対し、image_fileの名前で画像をPOST送信しています。
POST送信された画像をPHP側で受け取りリサイズする方法
次にHTML側から送られた画像をPHP側で受け取ってリサイズします。
最初に紹介した関数を用いて、PHP側でPOST送信された画像をリサイズします。
$file_path = $_FILES['image_file']['tmp_name']; $resize_width = 480; if(!resizeImage($file_path, $resize_width)){ echo "リサイズがエラーが発生しました。"; } $save_path = ""; move_uploaded_file($file_path, $save_path.$_FILES['image_file']['name']);
上記のコードの例では、image_fileの名前でPOST送信された画像を幅480pxにリサイズし、任意の保存先に保存しています。