Last active
May 7, 2023 10:54
-
-
Save mgks/3229e57322314bb54eec6efb5c3bb7cc to your computer and use it in GitHub Desktop.
Extracting information out of an image using PHP's internal libraries.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// loading image: using PHP's GD library to load the image from its file or URL | |
$img = imagecreatefromjpeg('image.jpg'); | |
// getting image size: using getimagesize() function to get the width and height of the image | |
list($width, $height) = getimagesize($img); | |
// extracting image metadata: using exif_read_data() function to extract the image metadata, including camera settings, location, and time | |
$exif = exif_read_data($img); | |
// extracting image colors: using imagecolorat() function to extract the color of each pixel in the image | |
$colors = array(); | |
for ($x = 0; $x < $width; $x++) { | |
for ($y = 0; $y < $height; $y++) { | |
$color = imagecolorat($img, $x, $y); | |
$colors[] = array( | |
'r' => ($color >> 16) & 0xFF, | |
'g' => ($color >> 8) & 0xFF, | |
'b' => $color & 0xFF, | |
); | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment