speciale-image-resizer
Gesponsorde koppelingen
PHP script bestanden
Code (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
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
<?
error_reporting(E_ALL);
Class clsImage {
var $cacheImages = true;
var $fileLocation;
var $cacheDirectory; // no trailing slash
var $imageSource;
var $imageHeight;
var $imageWidth;
var $imageNewHeight;
var $imageNewWidth;
var $imageMaxHeight;
var $imageMaxWidth;
var $imageQuality;
function clsImage( $file,$cacheDirectory ) {
// ---
// Constructor
// Set thins like default values
// ---
$this->imageMaxHeight = 300;
$this->imageMaxWidth = 400;
// ---
// Check if given cache dirtory directory exists, and then set it else throw error..
// ---
if( is_dir($cacheDirectory) ) {
$this->cacheDirectory = $cacheDirectory;
} else {
$this->_errorImage('clsImage::constructor -> message = Cache directory does not exist!');
exit;
}
// ---
// Set image properties
// Don't request imagesize if image is already cached
// ---
$this->fileLocation = $file;
$this->imageSource = $file;
if( !$this->_imageCached() ) {
$image = @getImageSize($file);
if( $image ) {
$this->imageWidth = $image[0];
$this->imageHeight = $image[1];
$this->imageType = $image[2];
} else {
$this->_errorImage('clsImage::constructor -> message = Cannot retrieve information about image, image proberly doesn\'t exist');
exit;
}
}
// ---
// Default image quality = 100 (%)
// ---
$this->imageQuality = 100;
}
function _setSizes() {
// ---
// Calculate the new sizes of the image
// ---
if( $this->imageWidth == $this->imageHeight ) {
// ---
// If image width and height is same size then we don't have to calculate the ratio
// ---
$this->imageNewWidth = $this->imageMaxWidth;
$this->imageNewHeight = $this->imageMax;
}
elseif( $this->imageWidth < $this->imageHeight ) {
$ratio = $this->imageMaxHeight/$this->imageHeight;
$this->imageNewWidth = $ratio*$this->imageWidth;
$this->imageNewHeight = $ratio*$this->imageHeight;
} else {
$ratio = $this->imageMaxWidth/$this->imageWidth;
$this->imageNewWidth = $ratio*$this->imageWidth;
$this->imageNewHeight = $ratio*$this->imageHeight;
}
if( $this->imageNewWidth > $this->imageWidth ) {
$this->imageNewWidth = $this->imageWidth;
}
if( $this->imageNewHeight > $this->imageHeight ) {
$this->imageNewHeight = $this->imageHeight;
}
}
function _writeToCache() {
// ---
// Function which writes and resized image to the cache
// ---
switch( $this->imageType ) {
case 1:
$input=imagecreatefromgif( $this->imageSource );
break;
case 2:
$input=imagecreatefromjpeg( $this->imageSource );
break;
case 3:
$input=imagecreatefrompng( $this->imageSource );
break;
default:
$this->_errorImage('clsImage::constructor -> message = Unknown image type');
exit;
}
$output = imageCreateTrueColor( $this->imageNewWidth ,$this->imageNewHeight );
imageColorAllocate( $output,200,0,80 );
imageCopyResampled( $output,$input,0,0,0,0,$this->imageNewWidth,$this->imageNewHeight,$this->imageWidth,$this->imageHeight );
$fileLocation = $this->cacheDirectory.'/'.md5( $this->fileLocation ).'.jpg';
imagejpeg( $output, $fileLocation, $this->imageQuality );
}
function _getImageFromCache() {
$image = imagecreatefromjpeg( $this->cacheDirectory.'/'.md5($this->fileLocation).'.jpg' );
imagejpeg($image);
}
function _imageCached() {
// ---
// Function which will return an boolean (true or false) if an image is cached before
// ---
if( file_exists( $this->cacheDirectory.'/'.md5($this->fileLocation).'.jpg' ) ) {
return true;
} else {
return false;
}
}
function _errorImage($text) {
// ---
// Prints an error image
// ---
$output=imagecreate( $this->imageNewWidth,$this->imageNewHeight );
imagecolorallocate($output,125,125,125);
imagestring($output,2,2,2,$text,imagecolorallocate($output,230,230,230));
header("Content-Type: image/jpeg");
imagejpeg($output);
}
function setMaxSizes( $maxHeight,$maxWidth ) {
if( is_numeric( $maxHeight ) && is_numeric( $maxWidth ) ) {
$this->imageMaxHeight = $maxHeight;
$this->imageMaxWidth = $maxWidth;
} else {
$this->_errorImage('clsImage::setMaxSizes -> message = parameters are not numeric!');
exit;
}
}
function loadImage() {
// ---
// A public function which returns the image with the correct headers.
// Set the headers first
// ---
header("Content-Type: image/jpeg");
if( !$this->_imageCached() ) {
$this->_setSizes();
$this->_writeToCache();
}
$this->_getImageFromCache();
}
}
if( !empty($_GET['location']) AND is_numeric($_GET['width']) AND is_numeric($_GET['height']) ) {
$image = New clsImage($_GET['location'],'imagecache/');
$image->setMaxSizes($_GET['height'],$_GET['width']);
$image->loadImage();
} else {
echo 'Invalid arguments!';
}
?>
error_reporting(E_ALL);
Class clsImage {
var $cacheImages = true;
var $fileLocation;
var $cacheDirectory; // no trailing slash
var $imageSource;
var $imageHeight;
var $imageWidth;
var $imageNewHeight;
var $imageNewWidth;
var $imageMaxHeight;
var $imageMaxWidth;
var $imageQuality;
function clsImage( $file,$cacheDirectory ) {
// ---
// Constructor
// Set thins like default values
// ---
$this->imageMaxHeight = 300;
$this->imageMaxWidth = 400;
// ---
// Check if given cache dirtory directory exists, and then set it else throw error..
// ---
if( is_dir($cacheDirectory) ) {
$this->cacheDirectory = $cacheDirectory;
} else {
$this->_errorImage('clsImage::constructor -> message = Cache directory does not exist!');
exit;
}
// ---
// Set image properties
// Don't request imagesize if image is already cached
// ---
$this->fileLocation = $file;
$this->imageSource = $file;
if( !$this->_imageCached() ) {
$image = @getImageSize($file);
if( $image ) {
$this->imageWidth = $image[0];
$this->imageHeight = $image[1];
$this->imageType = $image[2];
} else {
$this->_errorImage('clsImage::constructor -> message = Cannot retrieve information about image, image proberly doesn\'t exist');
exit;
}
}
// ---
// Default image quality = 100 (%)
// ---
$this->imageQuality = 100;
}
function _setSizes() {
// ---
// Calculate the new sizes of the image
// ---
if( $this->imageWidth == $this->imageHeight ) {
// ---
// If image width and height is same size then we don't have to calculate the ratio
// ---
$this->imageNewWidth = $this->imageMaxWidth;
$this->imageNewHeight = $this->imageMax;
}
elseif( $this->imageWidth < $this->imageHeight ) {
$ratio = $this->imageMaxHeight/$this->imageHeight;
$this->imageNewWidth = $ratio*$this->imageWidth;
$this->imageNewHeight = $ratio*$this->imageHeight;
} else {
$ratio = $this->imageMaxWidth/$this->imageWidth;
$this->imageNewWidth = $ratio*$this->imageWidth;
$this->imageNewHeight = $ratio*$this->imageHeight;
}
if( $this->imageNewWidth > $this->imageWidth ) {
$this->imageNewWidth = $this->imageWidth;
}
if( $this->imageNewHeight > $this->imageHeight ) {
$this->imageNewHeight = $this->imageHeight;
}
}
function _writeToCache() {
// ---
// Function which writes and resized image to the cache
// ---
switch( $this->imageType ) {
case 1:
$input=imagecreatefromgif( $this->imageSource );
break;
case 2:
$input=imagecreatefromjpeg( $this->imageSource );
break;
case 3:
$input=imagecreatefrompng( $this->imageSource );
break;
default:
$this->_errorImage('clsImage::constructor -> message = Unknown image type');
exit;
}
$output = imageCreateTrueColor( $this->imageNewWidth ,$this->imageNewHeight );
imageColorAllocate( $output,200,0,80 );
imageCopyResampled( $output,$input,0,0,0,0,$this->imageNewWidth,$this->imageNewHeight,$this->imageWidth,$this->imageHeight );
$fileLocation = $this->cacheDirectory.'/'.md5( $this->fileLocation ).'.jpg';
imagejpeg( $output, $fileLocation, $this->imageQuality );
}
function _getImageFromCache() {
$image = imagecreatefromjpeg( $this->cacheDirectory.'/'.md5($this->fileLocation).'.jpg' );
imagejpeg($image);
}
function _imageCached() {
// ---
// Function which will return an boolean (true or false) if an image is cached before
// ---
if( file_exists( $this->cacheDirectory.'/'.md5($this->fileLocation).'.jpg' ) ) {
return true;
} else {
return false;
}
}
function _errorImage($text) {
// ---
// Prints an error image
// ---
$output=imagecreate( $this->imageNewWidth,$this->imageNewHeight );
imagecolorallocate($output,125,125,125);
imagestring($output,2,2,2,$text,imagecolorallocate($output,230,230,230));
header("Content-Type: image/jpeg");
imagejpeg($output);
}
function setMaxSizes( $maxHeight,$maxWidth ) {
if( is_numeric( $maxHeight ) && is_numeric( $maxWidth ) ) {
$this->imageMaxHeight = $maxHeight;
$this->imageMaxWidth = $maxWidth;
} else {
$this->_errorImage('clsImage::setMaxSizes -> message = parameters are not numeric!');
exit;
}
}
function loadImage() {
// ---
// A public function which returns the image with the correct headers.
// Set the headers first
// ---
header("Content-Type: image/jpeg");
if( !$this->_imageCached() ) {
$this->_setSizes();
$this->_writeToCache();
}
$this->_getImageFromCache();
}
}
if( !empty($_GET['location']) AND is_numeric($_GET['width']) AND is_numeric($_GET['height']) ) {
$image = New clsImage($_GET['location'],'imagecache/');
$image->setMaxSizes($_GET['height'],$_GET['width']);
$image->loadImage();
} else {
echo 'Invalid arguments!';
}
?>