1

I'm calling a RETS based service to gather data about property listings ... and of course, one fo the main aspects about this is getting photos. However, the data for a photo is literally the raw image data:

ÿØÿàJFIF``ÿá"ExifII*îhÿÛC       $.' ",#(7),01444'9=82<.342ÿÛC     2!!22222222222222222222222222222222222222222222222222ÿÀU"ÿÄ    ÿĵ}!1AQa"q2‘¡#B±ÁRÑð$3br‚  %&'()*456789:CDEFGHIJSTUVWXYZcdefghijstuvwxyzƒ„…†‡ˆ‰Š’“”•–—˜™š¢£¤¥¦§¨©ª²³´µ¶·¸¹ºÂÃÄÅÆÇÈÉÊÒÓÔÕÖרÙÚáâãäåæçèéêñòóôõö÷øùúÿÄ    ÿĵw!1AQaq"2B‘¡±Á  #3RðbrÑ $4á%ñ&'()*56789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz‚ƒ„…†‡ˆ‰Š’“”•–—˜™š¢£¤¥¦§¨©ª²³´µ¶·¸¹ºÂÃÄÅÆÇÈÉÊÒÓÔÕÖרÙÚâãäåæçèéêòóôõö÷øùúÿÚ?÷ú(¢€ (¢€ (¢€ (¢€ (¢€ (¢€ (¢€ (¢€ (¢€ (¢€ (¢€ 

etc, etc, etc ...

How do I convert/use this raw data and turn it into a physical file on the web server?

j08691
  • 204,283
  • 31
  • 260
  • 272
dcolumbus
  • 9,596
  • 26
  • 100
  • 165

3 Answers3

2

Try simply writing this raw data to a file. If you are certain that the file format is always JPG, then set .jpg as the extension.

If not, you might want to have a look here - this should help you in getting the right file format and then set the appropiate file extension.

EDIT I don't know the RETS protocol, but it might return content type of your image, which should simplify the whole process even more. :)

Community
  • 1
  • 1
Pateman
  • 2,727
  • 3
  • 28
  • 43
0

Also, if you're getting multiple jpegs in the stream when using a GetObject request for all photos of a given listing, you can split on the ÿØÿà bytes (\xff\xd8\xff\xe0) and save each byte stream resulting from the split as a jpeg file, but you have to retain the ÿØÿà at the beginning of each jpeg, so you'll need to reinsert them at the beginning of each chunk after doing the split. I encountered this from a "RETS-like" service as well and had to manually add subscripts (_1, _2, etc) to the jpeg file names resulting from the split. It ended up being something like:

if response.find('\xff\xd8\xff\xe0') > -1:
    blist = response.split('\xff\xd8\xff\xe0')
    blist.remove("")  /* get rid of any empty list elements resulting from split */
    index = 1
    for img in blist:
        file_nm = os.path.join(photo_dir, "{0}_{1}.jpg".format(photo_id, index))
        with open(photo_file_nm, "wb") as photo_file:
            photo_file.write('\xff\xd8\xff\xe0' + img)
        index += 1

...where photo_dir and photo_id are defined outside this block. This example is python, but could translate pretty easily to php.

TonyM
  • 236
  • 1
  • 8
0

You have to parse the mime multipart object from the RETS server. You can do it manually getting each line and chunking it at the mime seperator string which you have to also figure out where it is in the headers.

If you want to save yourself time, look at the phrets class source code (or just use it). https://github.com/troydavisson/PHRETS/blob/master/phrets.php#L141

Here are some examples, https://github.com/troydavisson/PHRETS/wiki/GetObject

Anthony Hatzopoulos
  • 10,437
  • 2
  • 40
  • 57