-
Notifications
You must be signed in to change notification settings - Fork 0
/
class-jpegfit.php
227 lines (176 loc) · 4.64 KB
/
class-jpegfit.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
<?php
/**
* JpegFit Class
* 2007-04-12 Image Resource getters and setters with examples
* 2007-03-29 First Version
*
* IMPORTANT NOTE
* There is no warranty, implied or otherwise with this software.
*
* LICENCE
* This code has been placed in the Public Domain for all to enjoy.
*
* @author Zafar Iqbal
* @package JpegFit
*/
class JpegFit
{
// Image file path
private $imageFilePath;
// Image handle
private $im;
// Quality range
private $low, $high;
private $q, $cq;
// Target image size
private $targetSize;
// Target resolution
private $width,$height,$resizeFlag;
// Precision
private $precision;
// Max Iterations
private $maxIterations;
// Debug Array
private $debug;
// Class Constructor
public function __construct()
{
$this->init();
}
// Class Destructor
public function __destruct()
{
imagedestroy($this->im);
}
// Init variables
public function init()
{
// Image filename and resource handle
$this->imageFilePath = NULL;
$this->im = NULL;
// Quality range
$this->low = 0;
$this->high = 100;
$this->q = 50;
$this->cq = 50;
// Difference in high/low to break loop
$this->precision = 0.1;
// Total iterations to break loop
$this->maxIterations = 16;
// Default resize flag
$this->resizeFlag=false;
}
// Set function for path to image - can be URL
public function setImageFilePath( $path )
{
$this->imageFilePath = $path;
}
// Set function for target size - in bytes
public function setTargetSize( $size )
{
$this->targetSize = $size;
}
// Set width and height for output image (see example 5)
public function setResolution()
{
$args=func_get_args();
if(count($args)==0 or count($args)>2) return;
$this->resizeFlag=true;
$this->width=$args[0];
$this->height=$args[1];
}
// Set Image Resource
public function setImageResource( $ir )
{
$this->im = $ir;
}
// Get Image Resource
public function getImageResource()
{
return $this->im;
}
// Function to compute jpeg quality setting restricted to image size
public function fit()
{
// Load image
if( $this->im == NULL) $this->im = imagecreatefromstring( file_get_contents( $this->imageFilePath ) );
if($this->resizeFlag)
{
$newWidth=$this->width;
$newHeight=$this->height;
$t=imagecreatetruecolor($newWidth,$newHeight);
imagecopyresampled($t,$this->im,0,0,0,0,$newWidth,$newHeight,imagesx($this->im),imagesy($this->im));
imagedestroy($this->im);
$this->im=$t;
//imagedestroy($t);
$this->debug[]="[RESIZE] TRUE -> $newWidth $newHeight";
}
// Counter to keep track of iterations
$cc = 0;
// Use the buffer NOT the filesystem to compute intermediate image size
ob_start();
// Loop forever
while( true )
{
// Empty buffer
ob_clean();
// Keep track of previous quality setting
$this->cq = $this->q;
// Create and fill buffer with image with current quality setting
imagejpeg( $this->im, NULL, $this->cq );
// Compute current image size from size of buffer
$currentSize = strlen( ob_get_contents() );
// Some debug
$this->debug[] = " " . $this->low . " >>> " . $this->cq . " <<< " . $this->high . " [ $currentSize / " . $this->targetSize . " ]";
// Break loop if target size is reached - very rare!
if ( $currentSize == $this->targetSize )
break;
// If size > target then change quality range
if ( $currentSize > $this->targetSize )
{
$this->high = $this->q;
$this->q = ( $this->q + $this->low ) / 2;
}
// If size < target then change quality range
if ( $currentSize < $this->targetSize )
{
$this->low = $this->q;
$this->q = ( $this->q + $this->high ) / 2;
}
// Break loop if high/low gap below precision AND size is < target size
if ( ( ( $this->high - $this->low ) < $this->precision ) && ( $currentSize <= $this->targetSize ) )
break;
// Break loop of counter has reached maximum iterations - target size either to low/high
if ( $cc == $this->maxIterations )
break;
// Continue loop incrementing counter
$cc++;
}
// Final debug
$this->debug[] = "Final Quality Setting = " . $this->cq;
// Disable buffer
ob_end_clean();
}
// Save image to filename
public function saveImage($filename)
{
imagejpeg( $this->im, "$filename", $this->cq );
}
// Send image to client - including http header
public function clientOutput()
{
header( "Content-type: image/jpeg" );
imagejpeg( $this->im, NULL, $this->cq);
}
// Get the final quality setting
public function getQ()
{
return $this->cq;
}
// Ouput debug that has been generated
public function dprint()
{
print_r( $this->debug );
}
}
?>