How to detect if a image is a valid file before LoadImage?

Just starting out? Need help? Post your questions and find answers here.
ricardo
Addict
Addict
Posts: 2438
Joined: Fri Apr 25, 2003 7:06 pm
Location: Argentina

How to detect if a image is a valid file before LoadImage?

Post by ricardo »

Hi,

Im downloading images in run time and trying to load them, but since i find some error, i am trying to detect the file size:

Code: Select all

THIS IS NOT A CODE, is just showing what i am doing


Downloading images:
r = ReceiveHTTPFile(DireccionImagen$, ImgDir$)


Checking the size
FileSize(ImgDir$) < 1500

Need to check before LoadImage, because many times i get an error

ARGENTINA WORLD CHAMPION
BarryG
Addict
Addict
Posts: 4219
Joined: Thu Apr 18, 2019 8:17 am

Re: How to detect if a image is a valid file before LoadImage?

Post by BarryG »

ricardo wrote: Wed Jun 12, 2024 10:12 pmmany times i get an error
You'd be better off explaining what the error is. There's no quick and easy way to tell if an image is valid before loading (anything could be corrupt in the header and/or body of the image), plus it's double the work for your app because it has to read the file twice (once to check for errors, and again to LoadImage it).
ricardo
Addict
Addict
Posts: 2438
Joined: Fri Apr 25, 2003 7:06 pm
Location: Argentina

Re: How to detect if a image is a valid file before LoadImage?

Post by ricardo »

BarryG wrote: Wed Jun 12, 2024 10:15 pm
ricardo wrote: Wed Jun 12, 2024 10:12 pmmany times i get an error
You'd be better off explaining what the error is. There's no quick and easy way to tell if an image is valid before loading (anything could be corrupt in the header and/or body of the image), plus it's double the work for your app because it has to read the file twice (once to check for errors, and again to LoadImage it).

Hi, Thanks for your answer.

"The specified image does not exist"
ARGENTINA WORLD CHAMPION
boddhi
Enthusiast
Enthusiast
Posts: 524
Joined: Mon Nov 15, 2010 9:53 pm

Re: How to detect if a image is a valid file before LoadImage?

Post by boddhi »

ricardo wrote: "The specified image does not exist"
Silly questions:
Is the format the same for all images?
Are you sure of the format of the image you receive? Nowadays, many images on websites are in WEBP format, which is not supported by PB.
Are you sure that all the necessary image plugins have been called up?

Do the errors always occur for the same images or is it random?
If first case, can you provide a link to an image with which there is a problem, so that we can test?
If my English syntax and lexicon are incorrect, please bear with Google translate and DeepL. They rarely agree with each other!
Except on this sentence...
benubi
Enthusiast
Enthusiast
Posts: 227
Joined: Tue Mar 29, 2005 4:01 pm

Re: How to detect if a image is a valid file before LoadImage?

Post by benubi »

You can also use HTTPInfo() to get the status code and return header fields.

Then you check the status if it's 200; and/or you check the content-type field of the header if it starts with "image/" .

This way you could filter out most problems aside of unsupported file formats.

Code: Select all

Procedure.s HTTP_HeaderElement(Header$, Name$)
  Protected result$
  Protected ls,le
  ls=FindString(Header$, #CRLF$+name$+": ",1,#PB_String_NoCase)
  If ls
    ls = ls + Len(name$) + 4
    le = FindString(Header$, #CRLF$, ls)
    result$=Mid(header$,ls,le-ls)  
  Else 
   ; Debug "not found"
  EndIf 
  ProcedureReturn result$
EndProcedure


req = HTTPRequest(#PB_HTTP_Get,"https://www.google.com/")
If req
  Debug "req:"+req
  
  
  If HTTPInfo(req, #PB_HTTP_StatusCode)<>"200"
    Debug "oh,oh! statust code: "+HTTPInfo(req, #PB_HTTP_StatusCode)
  Else 
    Debug "status code 200 / OK"
  EndIf 
  
  Define hdr$    = HTTPInfo(req, #PB_HTTP_Headers)
  
  Define ctype$  = HTTP_HeaderElement(hdr$,"content-type")
  
  Debug "Content-type:"
  Debug ctype$
  
  
  If LCase(StringField(ctype$,1,"/"))="image"
    Debug "THIS IS AN IMAGE!"
  Else 
    Debug "ERROR: NO IMAGE"
  EndIf 
  
EndIf 

ricardo
Addict
Addict
Posts: 2438
Joined: Fri Apr 25, 2003 7:06 pm
Location: Argentina

Re: How to detect if a image is a valid file before LoadImage?

Post by ricardo »

boddhi wrote: Fri Jun 14, 2024 10:17 pm
ricardo wrote: "The specified image does not exist"
Silly questions:
Is the format the same for all images?
Are you sure of the format of the image you receive? Nowadays, many images on websites are in WEBP format, which is not supported by PB.
Are you sure that all the necessary image plugins have been called up?

Do the errors always occur for the same images or is it random?
If first case, can you provide a link to an image with which there is a problem, so that we can test?
All are .jpg
ARGENTINA WORLD CHAMPION
Post Reply