1
1
<?php
2
2
3
3
/*
4
- Copyright 2011-2013 Chris Jean & iThemes
4
+ Copyright 2011-2016 Chris Jean & iThemes
5
5
Licensed under GPLv2 or above
6
6
7
- Version 1.0.2
7
+ Version 1.0.3
8
8
*/
9
9
10
10
class PHP_ICO {
@@ -15,16 +15,16 @@ class PHP_ICO {
15
15
* @access private
16
16
*/
17
17
var $ _images = array ();
18
-
18
+
19
19
/**
20
20
* Flag to tell if the required functions exist.
21
21
*
22
22
* @var boolean
23
23
* @access private
24
24
*/
25
25
var $ _has_requirements = false ;
26
-
27
-
26
+
27
+
28
28
/**
29
29
* Constructor - Create a new ICO generator.
30
30
*
@@ -47,21 +47,21 @@ function __construct( $file = false, $sizes = array() ) {
47
47
'imagesy ' ,
48
48
'imagecopyresampled ' ,
49
49
);
50
-
50
+
51
51
foreach ( $ required_functions as $ function ) {
52
52
if ( ! function_exists ( $ function ) ) {
53
53
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. " );
54
54
return ;
55
55
}
56
56
}
57
-
57
+
58
58
$ this ->_has_requirements = true ;
59
-
60
-
59
+
60
+
61
61
if ( false != $ file )
62
62
$ this ->add_image ( $ file , $ sizes );
63
63
}
64
-
64
+
65
65
/**
66
66
* Add an image to the generator.
67
67
*
@@ -77,39 +77,39 @@ function __construct( $file = false, $sizes = array() ) {
77
77
function add_image ( $ file , $ sizes = array () ) {
78
78
if ( ! $ this ->_has_requirements )
79
79
return false ;
80
-
80
+
81
81
if ( false === ( $ im = $ this ->_load_image_file ( $ file ) ) )
82
82
return false ;
83
-
84
-
83
+
84
+
85
85
if ( empty ( $ sizes ) )
86
86
$ sizes = array ( imagesx ( $ im ), imagesy ( $ im ) );
87
-
87
+
88
88
// If just a single size was passed, put it in array.
89
89
if ( ! is_array ( $ sizes [0 ] ) )
90
90
$ sizes = array ( $ sizes );
91
-
91
+
92
92
foreach ( (array ) $ sizes as $ size ) {
93
93
list ( $ width , $ height ) = $ size ;
94
-
94
+
95
95
$ new_im = imagecreatetruecolor ( $ width , $ height );
96
-
96
+
97
97
imagecolortransparent ( $ new_im , imagecolorallocatealpha ( $ new_im , 0 , 0 , 0 , 127 ) );
98
98
imagealphablending ( $ new_im , false );
99
99
imagesavealpha ( $ new_im , true );
100
-
100
+
101
101
$ source_width = imagesx ( $ im );
102
102
$ source_height = imagesy ( $ im );
103
-
103
+
104
104
if ( false === imagecopyresampled ( $ new_im , $ im , 0 , 0 , 0 , 0 , $ width , $ height , $ source_width , $ source_height ) )
105
105
continue ;
106
-
106
+
107
107
$ this ->_add_image_data ( $ new_im );
108
108
}
109
-
109
+
110
110
return true ;
111
111
}
112
-
112
+
113
113
/**
114
114
* Write the ICO file data to a file path.
115
115
*
@@ -119,23 +119,23 @@ function add_image( $file, $sizes = array() ) {
119
119
function save_ico ( $ file ) {
120
120
if ( ! $ this ->_has_requirements )
121
121
return false ;
122
-
122
+
123
123
if ( false === ( $ data = $ this ->_get_ico_data () ) )
124
124
return false ;
125
-
125
+
126
126
if ( false === ( $ fh = fopen ( $ file , 'w ' ) ) )
127
127
return false ;
128
-
128
+
129
129
if ( false === ( fwrite ( $ fh , $ data ) ) ) {
130
130
fclose ( $ fh );
131
131
return false ;
132
132
}
133
-
133
+
134
134
fclose ( $ fh );
135
-
135
+
136
136
return true ;
137
137
}
138
-
138
+
139
139
/**
140
140
* Generate the final ICO data by creating a file header and adding the image data.
141
141
*
@@ -144,29 +144,29 @@ function save_ico( $file ) {
144
144
function _get_ico_data () {
145
145
if ( ! is_array ( $ this ->_images ) || empty ( $ this ->_images ) )
146
146
return false ;
147
-
148
-
147
+
148
+
149
149
$ data = pack ( 'vvv ' , 0 , 1 , count ( $ this ->_images ) );
150
150
$ pixel_data = '' ;
151
-
151
+
152
152
$ icon_dir_entry_size = 16 ;
153
-
153
+
154
154
$ offset = 6 + ( $ icon_dir_entry_size * count ( $ this ->_images ) );
155
-
155
+
156
156
foreach ( $ this ->_images as $ image ) {
157
157
$ data .= pack ( 'CCCCvvVV ' , $ image ['width ' ], $ image ['height ' ], $ image ['color_palette_colors ' ], 0 , 1 , $ image ['bits_per_pixel ' ], $ image ['size ' ], $ offset );
158
158
$ pixel_data .= $ image ['data ' ];
159
-
159
+
160
160
$ offset += $ image ['size ' ];
161
161
}
162
-
162
+
163
163
$ data .= $ pixel_data ;
164
164
unset( $ pixel_data );
165
-
166
-
165
+
166
+
167
167
return $ data ;
168
168
}
169
-
169
+
170
170
/**
171
171
* Take a GD image resource and change it into a raw BMP format.
172
172
*
@@ -175,59 +175,59 @@ function _get_ico_data() {
175
175
function _add_image_data ( $ im ) {
176
176
$ width = imagesx ( $ im );
177
177
$ height = imagesy ( $ im );
178
-
179
-
178
+
179
+
180
180
$ pixel_data = array ();
181
-
181
+
182
182
$ opacity_data = array ();
183
183
$ current_opacity_val = 0 ;
184
-
184
+
185
185
for ( $ y = $ height - 1 ; $ y >= 0 ; $ y -- ) {
186
186
for ( $ x = 0 ; $ x < $ width ; $ x ++ ) {
187
187
$ color = imagecolorat ( $ im , $ x , $ y );
188
-
188
+
189
189
$ alpha = ( $ color & 0x7F000000 ) >> 24 ;
190
190
$ alpha = ( 1 - ( $ alpha / 127 ) ) * 255 ;
191
-
191
+
192
192
$ color &= 0xFFFFFF ;
193
193
$ color |= 0xFF000000 & ( $ alpha << 24 );
194
-
194
+
195
195
$ pixel_data [] = $ color ;
196
-
197
-
196
+
197
+
198
198
$ opacity = ( $ alpha <= 127 ) ? 1 : 0 ;
199
-
199
+
200
200
$ current_opacity_val = ( $ current_opacity_val << 1 ) | $ opacity ;
201
-
201
+
202
202
if ( ( ( $ x + 1 ) % 32 ) == 0 ) {
203
203
$ opacity_data [] = $ current_opacity_val ;
204
204
$ current_opacity_val = 0 ;
205
205
}
206
206
}
207
-
207
+
208
208
if ( ( $ x % 32 ) > 0 ) {
209
209
while ( ( $ x ++ % 32 ) > 0 )
210
210
$ current_opacity_val = $ current_opacity_val << 1 ;
211
-
211
+
212
212
$ opacity_data [] = $ current_opacity_val ;
213
213
$ current_opacity_val = 0 ;
214
214
}
215
215
}
216
-
216
+
217
217
$ image_header_size = 40 ;
218
218
$ color_mask_size = $ width * $ height * 4 ;
219
219
$ opacity_mask_size = ( ceil ( $ width / 32 ) * 4 ) * $ height ;
220
-
221
-
220
+
221
+
222
222
$ data = pack ( 'VVVvvVVVVVV ' , 40 , $ width , ( $ height * 2 ), 1 , 32 , 0 , 0 , 0 , 0 , 0 , 0 );
223
-
223
+
224
224
foreach ( $ pixel_data as $ color )
225
225
$ data .= pack ( 'V ' , $ color );
226
-
226
+
227
227
foreach ( $ opacity_data as $ opacity )
228
228
$ data .= pack ( 'N ' , $ opacity );
229
-
230
-
229
+
230
+
231
231
$ image = array (
232
232
'width ' => $ width ,
233
233
'height ' => $ height ,
@@ -236,10 +236,10 @@ function _add_image_data( $im ) {
236
236
'size ' => $ image_header_size + $ color_mask_size + $ opacity_mask_size ,
237
237
'data ' => $ data ,
238
238
);
239
-
239
+
240
240
$ this ->_images [] = $ image ;
241
241
}
242
-
242
+
243
243
/**
244
244
* Read in the source image file and convert it into a GD image resource.
245
245
*
@@ -249,16 +249,16 @@ function _load_image_file( $file ) {
249
249
// Run a cheap check to verify that it is an image file.
250
250
if ( false === ( $ size = getimagesize ( $ file ) ) )
251
251
return false ;
252
-
252
+
253
253
if ( false === ( $ file_data = file_get_contents ( $ file ) ) )
254
254
return false ;
255
-
255
+
256
256
if ( false === ( $ im = imagecreatefromstring ( $ file_data ) ) )
257
257
return false ;
258
-
258
+
259
259
unset( $ file_data );
260
-
261
-
260
+
261
+
262
262
return $ im ;
263
263
}
264
264
}
0 commit comments