Page 1 of 1

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

Posted: Wed Jun 12, 2024 10:12 pm
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


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

Posted: Wed Jun 12, 2024 10:15 pm
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).

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

Posted: Fri Jun 14, 2024 5:40 pm
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"

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

Posted: Fri Jun 14, 2024 10:17 pm
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?

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

Posted: Sat Jun 15, 2024 11:40 am
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 


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

Posted: Sun Jun 16, 2024 4:03 pm
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