remove old readme
[atutor.git] / mods / _standard / photos / include / classes / SimpleImage.class.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 ?>