-
Notifications
You must be signed in to change notification settings - Fork 0
/
Image.php
222 lines (197 loc) · 5.7 KB
/
Image.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
<?php
/**
* Digitalus CMS
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://digitalus-media.com/license/new-bsd
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to info@digitalus-media.com so we can send you a copy immediately.
*
* @category Digitalus CMS
* @package Digitalus_Core_Library
* @copyright Copyright (c) 2007 - 2008, Digitalus Media USA (digitalus-media.com)
* @license http://digitalus-media.com/license/new-bsd New BSD License
* @version $Id: Image.php Tue Dec 25 21:12:41 EST 2007 21:12:41 forrest lyman $
*/
class Digitalus_Resource_Image extends Digitalus_Resource
{
/**
* the image path (within the resources directory)
*
*/
const IMAGE_PATH = 'images';
/**
* the default thumbnail width
*
*/
const THUMB_WIDTH = 120;
/**
* the default full size image width
*
*/
const FULL_WIDTH = 400;
/**
* the image source
*
* @var binary image
*/
protected $_image;
/**
* path to the source file
*
* @var string
*/
protected $_srcPath;
/**
* valid image mime types
*/
protected $_fileTypes = array(
'image/gif',
'image/jpeg',
'image/png'
);
/**
* the image compression settings. php5 / gd changed the png range from 1-100 to 1-10
*
* @var array
*/
protected $_imageQuality = array(
"png" => 9,
'jpeg' => 90,
'gif' => 90
);
/**
* the relative filepath to the full sized image
*
* @var string
*/
public $fullPath;
/**
* the relative filepath to the thumbnail
*
* @var string
*/
public $thumbPath;
/**
* the file type
*
* @var string
*/
public $fileType;
/**
* the image caption
*
* @var string
*/
public $caption;
/**
* uploads the selected file (the key is the name of the files control)
* if successfull then it resizes the images, creating a thumbnail and a full size image
* if then removes the source file
*
* @param string $key
*/
public function upload($key, $subdir = null, $resize = true, $makeThumb = true, $thumbWidth = null, $fullWidth = null)
{
//try to upload the file
if ($subdir !== null) {
$path = self::IMAGE_PATH . '/' . $subdir;
} else {
$path = self::IMAGE_PATH;
}
$upload = parent::upload($key, $path);
if ($upload) {
if ($resize == true) {
//make the full sized image
if ($fullWidth == null) {
$fullWidth = self::FULL_WIDTH;
}
$this->fullPath = $this->resize($upload, $fullWidth, 'full_');
} else {
$this->fullPath = $upload;
}
if ($makeThumb) {
//make the thumbnail
if ($thumbWidth == null) {
$thumbWidth == self::THUMB_WIDTH;
}
$this->thumbPath = $this->resize($upload, $thumbWidth, 'thumb_');
}
if ($resize == true) {
//remove the source file
unlink($upload);
}
}
}
/**
* resizes images
* if append is set then it will append this string to the new filename
* it returns the path to the resized file
*
* @param string $path
* @param int $newwidth
* @param string $append
* @return string
*/
public function resize($path, $newwidth, $append = null)
{
$fName = basename($path);
// Create an Image from it so we can do the resize
$upload = $this->_openImage($path);
if ($upload) {
// Capture the original size of the image
$width = imagesx($this->_image);
$height = imagesy($this->_image);
$newheight=$height * ($newwidth/$width);
$tmp=imagecreatetruecolor($newwidth,$newheight);
// this line actually does the image resizing, copying from the original
// image into the $tmp image
imagecopyresampled($tmp,$this->_image,0,0,0,0,$newwidth,$newheight,$width,$height);
// now write the resized image to disk.
$newPath = str_replace($fName, $append . $fName, $path);
$imageFunction = "image" . $this->fileType;
$imageFunction($tmp,$newPath, $this->_imageQuality[$this->fileType]);
imagedestroy($this->_image);
imagedestroy($tmp); // NOTE: PHP will clean up the temp file it created when the request
return $newPath;
} else {
die("Could not open image file");
}
}
/**
* tests the selected image and opens it with the proper function
*
* @param string $file
* @return bool
*/
private function _openImage($file)
{
# JPEG:
$im = @imagecreatefromjpeg($file);
if ($im !== false) {
$this->fileType = 'jpeg';
$this->_image = $im;
return true;
}
# GIF:
$im = @imagecreatefromgif ($file);
if ($im !== false) {
$this->fileType = 'gif';
$this->_image = $im;
return true;
}
# PNG:
$im = @imagecreatefrompng($file);
if ($im !== false) {
$this->fileType = 'png';
$this->_image = $im;
return true;
}
return false;
}
}