Search code examples
rets

How can I display binary image data as an Image in PHRETS


I am using http://otw.rets.interealty.com/Login.asmx/Login I am getting image as Binay Data. How can I display a binary data from RETS as an Image. Here is my code

$sysid = $data['sysid'];
$photos = $rets->GetObject("Property", "Photo", $sysid, "*", 1);
echo $photos[0]['Data'];

Solution

  • According to the PHRETS documentation for GetObject, the last argument in GetObject, $location, can either be a "0" or "1". "1" returns the image's URL string and "0" returns the binary image data.

    #1 Encoding the image data and outputting to browser without saving to a file. From this SO question

    $photos = $rets->GetObject("Property", "Photo", $sysid, "*", 0);
    foreach ($photos as $photo) 
    {
    
        if ($photo['Success'] == true) 
        {
            $contentType = $photo['Content-Type'];
            $base64 = base64_encode($photo['Data']); 
            echo "<img src='data:{$contentType};base64," . $base64 . "' />";
        }
        else 
        {
            echo "({$photo['Content-ID']}-{$photo['Object-ID']}): {$photo['ReplyCode']} = {$photo['ReplyText']}<br />";
        }
    
    }
    

    #2 Saving the image to a file and then displaying it. From PHRETS

    $photos = $rets->GetObject("Property", "Photo", $sysid, "*", 0);
    foreach ($photos as $photo) 
    {
    
        if ($photo['Success'] == true) 
        {
            file_put_contents("image-{$listing}-{$number}.jpg", $photo['Data']);
            echo "<img src='image-{$listing}-{$number}.jpg' />";
        }
        else 
        {
            echo "({$photo['Content-ID']}-{$photo['Object-ID']}): {$photo['ReplyCode']} = {$photo['ReplyText']}<br />";
        }
    
    }