Link to home
Start Free TrialLog in
Avatar of petervullings
petervullings

asked on

Getting image dimensions and size over HTTP with TNMHTTP component

Hi There,

I am attempting to get image information by requesting the image header via HTTP using the TNMHTTP component.  I don't want to download the whole image to find out this information to save bandwidth.

There is a command:
http1.head('http://path.to/image.jpg');

But I cant seem to find where to get the image dimension and file size information out of this.

I notice that many browsers can tell you the size and dimensions of an image before it downloads it (without the image size being set in the HTML even).

Thanks in advance
Pea
ASKER CERTIFIED SOLUTION
Avatar of Eddie Shipman
Eddie Shipman
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
There was no need to post a second question. I would have the one deleted where you
do not accept an answer..

Anyway, Here is how to do it with Indy components:

function GetFileSize(AURL: String): Integer;
var
  idHTTP1: TidHTTP;
begin
  Result := 0;
  idHTTP1 := TidHTTP.Create(nil);  
  try
    idHTTP1.Head(AURL);
    Result := idHTTP1.Response.ContentLength;
  finally
    idHTTP1.Free;
  end;
end;

Now with NMHTTP:

function GetFileSize(AURL: String): Integer;
var
  NMHTTP1: TNMHTTP;
  i: Integer;
  s: String;
  slHeader: TStringList;
begin
  slHeader := TStringList.Create;
  Result := 0;
  NMHTTP1: TNMHTTP.Create(nil);
  try  
    NMHTTP1.InputFileMode := False;
    NMHTTP1.Head(AURL);
    slHeader.Text := NMHTTP1.Header;
    for i := 0 to slHeader.Count-1 do
      if Pos('Length', slHeader[i]) > 0 then
        s := slHeader[i];
    Result := StrToInt(Trim(Copy(s, 16, Length(S))));
  finally
    NMHTTP1.Free;
    slHeader.Fre;
  end;
end;
Please accept the answer if it helped you solve your problem. That is the spirit of this forum.
Avatar of petervullings
petervullings

ASKER

Thanks Eddie. I'm well aware of the spirit of it, being a contributer myself (though a little infrequent). Thanks for the reminder :)
I still have found no way to get the dimensions without looking at the IMG tag. But if the IMG tag doesn't
have the dimensions in it, you'd be out of luck.