Hi, i'm newbie in php and i got a little problem. I use this php script to get the rgb color from a jpg file... <?php $image=imagecreatefromjpeg('thumbs/dark-blue-material-texture.jpg'); $thumb=imagecreatetruecolor(1,1); imagecopyresampled($thumb,$image,0,0,0,0,1,1,imagesx($image),imagesy($image)); $mainColor=(dechex(imagecolorat($thumb,0,0))); echo $mainColor; ?> PHP: the script for the image "dark-blue-material-texture.jpg" returns this hex color: 176e from (rgb(0, 23, 110)) but the correct hex would be: 00176e I see that the script not showing the first two zeros, is there a way to showing all the zeros when using dechex?
$hex = str_repeat("0", floor(strspn($binary, "0") / 4)).$hex; PHP: For more information about the code please check: https://stackoverflow.com/questions/16576187/php-binary-to-hex-with-leading-zeros
str_pad is your friend. $mainColor = str_pad(dechex(imagecolorat($thumb, 0, 0)), 6, '0', STR_PAD_LEFT); Code (markup): @RoseHosting -- REALLY? floating point math doing subtraction's job?
$mainColor=str_pad((dechex(imagecolorat($thumb,0,0))),6,"0",STR_PAD_LEFT); PHP: I got help from: https://stackoverflow.com/questions/46131643/dechex-not-showing-first-two-zeros-rgb-to-hex-php Anyway thanks for answer(s)
And remember, the value of 00176E and the value of 176E are exactly the same. Break it into sets of 2 digits starting at the right end.