Solved

Getting image dimensions and size over HTTP with TNMHTTP component

Posted on 2003-11-03
5
513 Views
Last Modified: 2007-12-19
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
0
Comment
Question by:petervullings
  • 4
5 Comments
 
LVL 26

Accepted Solution

by:
EddieShipman earned 125 total points
ID: 9673259
Youu can get the file SIZE by checking the Content-Length header field but you can not get
the dimensions from the http stream. Now the IMG tag can, and will, most likely have the
dimensions in it and that is where the browser gets it unless it doesn't have them then
it is calculated after it is loaded.

Are you talking GIF or JPG or PNG here, anyway?

Would you be willing to settle for a solution using Indy as I do not do Netmasters components
because they are buggy and haven't bee updated since 1998.
0
 
LVL 26

Expert Comment

by:EddieShipman
ID: 9673578
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;
0
 
LVL 26

Expert Comment

by:EddieShipman
ID: 9771194
Please accept the answer if it helped you solve your problem. That is the spirit of this forum.
0
 
LVL 1

Author Comment

by:petervullings
ID: 9777086
Thanks Eddie. I'm well aware of the spirit of it, being a contributer myself (though a little infrequent). Thanks for the reminder :)
0
 
LVL 26

Expert Comment

by:EddieShipman
ID: 9779179
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.
0

Featured Post

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
proper way to parse text with delphi 7 121
How to debug For loops? 3 49
How to define IfThen functions in one common unit? 4 54
Delphi: Connect to running MS Outlook 4 87
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

778 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question