3 * File: SimpleImage.php
5 * Copyright: 2006 Simon Jarvis
7 * Link: http://www.white-hat-web-design.co.uk/articles/php-image-resizing.php
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.
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
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);
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);
46 if( $permissions != null) {
47 chmod($filename,$permissions);
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);
60 return imagesx($this->image);
62 function getHeight() {
63 return imagesy($this->image);
65 function resizeToHeight($height) {
66 $ratio = $height / $this->getHeight();
67 $width = $this->getWidth() * $ratio;
68 $this->resize($width,$height);
70 function resizeToWidth($width) {
71 $ratio = $width / $this->getWidth();
72 $height = $this->getheight() * $ratio;
73 $this->resize($width,$height);
75 function scale($scale) {
76 $width = $this->getWidth() * $scale/100;
77 $height = $this->getheight() * $scale/100;
78 $this->resize($width,$height);
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;