Skip to content

Instantly share code, notes, and snippets.

@mgks
Last active May 7, 2023 10:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mgks/3229e57322314bb54eec6efb5c3bb7cc to your computer and use it in GitHub Desktop.
Save mgks/3229e57322314bb54eec6efb5c3bb7cc to your computer and use it in GitHub Desktop.
Extracting information out of an image using PHP's internal libraries.
<?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