c4cd5fe33c3833d5a9ba33e14d66d3027dbe8240
[living-lab-site.git] / application / libraries / Image.php
1 <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); 
2
3 /**
4  * An image processing library initially intended for creating thumbnails.
5  * 
6  * @category    Library
7  * @author              Simon Jarvis, Călin-Andrei Burloiu
8  * @copyright   2006 Simon Jarvis, 2011 Călin-Andrei Burloiu
9  */
10
11 class Image {
12          
13         var $image;
14         var $image_type;
15
16         function load($filename) {
17                 $image_info = getimagesize($filename);
18                 $this->image_type = $image_info[2];
19                 if( $this->image_type == IMAGETYPE_JPEG ) {
20                         $this->image = imagecreatefromjpeg($filename);
21                 } elseif( $this->image_type == IMAGETYPE_GIF ) {
22                         $this->image = imagecreatefromgif($filename);
23                 } elseif( $this->image_type == IMAGETYPE_PNG ) {
24                         $this->image = imagecreatefrompng($filename);
25                 }
26         }
27         function save($filename, $image_type=IMAGETYPE_JPEG, $compression=60, $permissions=null) {
28                 if( $image_type == IMAGETYPE_JPEG ) {
29                         imagejpeg($this->image,$filename,$compression);
30                 } elseif( $image_type == IMAGETYPE_GIF ) {
31                         imagegif($this->image,$filename);
32                 } elseif( $image_type == IMAGETYPE_PNG ) {
33                         imagepng($this->image,$filename);
34                 }
35                 if( $permissions != null) {
36                         chmod($filename,$permissions);
37                 }
38         }
39         function output($image_type=IMAGETYPE_JPEG) {
40                 if( $image_type == IMAGETYPE_JPEG ) {
41                         imagejpeg($this->image);
42                 } elseif( $image_type == IMAGETYPE_GIF ) {
43                         imagegif($this->image);
44                 } elseif( $image_type == IMAGETYPE_PNG ) {
45                         imagepng($this->image);
46                 }
47         }
48         function saveThumbnail($filename, $width, $height)
49         {
50                 $ratio = $this->getWidth() / $this->getHeight();
51                 $thumbRatio = $width / $height;
52
53                 if($ratio < $thumbRatio)
54                 $this->resizeToHeight($height);
55                 else
56                 $this->resizeToWidth($width);
57
58                 $this->save($filename);
59         }
60         function getWidth() {
61                 return imagesx($this->image);
62         }
63         function getHeight() {
64                 return imagesy($this->image);
65         }
66         function resizeToHeight($height) {
67                 $ratio = $height / $this->getHeight();
68                 $width = $this->getWidth() * $ratio;
69                 $this->resize($width,$height);
70         }
71         function resizeToWidth($width) {
72                 $ratio = $width / $this->getWidth();
73                 $height = $this->getheight() * $ratio;
74                 $this->resize($width,$height);
75         }
76         function scale($scale) {
77                 $width = $this->getWidth() * $scale/100;
78                 $height = $this->getheight() * $scale/100;
79                 $this->resize($width,$height);
80         }
81         function resize($width,$height) {
82                 $new_image = imagecreatetruecolor($width, $height);
83                 imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
84                 $this->image = $new_image;
85         }
86 }
87
88
89 /* End of file Singleton_db.php */
90 /* Location: ./application/libraries/Singleton_db.php */