1 <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
4 * An image processing library initially intended for creating thumbnails.
7 * @author Simon Jarvis, Călin-Andrei Burloiu
8 * @copyright 2006 Simon Jarvis, 2011 Călin-Andrei Burloiu
11 define('IMAGETYPE_AUTO', 0);
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);
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;
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);
48 if( $permissions != null) {
49 chmod($filename,$permissions);
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);
61 function save_thumbnail($filename, $width, $height,
62 $image_type=IMAGETYPE_JPEG)
64 $ratio = $this->get_width() / $this->get_height();
65 $thumbRatio = $width / $height;
67 if($ratio < $thumbRatio)
68 $this->resize_to_height($height);
70 $this->resize_to_width($width);
72 $this->save($filename, $image_type);
74 function get_width() {
75 return imagesx($this->image);
77 function get_height() {
78 return imagesy($this->image);
80 function resize_to_height($height) {
81 $ratio = $height / $this->get_height();
82 $width = $this->get_width() * $ratio;
83 $this->resize($width,$height);
85 function resize_to_width($width) {
86 $ratio = $width / $this->get_width();
87 $height = $this->get_height() * $ratio;
88 $this->resize($width,$height);
90 function scale($scale) {
91 $width = $this->get_width() * $scale/100;
92 $height = $this->get_height() * $scale/100;
93 $this->resize($width,$height);
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;
103 /* End of file Singleton_db.php */
104 /* Location: ./application/libraries/Singleton_db.php */