forked from chrisbliss18/php-ico
-
Notifications
You must be signed in to change notification settings - Fork 0
/
class-php-ico.php
264 lines (198 loc) · 6.86 KB
/
class-php-ico.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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
<?php
/*
Copyright 2011-2016 Chris Jean & iThemes
Licensed under GPLv2 or above
Version 1.0.4
*/
class PHP_ICO {
/**
* Images in the BMP format.
*
* @var array
* @access private
*/
var $_images = array();
/**
* Flag to tell if the required functions exist.
*
* @var boolean
* @access private
*/
var $_has_requirements = false;
/**
* Constructor - Create a new ICO generator.
*
* If the constructor is not passed a file, a file will need to be supplied using the {@link PHP_ICO::add_image}
* function in order to generate an ICO file.
*
* @param string $file Optional. Path to the source image file.
* @param array $sizes Optional. An array of sizes (each size is an array with a width and height) that the source image should be rendered at in the generated ICO file. If sizes are not supplied, the size of the source image will be used.
*/
function __construct( $file = false, $sizes = array() ) {
$required_functions = array(
'getimagesize',
'imagecreatefromstring',
'imagecreatetruecolor',
'imagecolortransparent',
'imagecolorallocatealpha',
'imagealphablending',
'imagesavealpha',
'imagesx',
'imagesy',
'imagecopyresampled',
);
foreach ( $required_functions as $function ) {
if ( ! function_exists( $function ) ) {
trigger_error( "The PHP_ICO class was unable to find the $function function, which is part of the GD library. Ensure that the system has the GD library installed and that PHP has access to it through a PHP interface, such as PHP's GD module. Since this function was not found, the library will be unable to create ICO files." );
return;
}
}
$this->_has_requirements = true;
if ( false != $file )
$this->add_image( $file, $sizes );
}
/**
* Add an image to the generator.
*
* This function adds a source image to the generator. It serves two main purposes: add a source image if one was
* not supplied to the constructor and to add additional source images so that different images can be supplied for
* different sized images in the resulting ICO file. For instance, a small source image can be used for the small
* resolutions while a larger source image can be used for large resolutions.
*
* @param string $file Path to the source image file.
* @param array $sizes Optional. An array of sizes (each size is an array with a width and height) that the source image should be rendered at in the generated ICO file. If sizes are not supplied, the size of the source image will be used.
* @return boolean true on success and false on failure.
*/
function add_image( $file, $sizes = array() ) {
if ( ! $this->_has_requirements )
return false;
if ( false === ( $im = $this->_load_image_file( $file ) ) )
return false;
if ( empty( $sizes ) )
$sizes = array( imagesx( $im ), imagesy( $im ) );
// If just a single size was passed, put it in array.
if ( ! is_array( $sizes[0] ) )
$sizes = array( $sizes );
foreach ( (array) $sizes as $size ) {
list( $width, $height ) = $size;
$new_im = imagecreatetruecolor( $width, $height );
imagecolortransparent( $new_im, imagecolorallocatealpha( $new_im, 0, 0, 0, 127 ) );
imagealphablending( $new_im, false );
imagesavealpha( $new_im, true );
$source_width = imagesx( $im );
$source_height = imagesy( $im );
if ( false === imagecopyresampled( $new_im, $im, 0, 0, 0, 0, $width, $height, $source_width, $source_height ) )
continue;
$this->_add_image_data( $new_im );
}
return true;
}
/**
* Write the ICO file data to a file path.
*
* @param string $file Path to save the ICO file data into.
* @return boolean true on success and false on failure.
*/
function save_ico( $file ) {
if ( ! $this->_has_requirements )
return false;
if ( false === ( $data = $this->_get_ico_data() ) )
return false;
if ( false === ( $fh = fopen( $file, 'w' ) ) )
return false;
if ( false === ( fwrite( $fh, $data ) ) ) {
fclose( $fh );
return false;
}
fclose( $fh );
return true;
}
/**
* Generate the final ICO data by creating a file header and adding the image data.
*
* @access private
*/
function _get_ico_data() {
if ( ! is_array( $this->_images ) || empty( $this->_images ) )
return false;
$data = pack( 'vvv', 0, 1, count( $this->_images ) );
$pixel_data = '';
$icon_dir_entry_size = 16;
$offset = 6 + ( $icon_dir_entry_size * count( $this->_images ) );
foreach ( $this->_images as $image ) {
$data .= pack( 'CCCCvvVV', $image['width'], $image['height'], $image['color_palette_colors'], 0, 1, $image['bits_per_pixel'], $image['size'], $offset );
$pixel_data .= $image['data'];
$offset += $image['size'];
}
$data .= $pixel_data;
unset( $pixel_data );
return $data;
}
/**
* Take a GD image resource and change it into a raw BMP format.
*
* @access private
*/
function _add_image_data( $im ) {
$width = imagesx( $im );
$height = imagesy( $im );
$pixel_data = array();
$opacity_data = array();
$current_opacity_val = 0;
for ( $y = $height - 1; $y >= 0; $y-- ) {
for ( $x = 0; $x < $width; $x++ ) {
$color = imagecolorat( $im, $x, $y );
$alpha = ( $color & 0x7F000000 ) >> 24;
$alpha = round( ( 1 - ( $alpha / 127 ) ) * 255);
$color &= 0xFFFFFF;
$color |= 0xFF000000 & ( $alpha << 24 );
$pixel_data[] = $color;
$opacity = ( $alpha <= 127 ) ? 1 : 0;
$current_opacity_val = ( $current_opacity_val << 1 ) | $opacity;
if ( ( ( $x + 1 ) % 32 ) == 0 ) {
$opacity_data[] = $current_opacity_val;
$current_opacity_val = 0;
}
}
if ( ( $x % 32 ) > 0 ) {
while ( ( $x++ % 32 ) > 0 )
$current_opacity_val = $current_opacity_val << 1;
$opacity_data[] = $current_opacity_val;
$current_opacity_val = 0;
}
}
$image_header_size = 40;
$color_mask_size = $width * $height * 4;
$opacity_mask_size = ( ceil( $width / 32 ) * 4 ) * $height;
$data = pack( 'VVVvvVVVVVV', 40, $width, ( $height * 2 ), 1, 32, 0, 0, 0, 0, 0, 0 );
foreach ( $pixel_data as $color )
$data .= pack( 'V', $color );
foreach ( $opacity_data as $opacity )
$data .= pack( 'N', $opacity );
$image = array(
'width' => $width,
'height' => $height,
'color_palette_colors' => 0,
'bits_per_pixel' => 32,
'size' => $image_header_size + $color_mask_size + $opacity_mask_size,
'data' => $data,
);
$this->_images[] = $image;
}
/**
* Read in the source image file and convert it into a GD image resource.
*
* @access private
*/
function _load_image_file( $file ) {
// Run a cheap check to verify that it is an image file.
if ( false === ( $size = getimagesize( $file ) ) )
return false;
if ( false === ( $file_data = file_get_contents( $file ) ) )
return false;
if ( false === ( $im = imagecreatefromstring( $file_data ) ) )
return false;
unset( $file_data );
return $im;
}
}