前の記事 ≪:2008年7月14日 管理人のブックマーク
次の記事 ≫:iPhone用Webページでオブジェクトのドラッグ&ドロップ、リサイズをジ...

PHPで画像を扱うサンプルコード色々

2008年07月15日-はてなブックマーク

スポンサード リンク
[PR] 英単語を忘却曲線アプリを使って超効率よく記憶する方法

Welcome? php|architect's C7Y, The PHP Community Website

PHPで画像を扱うサンプルコードが色々紹介されてます。

(1) ご存知、画像のサイズを得るサンプル

$information = getimagesize($path_to_image);

(2) Imagick(ImageMagick)を使って更に情報を得る

<?php
$image = new Imagick();
$image->pingImage($path_to_image);
$information = $image->identifyImage();
?>

(3) EXIF情報を読む

$exif_information = exif_read_data($path_to_image, 'EXIF');

(4) EXIF情報をImagickで読む

<?php
$im = new Imagick($path_to_image);
$ exif_information = $im->getImageProperties('exif:*');
?>

(5) 各種フォーマット画像をJpegに変換

<?php
$image_information = getimagesize($path_to_image);
/* Load image into buffer */
switch ($image_information[2])
{
    case IMAGETYPE_GIF: 
        $image = imagecreatefromgif($path_to_image);
        break;
    case IMAGETYPE_JPEG: 
        $image = imagecreatefromjpeg($path_to_image); 
        break;
    case IMAGETYPE_PNG 
        $image = imagecreatefrompng($path_to_image); 
        break;
    default: 
        echo “Unsupported Image Type”; 
        exit;
}
imagejpeg($image, $path_to_destination_image);
imagedestroy($image);
?>

最後のimagejpegをimagepngにすれば、PNGに変換できますね

(6) 画像のリサイズ

<?php
$image = imagecreatefromjpeg($path_to_image);
$max_width = 100;
$ratio = $max_width / imagesx($image);
$max_height = imagesy($image) * $ratio;
$thumbnail = imagecreatetruecolor($max_width, $max_height);
imagecopyresampled($thumbnail, $ image, 0, 0, 0, 0,
$max_width, $max_height, imagesx($image), imagesy($image));
imagejpeg($thumbnail, $path_to_thumbnail);
imagedestroy($thumbnail);
?>

(7) Imagick でフォーマット変換

<?php
$imagick = new Imagick($path_to_image);
$imagick->setImageFomat(‘jpeg');
$imagick->writeImage($path_to_destination_image);
?>

(8) Imagick で画像のリサイズ

<?php
$imagick = new Imagick($path_to_image);
$imagick->thumbnailImage(100, null);
$imagick->writeImage($path_to_thumbnail);
?>

Imagickを使えばコードが非常にシンプルになっていいですね。

関連エントリ

関連の記事検索:PHP, 画像, image, PHPSPOT開発日誌
スポンサード リンク

By.KJ : 2008年07月15日 07:08 livedoor Readerで購読 Twitterに投稿

間違いの指摘をしていただける方はメール、あるいはTwitter/FBでお願いします(クリック)