Know the image type?

I am parsing some pages on a government site and they contain image links like
this:


image_render.aspx?item=011739056&sz=lg

When I navigate to this URL, it doesn’t show the image, it shows code:

�����JFIF��d�d�����

I have used this code in the past to get these images but I have no idea how to determine the image TYPE because some of them are GIF.

Using simple_html_dom class to parse the HTML after Tidy’ing it up.


$image = $html2->getElementById('hlHeadImage');
if (isset($image)) {
  if (strpos($image->children(0)->src, 'clearpixel') == 0) {
    $link = $image->children(0)->src;
    $path_parts = pathinfo($link);
    $ext = $path_parts['extension'];
    $image_path =  "part_images/".$ITEM_NO.".".$ext;
    @copy($link."&sz=lg", $image_path);
  }
} else {
  $image = $html2->getElementById('hlLargeImage');
  if (isset($image)) {
    if (strpos($image->children(0)->src, 'clearpixel') == 0) {
      $link = $image->children(0)->src;
      $path_parts = pathinfo($link);
      $ext = $path_parts['extension'];
      $image_path =  "part_images/".$ITEM_NO.".".$ext;
      @copy($link, $image_path);
    }
  }
}

Question is, how can I download these images and maintain the extension?

use getimagesize() and check the IMAGETYPE_XXX constant.