まらんさんのチラ裏

その日暮らしのおじさん

PHPによる画像伸縮処理って

意外と簡単だった。
アップロードして伸縮処理とか行うやつ。gif用。
jpg用にするにはソース中の関数の名称にgifが混じってるやつをjpgに変えればおk。
かなりPHPマニュアルのパクリ。

<?php
$out_file = "/tmp/hoge.gif";
$max_width = 100;
$max_height = 80;

// 現在の画像サイズを取得
list($width,$height)=getimagesize($_FILES['photo']['tmp_name']);

// リサイズ後の画像サイズを計算
if ($max_width && ($width < $height)) {
    $max_width = ($max_height / $height) * $width;
} else {
    $max_height = ($max_width / $width) * $height;
}

$dst = imagecreatetruecolor($max_width, $max_height);
$src = @imagecreatefromgif($_FILES['photo']['tmp_name']);
imagecopyresampled($dst,$src,0,0,0,0,$max_width,$max_height,$width,$height);
imagegif($dst, $out_file);
?>

眠い。