Skip to content

Comptability PHP 8 #20

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions changelog
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@
Removed trailing whitespace from blank lines.
1.0.4 - 2016-09-27
Updated version to get composer to track properly.
2.0.0 - 2023-08-02
Comptability PHP 8
47 changes: 32 additions & 15 deletions class-php-ico.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ class PHP_ICO {
* @var array
* @access private
*/
var $_images = array();
private $_images = [];

/**
* Flag to tell if the required functions exist.
*
* @var boolean
* @access private
*/
var $_has_requirements = false;
private $_has_requirements = false;


/**
Expand All @@ -34,8 +34,8 @@ class PHP_ICO {
* @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(
public function __construct( $file = false, array $sizes = [] ) {
$required_functions = [
'getimagesize',
'imagecreatefromstring',
'imagecreatetruecolor',
Expand All @@ -45,8 +45,8 @@ function __construct( $file = false, $sizes = array() ) {
'imagesavealpha',
'imagesx',
'imagesy',
'imagecopyresampled',
);
'imagecopyresampled'
];

foreach ( $required_functions as $function ) {
if ( ! function_exists( $function ) ) {
Expand Down Expand Up @@ -74,7 +74,7 @@ function __construct( $file = false, $sizes = array() ) {
* @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() ) {
public function add_image( string $file, array $sizes = [] ):bool {
if ( ! $this->_has_requirements )
return false;

Expand Down Expand Up @@ -116,7 +116,7 @@ function add_image( $file, $sizes = array() ) {
* @param string $file Path to save the ICO file data into.
* @return boolean true on success and false on failure.
*/
function save_ico( $file ) {
public function save_ico(string $file ) :bool {
if ( ! $this->_has_requirements )
return false;

Expand All @@ -135,13 +135,29 @@ function save_ico( $file ) {

return true;
}

/**
* Display the ICO file data.
*
* @return boolean true on success and false on failure.
*/
public function render_ico():bool {
if ( ! $this->_has_requirements )
return false;

if ( false === ( $data = $this->_get_ico_data() ) )
return false;

header('Content-Type: image/x-icon');
echo $data;

return true;
}
/**
* Generate the final ICO data by creating a file header and adding the image data.
*
* @access private
*/
function _get_ico_data() {
private function _get_ico_data() {
if ( ! is_array( $this->_images ) || empty( $this->_images ) )
return false;

Expand Down Expand Up @@ -172,7 +188,7 @@ function _get_ico_data() {
*
* @access private
*/
function _add_image_data( $im ) {
private function _add_image_data( $im ) {
$width = imagesx( $im );
$height = imagesy( $im );

Expand All @@ -187,7 +203,8 @@ function _add_image_data( $im ) {
$color = imagecolorat( $im, $x, $y );

$alpha = ( $color & 0x7F000000 ) >> 24;
$alpha = ( 1 - ( $alpha / 127 ) ) * 255;
//$alpha = ( 1 - ( $alpha / 127 ) ) * 255;
$alpha = round( ( 1 - ( $alpha / 127 ) ) * 255);

$color &= 0xFFFFFF;
$color |= 0xFF000000 & ( $alpha << 24 );
Expand Down Expand Up @@ -228,14 +245,14 @@ function _add_image_data( $im ) {
$data .= pack( 'N', $opacity );


$image = array(
$image = [
'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;
}
Expand All @@ -245,7 +262,7 @@ function _add_image_data( $im ) {
*
* @access private
*/
function _load_image_file( $file ) {
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;
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "chrisjean/php-ico",
"description": "An easy-to-use library to generate valid ICO files.",
"version": "1.0.4",
"version": "2.0.0",
"keywords": ["ico", "favicon"],
"homepage": "https://github.com/chrisbliss18/php-ico",
"license": "GPL-2.0+",
Expand All @@ -17,7 +17,7 @@
"source": "https://github.com/chrisbliss18/php-ico"
},
"require": {
"php": ">=5.2.4",
"php": ">=7.3",
"ext-gd": "*"
},
"autoload": {
Expand Down
Loading