phpThumbnailerで背景を埋める処理
Index of /projects/phpThumbnailer
というPHPで画像のリサイズをしてくれるライブラリがあって、これが1ファイルでできている上にリサイズするだけなら
-
<?
-
include("../class.Thumbnail.php");
-
$tn_image = new Thumbnail("sample.jpg", 0, 0, 25);
-
$tn_image->save("tn_sample.jpg");
-
?>
というシンプルさなのでとても重宝しています。
使い方とか、キャッシュをする方法は以前書いたのでこちらをご覧下さい。
Webアプリには必要十分なのですが唯一つ、画像のリサイズで空白を埋めるというのをやってほしかったので拡張をしました。
たとえばw100xh50の画像を50x50にリサイズしたときに、結果として50x25の画像を受け取るのではなく、50x50の画像ができあがって、縦の足りない部分は背景色を表示するというような感じです。
thumbnail.inc.phpに下記のメソッドを追加して使ってください。
-
/**
-
* Resizes image to maxWidth x maxHeight
-
*
-
* @param int $maxWidth
-
* @param int $maxHeight
-
* @param string $color
-
*/
-
function resizeFar($maxWidth = 0, $maxHeight = 0, $color = "FFFFFF") {
-
$this->resize($maxWidth, $maxHeight);
-
-
if($maxWidth> $this->currentDimensions['width'] || $maxHeight> $this->currentDimensions['height']){
-
$this->workingImage = ImageCreateTrueColor($maxWidth, $maxHeight);
-
}else {
-
$this->workingImage = ImageCreate($maxWidth, $maxHeight);
-
}
-
imageFill($this->workingImage,0,0,$color);
-
-
ImageCopy(
-
$this->workingImage,
-
$this->newImage,
-
($maxWidth - $this->currentDimensions['width']) / 2,
-
($maxHeight - $this->currentDimensions['height']) / 2,
-
0,
-
0,
-
$this->currentDimensions['width'],
-
$this->currentDimensions['height']
-
);
-
$this->newImage = $this->workingImage;
-
$this->currentDimensions['width'] = $maxWidth;
-
$this->currentDimensions['height'] = $maxHeight;
-
}
-
}





