video thumbnails are displayed as slideshow when mouse is over; video widget bugs...
[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 define('IMAGETYPE_AUTO', 0);
12
13 class Image {
14          
15         var $image;
16         var $image_type;
17
18         function load($filename) {
19                 $image_info = getimagesize($filename);
20                 $this->image_type = $image_info[2];
21                 if( $this->image_type == IMAGETYPE_JPEG ) {
22                         $this->image = imagecreatefromjpeg($filename);
23                 } elseif( $this->image_type == IMAGETYPE_GIF ) {
24                         $this->image = imagecreatefromgif($filename);
25                 } elseif( $this->image_type == IMAGETYPE_PNG ) {
26                         $this->image = imagecreatefrompng($filename);
27                 }
28         }
29         function save($filename, $image_type=IMAGETYPE_JPEG, $compression=60, $permissions=null) {
30                 if( $image_type == IMAGETYPE_AUTO) {
31                         if (preg_match('/\.jpg$/', $filename) 
32                                         || preg_match('/\.jpeg$/', $filename))
33                                 $image_type = IMAGETYPE_JPEG;
34                         elseif (preg_match('/\.gif$/', $filename))
35                                 $image_type = IMAGETYPE_GIF;
36                         elseif (preg_match('/\.png$/', $filename))
37                                 $image_type = IMAGETYPE_PNG;
38                 }
39                 
40                 if( $image_type == IMAGETYPE_JPEG ) {
41                         imagejpeg($this->image,$filename,$compression);
42                 } elseif( $image_type == IMAGETYPE_GIF ) {
43                         imagegif($this->image,$filename);
44                 } elseif( $image_type == IMAGETYPE_PNG ) {
45                         imagepng($this->image,$filename);
46                 }
47                 
48                 if( $permissions != null) {
49                         chmod($filename,$permissions);
50                 }
51         }
52         function output($image_type=IMAGETYPE_JPEG) {
53                 if( $image_type == IMAGETYPE_JPEG ) {
54                         imagejpeg($this->image);
55                 } elseif( $image_type == IMAGETYPE_GIF ) {
56                         imagegif($this->image);
57                 } elseif( $image_type == IMAGETYPE_PNG ) {
58                         imagepng($this->image);
59                 }
60         }
61         function save_thumbnail($filename, $width, $height,
62                         $image_type=IMAGETYPE_JPEG)
63         {
64                 $ratio = $this->get_width() / $this->get_height();
65                 $thumbRatio = $width / $height;
66
67                 if($ratio < $thumbRatio)
68                         $this->resize_to_height($height);
69                 else
70                         $this->resize_to_width($width);
71
72                 $this->save($filename, $image_type);
73         }
74         function get_width() {
75                 return imagesx($this->image);
76         }
77         function get_height() {
78                 return imagesy($this->image);
79         }
80         function resize_to_height($height) {
81                 $ratio = $height / $this->get_height();
82                 $width = $this->get_width() * $ratio;
83                 $this->resize($width,$height);
84         }
85         function resize_to_width($width) {
86                 $ratio = $width / $this->get_width();
87                 $height = $this->get_height() * $ratio;
88                 $this->resize($width,$height);
89         }
90         function scale($scale) {
91                 $width = $this->get_width() * $scale/100;
92                 $height = $this->get_height() * $scale/100;
93                 $this->resize($width,$height);
94         }
95         function resize($width,$height) {
96                 $new_image = imagecreatetruecolor($width, $height);
97                 imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->get_width(), $this->get_height());
98                 $this->image = $new_image;
99         }
100 }
101
102
103 /* End of file Singleton_db.php */
104 /* Location: ./application/libraries/Singleton_db.php */