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