php


Is it possible to retrieve the alpha value of a pixel of a PNG file in the 0-255 range?


In a recent competition I was given the task to extract binary data (another PNG) from a PNG image file's alpha channel. The data was encoded in such a way that if I read the values in the alpha channel for each pixel from the top left (e.g. 80,78,71,13,10,26,10) up to a specific point then the resulting data would form another image.
Initially I tried to complete this task using PHP, but I hit a roadblock that I could not overcome. Consider the code below:
function pad($hex){
return strlen($hex) < 2 ? "0$hex" : $hex;
}
$channel = '';
$ir = imagecreatefrompng('image.png');
imagesavealpha($ir, true);
list($width, $height) = getimagesize($image);
for ($y = 0; $y < $height; $y++){
for ($x = 0; $x < $width; $x++){
$pixel = imagecolorat($ir, $x, $y);
$colors = imagecolorsforindex($ir, $pixel);
$a = pad(dechex($colors['alpha']));
$channel.= $a;
}
}
After running this, I noticed that the output did not contain the PNG magic number, and I just didn't know what went wrong. After a bit of digging I found out that $colors['alpha'] only contained values less than or equal to 127. Due to the data having been encoded with a 0-255 range in mind, I could not find a way to extract it, and ended up (successfully) solving the problem with node.js instead.
So, my question is: Is there any way I could have read the PNG file's alpha channel that would have returned the values in a 0 to 255 range as opposed to 0-127, or is this a hard-coded limitation of PHP and/or GD?
For the record, I tried to use ($colors['alpha']/127)*255 in order to try and forge the value in the incorrect range to the correct one, but to no avail.
It is a limitation of GD. According to https://bitbucket.org/libgd/gd-libgd/issues/132/history-behind-7-bit-alpha-component, in GD source it says:
gd has only 7 bits of alpha channel resolution, and 127 is
transparent, 0 opaque. A moment of convenience, a lifetime of
compatibility.
If you install ImageMagick PHP extension, you can get the alpha value between 0-255 for a pixel (let's say with x=300 and y=490) like this:
$image = new Imagick(__DIR__ . DIRECTORY_SEPARATOR . 'image.png');
$x = 300;
$y = 490;
$pixel = $image->getImagePixelColor($x, $y);
$colors = $pixel->getColor(true);
echo 'Alpha value: ' . round($colors['a'] * 255, 0);
ImageMagick: https://www.imagemagick.org
ImageMagick for PHP (called Imagick): http://php.net/manual/en/book.imagick.php

Related Links

HTML/PHP set input field to an exact number.
cannot access nusoap webservice on my hosted website to localhost, but work on localhost to localhost
CSV: Change One Field Column
How to set values of all element in array with loop conditional?
include head with php
enable Access-Control-Allow-Headers in codeigniter (issue related to ajax) [duplicate]
Can MongoDB instances in Google App Engine flexible be stateful?
MYSQL : select count with union three table
How to replace ?{GET Variable}= with a /
Run npm install script through php
PHP ftp_put fails with “Warning: ftp_put (): PORT command successful”
Page redirects to itself
What is the purpose of repository when service classes can do the same?
input text field 1 validation sql result equal then go next input text field 2
Can't use function return value in write context (numeric function) [duplicate]
Codeigniter Update_batch not updating records

Categories

HOME
visual-studio
flask
user-interface
mod-rewrite
facebook-messenger
angular-ui-grid
exchange-server
scroll
key
message-queue
websocket
mongoid
iptables
flurry
drag-and-drop
facebook-android-sdk
reportviewer
clickable-image
google-awareness
squid
pygobject
significance
bazaar
adobe-illustrator
pfobject
clr
dashdb
kaggle
generator
qa
intel
ini
password-generator
uri
xdebug
tf-idf
math.js
acrobat
code-climate
bonita
guice
nic
google-play-developer-api
rust-cargo
rspec-rails
mongoengine
image-optimization
runtimeexception
cartopy
microsoft-ui-automation
llvm-ir
python-jira
secure-coding
fiware-wirecloud
permission-denied
type-inference
dredd
qvtkwidget
supertest
nonlinear-functions
popen
emacs25
mechanicalturk
word-2016
fitbit
boost-compute
scheduledexecutorservice
p6spy
conan
fiber
deeplink
directx-9
fanotify
ambiguity
uitest
explain
phpredis
ocmockito
gflags
htmltidy
pass-by-value
kognitio-wx2
qt5.4
lmax
nidaqmx
senchatouch-2.4
forio-contour
map
windows-taskbar
assetic
smips
hyphen
sql-view
pacman
xs
stretch
android-dialog
jzmq
fbml
dynamic-c
webresponse
iphone-sdk-4.3
fxruby
data-retrieval
database-cloning
server-load
remote-working

Resources

Encrypt Message