Skip to content

Instantly share code, notes, and snippets.

@mia-0032
Created March 20, 2013 16:16
Show Gist options
  • Save mia-0032/5205976 to your computer and use it in GitHub Desktop.
Save mia-0032/5205976 to your computer and use it in GitHub Desktop.
画像の縦横比を保ったまま、必要に応じて黒帯をつけて表示するプログラム。
<?php
class ImageResizer {
public function resize($path, $width, $height) {
if (is_int($width) === FALSE || is_int($height) === FALSE) {
throw new Exception('$widthまたは$heightが不正');
}
if (is_string($path) && is_file($path)) {
$this->resizeImage($path, $width, $height);
}
}
/**
* 指定した大きさの画像を出力する。縦横比は保ったまま必要に応じて黒帯をつける。
* PNG,JPEG,GIFのみ対応。
* @param string $path
* @param int $width
* @param int $height
*/
private function resizeImage($path, $width, $height) {
$originalImageInfo = $this->getOriginalImage($path);
$shrunkenParams = $this->calculateShrunkenParams($width, $height, $originalImageInfo[0], $originalImageInfo[1]);
$canvas = imagecreatetruecolor($width, $height);
if ($canvas === FALSE) {
throw new Exception('$canvasの生成に失敗');
}
$color = imagecolorallocate($canvas, 0, 0, 0);
if ($color === FALSE) {
throw new Exception('$colorの生成に失敗');
}
$success = imagefill($canvas, 0, 0, $color);
if ($success === FALSE) {
throw new Exception('imagefill()に失敗');
}
$success = imagecopyresampled($canvas, $originalImageInfo['resource'], $shrunkenParams['x'], $shrunkenParams['y'], 0, 0, $shrunkenParams['width'], $shrunkenParams['height'], $originalImageInfo[0], $originalImageInfo[1]);
if ($success === FALSE) {
throw new Exception('imagecopyresampled()に失敗');
}
header("Content-type: image/jpeg");
imagejpeg($canvas, NULL, 90);
//メモリ開放処理
imagedestroy($canvas);
imagedestroy($originalImageInfo['resource']);
}
/**
* pathから画像リソースを生成しかえす。
* @param string $path
* @return array 画像リソース
*/
private function getOriginalImage($path) {
$imageInfo = getimagesize($path);
$originalImage = FALSE;
if (is_array($imageInfo) === FALSE) {
throw new Exception('指定されたパスは画像ファイルではない');
}
switch ($imageInfo[2]) {
case IMAGETYPE_GIF:
$originalImage = imagecreatefromgif($path);
break;
case IMAGETYPE_JPEG:
$originalImage = imagecreatefromjpeg($path);
break;
case IMAGETYPE_PNG:
$originalImage = imagecreatefrompng($path);
break;
default :
throw new Exception('縮小版に対応していない形式');
}
if ($originalImage === FALSE) {
throw new Exception('画像の取得に失敗');
}
$imageInfo['resource'] = $originalImage;
return $imageInfo;
}
/**
* 縮小した画像の座標と幅、高さを取得する
* @param int $targetWidth 変形後の幅
* @param int $targetHeight 変形後の高さ
* @param int $currentWidth 今の幅
* @param int $currentHeight 今の高さ
* @return array
*/
private function calculateShrunkenParams($targetWidth, $targetHeight, $currentWidth, $currentHeight) {
$shrunkenWidth = $targetWidth;
$shrunkenHeight = $currentHeight * ($targetWidth / $currentWidth);
if ($shrunkenHeight > $targetHeight) {
$shrunkenWidth = $currentWidth * ($targetHeight / $currentHeight);
$shrunkenHeight = $targetHeight;
}
//描画開始時点の座標
$x = ($targetWidth - $shrunkenWidth) / 2;
$y = ($targetHeight - $shrunkenHeight) / 2;
return array('x' => $x, 'y' => $y, 'width' => $shrunkenWidth, 'height' => $shrunkenHeight);
}
}
$resizer = new ImageResizer();
$resizer->resize('./hoge.jpg', 50, 200);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment