-
Notifications
You must be signed in to change notification settings - Fork 68
/
Copy pathimagelib.php
228 lines (213 loc) · 7.73 KB
/
imagelib.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
223
224
225
226
227
228
<?php
/*
* * File: imagelib.php
* * Author: Paul Krix
* * Image resizing based heavily on SimpleImage.php. Attribution below.
* *
* * File: SimpleImage.php
* * Author: Simon Jarvis
* * Copyright: 2006 Simon Jarvis
* * Date: 08/11/06
* * Link: http://www.white-hat-web-design.co.uk/articles/php-image-resizing.php
* *
* * This program is free software; you can redistribute it and/or
* * modify it under the terms of the GNU General Public License
* * as published by the Free Software Foundation; either version 2
* * of the License, or (at your option) any later version.
* *
* * This program is distributed in the hope that it will be useful,
* * but WITHOUT ANY WARRANTY; without even the implied warranty of
* * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* * GNU General Public License for more details:
* * http://www.gnu.org/licenses/gpl.html
* *
* */
function validate_upload($upload_name) {
$whitelist = array('jpg', 'png', 'gif', 'jpeg'); // Allowed file extensions
$backlist = array('php', 'php3', 'php4', 'phtml','exe'); // Restrict file extensions
$valid_chars_regex = 'A-Za-z0-9_-\s. '; // Characters allowed in the file name (in a Regular Expression format)
$file_name = '';
$file_extension = '';
$mime_type = '';
// Validate the upload
if (!isset($_FILES[$upload_name])) {
HandleError('No upload found in \$_FILES for ' . $upload_name);
return false;
} else if (isset($_FILES[$upload_name]['error']) && $_FILES[$upload_name]['error'] != 0) {
HandleError($uploadErrors[$_FILES[$upload_name]['error']]);
return false;
} else if (!isset($_FILES[$upload_name]['tmp_name']) || !@is_uploaded_file($_FILES[$upload_name]['tmp_name'])) {
HandleError('Upload failed is_uploaded_file test.');
return false;
} else if (!isset($_FILES[$upload_name]['name'])) {
HandleError('File has no name.');
return false;
}
// Validate its a MIME Images (Take note that not all MIME is the same across different browser, especially when its zip file)
$mime_type = $_FILES[$upload_name]['type'];
if(!eregi('image/', $mime_type)) {
HandleError('Please upload a valid file!');
return false;
}
// Validate that it is an image
$imageinfo = getimagesize($_FILES[$upload_name]['tmp_name']);
if($imageinfo['mime'] != 'image/gif' && $imageinfo['mime'] != 'image/jpeg' && $imageinfo['mime'] != 'image/png' && isset($imageinfo)) {
HandleError('Sorry, we only accept GIF and JPEG images');
return false;
}
// Validate file name (for our purposes we'll just remove invalid characters)
$file_name = preg_replace('/[^'.$valid_chars_regex.']|\.+$/i', '', strtolower(basename($_FILES[$upload_name]['name'])));
if (strlen($file_name) == 0) {
HandleError('Invalid file name');
return false;
}
// Validate file extension
if(!in_array(end(explode('.', $file_name)), $whitelist)) {
HandleError('Invalid file extension');
return false;
}
if(in_array(end(explode('.', $file_name)), $backlist)) {
HandleError('Invalid file extension');
return false;
}
return true;
}
function HandleError($message) {
//just outputs the message at the moment. Here for future convenience
echo "Error: " . $message;
}
class ImageFunctions {
var $image;
var $image_type;
function set_image($image, $image_type) {
$this->image = $image;
$this->image_type = $image_type;
}
function get_image() {
return $this->image;
}
function load($filename) {
$image_info = getimagesize($filename);
$this->image_type = $image_info[2];
if( $this->image_type == IMAGETYPE_JPEG ) {
$this->image = imagecreatefromjpeg($filename);
} elseif( $this->image_type == IMAGETYPE_GIF ) {
$this->image = imagecreatefromgif($filename);
} elseif( $this->image_type == IMAGETYPE_PNG ) {
$this->image = imagecreatefrompng($filename);
}
}
function save($filename, $image_type=IMAGETYPE_JPEG, $compression=75, $permissions=null) {
if( $image_type == IMAGETYPE_JPEG ) {
imagejpeg($this->image,$filename,$compression);
} elseif( $image_type == IMAGETYPE_GIF ) {
imagegif($this->image,$filename);
} elseif( $image_type == IMAGETYPE_PNG ) {
imagepng($this->image,$filename);
}
if( $permissions != null) {
chmod($filename,$permissions);
}
}
function output($image_type=IMAGETYPE_JPEG) {
if( $image_type == IMAGETYPE_JPEG ) {
imagejpeg($this->image);
} elseif( $image_type == IMAGETYPE_GIF ) {
imagegif($this->image);
} elseif( $image_type == IMAGETYPE_PNG ) {
imagepng($this->image);
}
}
function getWidth() {
return imagesx($this->image);
}
function getHeight() {
return imagesy($this->image);
}
function resizeToHeight($height) {
$ratio = $height / $this->getHeight();
$width = $this->getWidth() * $ratio;
$this->resize($width,$height);
}
function resizeToWidth($width) {
$ratio = $width / $this->getWidth();
$height = $this->getheight() * $ratio;
$this->resize($width,$height);
}
function scale($scale) {
$width = $this->getWidth() * $scale/100;
$height = $this->getheight() * $scale/100;
$this->resize($width,$height);
}
function resize($width,$height) {
$new_image = imagecreatetruecolor($width, $height);
imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
$this->image = $new_image;
}
function cropCenterHeight($height) {
$width = $this->getWidth();
$srcOffset = $this->getHeight()/2 - $height/2;
$new_image = imagecreatetruecolor($width, $height);
imagecopyresampled($new_image, $this->image, 0, 0, 0, $srcOffset, $width, $height, $width, $height);
$this->image = $new_image;
}
function cropCenterWidth($width) {
$height = $this->getHeight();
$srcOffset = $this->getWidth()/2 - $width/2;
$new_image = imagecreatetruecolor($width, $height);
imagecopyresampled($new_image, $this->image, 0, 0, $srcOffset, 0, $width, $height, $width, $height);
$this->image = $new_image;
}
function getSmallestDim() {
$width = $this->getWidth();
$height = $this->getHeight();
if ($width < $height) {
return $width;
}
return $height;
}
function cropSmallestDim() {
$width = $this->getWidth();
$height = $this->getHeight();
$yOffset = 0;
$xOffset = 0;
$smallestDim = 0;
if ($width < $height) {
$smallestDim = $width;
$yOffset = $height/2 - $smallestDim/2;
} else {
$smallestDim = $height;
$xOffset = $width/2 - $smallestDim/2;
}
$new_image = imagecreatetruecolor($smallestDim, $smallestDim);
imagecopyresampled($new_image, $this->image, 0, 0, $xOffset, $yOffset, $smallestDim, $smallestDim, $smallestDim, $smallestDim);
$this->image = $new_image;
}
function resizeAndCrop($width, $height) {
$ratio = $width / $height;
$curWidth = $this->getWidth();
$curHeight = $this->getHeight();
$curRatio = $curWidth / $curHeight;
if($curRatio < $ratio) {
$this->resizeToWidth($width);
$this->cropCenterHeight($height);
} else {
$this->resizeToHeight($height);
$this->cropCenterWidth($width);
}
}
function resizeNormalImage($width, $height) {
$curWidth = $this->getWidth();
$curHeight = $this->getHeight();
$diffWidth = $curWidth - $width;
$diffHeight = $curHeight - $height;
if($diffWidth > 0 || $diffHeight > 0) {
if($diffWidth >= $diffHeight) {
$this->resizeToWidth($width);
} else {
$this->resizeToHeight($height);
}
}
}
}
?>