Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to get color of image for the background using PHP

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 1.59k
    Comment on it

    Hello Readers! If you are developing the dynamic website in which the image color will be the background color then you can see it doing in PHP :-

     <?php
          $filename = $_GET['filename'];    
          $image = imagecreatefromjpeg($filename);
          $width = imagesx($image);
          $height = imagesy($image);
          $pixel = imagecreatetruecolor(1, 1);
          imagecopyresampled($pixel, $image, 0, 0, 0, 0, 1, 1, $width, $height);
          $rgb = imagecolorat($pixel, 0, 0);
          $color = imagecolorsforindex($pixel, $rgb); //you are getting the most common colors in the image
        ?>
    

    And the html part will go like this:-

    <html>
      <head>
        <title>Getting BG color</title>
      </head>
      <body style='background-color: rgb(<?php echo $color['red'] ?>, <?php echo $color['green'] ?>, <?php echo $color['blue'] ?>)'>
        <form action='' method='get'>
          <input type='text' name='filename'><input type='submit'>
        </form>
        <img src='<?php echo $filename ?>'>
      </body>
    </html>
    

    By running the code above the color which is most common will be shown in the background.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: