1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

dechex not showing first two zeros (rgb to hex)

Discussion in 'PHP' started by abyse, Sep 9, 2017.

  1. #1
    Hi, i'm newbie in php and i got a little problem.
    SEMrush
    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?
     
    abyse, Sep 9, 2017 IP
    SEMrush
  2. RoseHosting

    RoseHosting Active Member

    Messages:
    182
    Likes Received:
    10
    Best Answers:
    9
    Trophy Points:
    88
    #2
    RoseHosting, Sep 12, 2017 IP
    abyse likes this.
  3. deathshadow

    deathshadow Acclaimed Member

    Messages:
    8,406
    Likes Received:
    1,500
    Best Answers:
    220
    Trophy Points:
    515
    #3
    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?
     
    deathshadow, Sep 14, 2017 at 2:23 PM IP
    abyse likes this.
  4. abyse

    abyse Well-Known Member

    Messages:
    345
    Likes Received:
    9
    Best Answers:
    1
    Trophy Points:
    195
    #4
    abyse, Sep 15, 2017 at 6:45 AM IP