小さい画像を生成するのは、一般的なイメージは画像を編集する専用ソフトウェアを使って作成しますね。Webアプリケーションの場合それはもちろん無理ですよね。Web系には、ほぼ全てをPHPスクリプトが自動で行ってくれますので、ブラウザからアップロードできるのでサイトを更新する時に画像を使いたい人にはとっても便利なんです。特にメンドクサイ画像エディタを使って「元の画像」「サムネイル画像」両方をFTPでアップして・・・とかなんてのは必要ないし、 WordPressとか、MovableTypeなどのブログを使ってる人もこっちの方が全然使い勝手がいいです。PHPにはその機能を簡単で実現できますか。

回答はもちろんです。ネット中探して下記のようなPHPで画像のサムネイルを超簡単に作る方法を見つかりました(実はサムネイルの生成のやり方はいろいろあると思います)。

ここからは画像のサムネイルを生成するPHPソースコード:

Download: SimpleImage.php
  1. <?php
  2. /*
  3. * File: SimpleImage.php
  4. * Author: Simon Jarvis
  5. * Copyright: 2006 Simon Jarvis
  6. * Date: 08/11/06
  7. * Link: http://www.white-hat-web-design.co.uk/articles/php-image-resizing.php
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License
  11. * as published by the Free Software Foundation; either version 2
  12. * of the License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details:
  18. * http://www.gnu.org/licenses/gpl.html
  19. *
  20. */
  21.  
  22. class SimpleImage {
  23.   
  24.    var $image;
  25.    var $image_type;
  26.  
  27.    function load($filename) {
  28.       $image_info = getimagesize($filename);
  29.       $this->image_type = $image_info[2];
  30.       if( $this->image_type == IMAGETYPE_JPEG ) {
  31.          $this->image = imagecreatefromjpeg($filename);
  32.       } elseif( $this->image_type == IMAGETYPE_GIF ) {
  33.          $this->image = imagecreatefromgif($filename);
  34.       } elseif( $this->image_type == IMAGETYPE_PNG ) {
  35.          $this->image = imagecreatefrompng($filename);
  36.       }
  37.    }
  38.    function save($filename, $image_type=IMAGETYPE_JPEG, $compression=75, $permissions=null) {
  39.       if( $image_type == IMAGETYPE_JPEG ) {
  40.          imagejpeg($this->image,$filename,$compression);
  41.       } elseif( $image_type == IMAGETYPE_GIF ) {
  42.          imagegif($this->image,$filename);        
  43.       } elseif( $image_type == IMAGETYPE_PNG ) {
  44.          imagepng($this->image,$filename);
  45.       }  
  46.       if( $permissions != null) {
  47.          chmod($filename,$permissions);
  48.       }
  49.    }
  50.    function output($image_type=IMAGETYPE_JPEG) {
  51.       if( $image_type == IMAGETYPE_JPEG ) {
  52.          imagejpeg($this->image);
  53.       } elseif( $image_type == IMAGETYPE_GIF ) {
  54.          imagegif($this->image);        
  55.       } elseif( $image_type == IMAGETYPE_PNG ) {
  56.          imagepng($this->image);
  57.       }  
  58.    }
  59.    function getWidth() {
  60.       return imagesx($this->image);
  61.    }
  62.    function getHeight() {
  63.       return imagesy($this->image);
  64.    }
  65.    function resizeToHeight($height) {
  66.       $ratio = $height / $this->getHeight();
  67.       $width = $this->getWidth() * $ratio;
  68.       $this->resize($width,$height);
  69.    }
  70.    function resizeToWidth($width) {
  71.       $ratio = $width / $this->getWidth();
  72.       $height = $this->getheight() * $ratio;
  73.       $this->resize($width,$height);
  74.    }
  75.    function scale($scale) {
  76.       $width = $this->getWidth() * $scale/100;
  77.       $height = $this->getheight() * $scale/100;
  78.       $this->resize($width,$height);
  79.    }
  80.    function resize($width,$height) {
  81.       $new_image = imagecreatetruecolor($width, $height);
  82.       imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
  83.       $this->image = $new_image;  
  84.    }     
  85. }
  86. ?>

以下は上記のソースの使用例:

  1. <?php
  2.    include('SimpleImage.php');
  3.    $image = new SimpleImage();
  4.    $image->load('picture.jpg');
  5.    $image->resize(250,400);
  6.    $image->save('picture2.jpg');
  7. ?>

とても簡単ですよね。

リソース:

http://www.white-hat-web-design.co.uk/articles/php-image-resizing.php

メインコンテンツEND ■
1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading ... Loading ...

Posted on Wednesday, 14th January 2009 by admin

Tags: , , , , ,
Posted in PHP | Comments (0) | 5,300 views

Leave a Reply