note 130540 added to function.getimagesize

From: Date: Sat, 08 Nov 2025 22:43:17 +0000
Subject: note 130540 added to function.getimagesize
Groups: php.notes 
Request: Send a blank email to php-notes+get-222412@lists.php.net to get a copy of this message
Looking at the number of channels, you can detect if the image SUPPORTS transparecy function image_supports_transparency(string $file_path): ?bool { $ret = \getimagesize($file_path); $image_type = $ret[2]; $supports_transparency = null; if (isset($ret['channels'])) { $channels = $ret['channles']; if ($image_type === \IMAGETYPE_JPEG || $image_type === \IMAGETYPE_GIF) { $supports_transparency = false; } elseif ($image_type === \IMAGETYPE_PNG) { if ($channels === 2 || $channels === 4) { $supports_transparency = true; } elseif ($channels !== 1) { // PNG also supports transparency via palette, which only uses a single channel. $supports_transparency = false; } } elseif ($image_type === \IMAGETYPE_WEBP) { $supports_transparency = $channels === 4; } elseif ($mime === 'image/avif') { $supports_transparency = $channels === 4 || $channels == 2; } elseif ($image_type === \IMAGETYPE_BMP) { $supports_transparency = $channels === 4; } } return $supports_transparency; } I say supports because an image may have an alpha channel but not actually use it, you'll need to check all the image's pixels to detect that. Also note that it's not always possible to detect the alpha channel with just getimagesize(); see for example png which can define the number of channles actually used in the palette chunk. ---- Server IP: 45.112.84.4 Probable Submitter: 80.90.5.137 (proxied: 93.65.107.43) ---- Manual Page -- https://php.net/manual/en/function.getimagesize.php Edit -- https://main.php.net/note/edit/130540 Del: integrated -- https://main.php.net/note/delete/130540/integrated Del: useless -- https://main.php.net/note/delete/130540/useless Del: bad code -- https://main.php.net/note/delete/130540/bad+code Del: spam -- https://main.php.net/note/delete/130540/spam Del: non-english -- https://main.php.net/note/delete/130540/non-english Del: in docs -- https://main.php.net/note/delete/130540/in+docs Del: other reasons-- https://main.php.net/note/delete/130540 Reject -- https://main.php.net/note/reject/130540 Search -- https://main.php.net/manage/user-notes.php

« previous php.notes (#222412) next »