What a week : getimagesize with URIs

Mar 12, 2004

Been a busy week at work, so I haven't really had much of a chance to script lately. I could release the Knowledgebase soon, but so far I haven't written up the readme.txt file. I should get around to that soon :|

In other news, I bought some stock in Opera Software two days ago :) I have some other stock, but this is the first company I've invested in which affects me personally. I've used Opera for almost a year now and I'd never look at anything else. Especially since I'm on dialup at home, and the Opera download is only 3.7MB! Amazing!

In my nooks and crannies of spare time this week, I found a bit of PHP code which allows you to use URI's in the getimagesize() function. PHP only added support for it in 4.0.5. Most installations of PHP are later than this of course, but it's useful for totally backwards compatibility. I found it on PHP.net but I recoded it for my own use as below:

function getimagesize_remote($image_url) {
  $handle = @fopen($image_url, "rb");
  $contents = "";
  if ($handle) {
    while ($data = fread($handle, 8192)) $contents .= $data;
    fclose($handle);

    $im = imagecreatefromstring($contents);
    if ($im) {
      $gis[0] = imagesx($im);
      $gis[1] = imagesy($im);
      $gis[3] = "width=\"{$gis[0]}\" height=\"{$gis[1]}\"";
      imagedestroy($im);
      return $gis;
    }
  }
  return false;
}

Until next time!


Comments closed

Recent posts

  1. Book Review - The Forever Man by Gordon R Dickson Jun 2025
  2. How to calculate the day of the week from a date... in your head Mar 2025
  3. Version 1.54 of the Virtual Keyboard Interface Javascript Released Nov 2024
  4. Customize Clipboard Content on Copy: Caveats Dec 2023
  5. Orcinus Site Search now available on Github Apr 2023

Archive