2008年12月18日
GDとImageMagickの画質比較
こんばんは、牧野です。
以前、GDでの画像処理を紹介しました。
今日はPHPでよく使われるもう1つの画像処理方法、ImageMagickを使うやり方で画質にどれくらい差があるのか比べてみました。
まずは早速、テスト用プログラムです。
gd_test.php
<?php
$image_file = 'sample.jpg';
$img = new MyImage($image_file);
$img->resizeImage(500, 500);
$img->createImageFile('.', 'gd_sample.jpg');
header('Content-type: image/jpeg');
readfile('gd_sample.jpg');
class MyImage {
var $img;
var $width;
var $height;
var $font_path;
function MyImage($image_path, $font_file_path = null) {
$this->img = imagecreatefromjpeg($image_path);
$this->width = imagesx($this->img);
$this->height = imagesy($this->img);
}
function resizeImage($max_w, $max_h) {
if (floatval($max_w / $this->width - 1) <= floatval($max_h / $this->height - 1)) {
$rate = floatval($max_w / $this->width);
$new_w = $max_w;
$new_h = (int)min(intval($this->height * $rate), $max_h);
} else {
$rate = floatval($max_h / $this->height);
$new_w = (int)min(intval($this->width * $rate), $max_w);
$new_h = $max_h;
}
$new_img = imagecreatetruecolor($new_w, $new_h);
if (imagecopyresampled($new_img, $this->img, 0, 0, 0, 0, $new_w, $new_h, $this->width, $this->height)) {
$this->img = $new_img;
return true;
} else {
return false;
}
}
function createImageFile($new_file_dir, $new_file_name) {
$new_file_path = $new_file_dir .'/'. $new_file_name;
$result = imagejpeg($this->img, $new_file_path, 100);
chmod($new_file_path,