Search code examples
javascriptencodingpngautodesk

Display Encoded PNG


I am using Autodesk View and Data API it's items API is returning thumbnail in some form which I am unable to understand how to display this. Following is the string returned from it.

�PNG\r\n\u001a\n\u0000\u0000\u0000\rIHDR\u0000\u0000\u0000�\u0000\u0000\u0000�\b\u0002\u0000\u0000\u0000\":9�\u0000\u0000\u0000\u0003sBIT\b\b\b��O�\u0000\u0000\u0003UIDATx����nSW\u0014@��ش���H(��>\u0010\u0001RE�vw3�3Z,��O\u0012��\u001c�m\u001b��˳\u0017��IX$�EBX$�EBX$�EBX$�EBX$�EBX$�EBX$�EBX$�EBX$�EBX$�EBX$�EBX$�EBX$�E�t�&so�9�\u001d�*3��\u000b�:3��������u���̌Ʈ��G!\ta�\u0010\u0016\ta�\u0010\u0016\ta�\u0010\u0016\ta�\u0010\u0016\ta�\u0010\u0016\ta�\u0010\u0016\ta�\u0010\u0016\ta�\u0010\u0016\ta�8m�\u0014�ޯ��;3\u001a�Ъ�6��!9I=�Q�UfFcWYՙw*�\"!,\u0012�\"!,\u0012�\"!,\u0012�\"!,\u0012�\"!,\u0012�\"!,\u0012�\"!,\u0012�\"!,\u0012�\"!,\u0012.S�?3\u001a�Ъ.ST3�����2\u0005\u0015a�\u0010\u0016\ta�\u0010\u0016\ta�\u0010\u0016\ta�\u0010\u0016\ta�\u0010\u0016\ta�\u0010\u0016\ta�\u0010\u0016\ta�\u0010\u0016\ta�p�}���؅Vu潚\u0019�]eUgީ\b���H\b���H\b���H\b���H\b���H\b���H\b���H\b�A^/�m�^⁄�\b�_�v�/��ݟ�ʣ\b+�z��*GNj�����v��p���y�]����Ǟ�s���e���j�L�f��\u0004��������N���l��m��mf����O�\u001f���v�*��q\u000e�p�'\u0012��������|��w��!��ݿ�ɟ}Z���\u0013-�2+�������o�\f�ې�=�����0��ҳ\bk&����n���玲ȣ���H\b���H\b���H\b���H\b���H\b���H\b��3��ό�.��3���h�*��\u0005\u0002T�EBX$�EBX$�EBX$�EBX$�EBX$�EBX$�EBX$�EBX$�EBX$\\��f4v�U]��fFcWY�e\n*�\"!,\u0012�\"!,\u0012�\"!,\u0012�\"!,\u0012�\"!,\u0012�\"!,\u0012�\"!,\u0012�\"!,\u0012�\"qx�l���}�{G���h�B���aے���<\nI\b���H\b���H\b���H\b���H\b���H\b���H\b���H\b���H\b���H\b���H\b���H\b���H\b���H�\u0005�\u000e�e�-S\u0002\u0000\u0000\u0000\u0000IEND�B`�

My queston is how to display this type of encoded string as PNG in html. The above output was received by writing the following code in javascript.

 function getThumbnails(urn) {
        var urnStr = urn;
        if (!urnStr || (0 === urnStr.length)) {
            alert("You must specify a URN!");
            return;
        }
        var urlStr = _baseURL + '/viewingservice/v1/thumbnails/' + urnStr;

        var jqxhr = $.ajax({
            url: urlStr + '?width=150&height=150',
            type: 'GET',
            headers: {
                "Authorization": "Bearer " + getAccessToken()
            },
            beforeSend: startSpinner("spn_viewTranslationStatus")
        })
            .done(function (ajax_data) {
                console.log(JSON.stringify(ajax_data, null, '   '));
                var download = document.createElement('a');
                //download.href = 'data:image/png;'+ajax_data+'';
                download.href = ajax_data + '';
                var r = /\\u([\d\w]{4})/gi;
                var x = ajax_data.replace(r, function (match, grp) {
                    return String.fromCharCode(parseInt(grp, 16)); } );
                x = unescape(x);
                console.log(x);
                download.href = 'data:image/png;'+x+'';
                download.download = 'reddot.png';
                download.click();
                $('body').append('<img src="data:image/png;base64,' + Base64.encode(x) + '"/>'),
                    stopSpinner();
            })
            .fail(function (jqXHR, textStatus) {
                //$("#txt_resViewTranslateStatus").html(ajaxErrorStr(jqXHR));
                stopSpinner();
            });
    }

Solution

  • Actually I was receiving a PNG from server and it was not displaying in javascript notation properly so I wrote a curl in PHP

        $ch = curl_init();
    
    // set URL and other appropriate options
    curl_setopt($ch, CURLOPT_URL, "https://developer.api.autodesk.com/viewingservice/v1/thumbnails/urn");
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
        'Authorization:Bearer x8KaVvMxZTxliTjYPiBTmpL0JaIC' ));
    
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    // grab URL and pass it to the browser
    $out = curl_exec($ch);
    $information = curl_getinfo($ch);
    //var_dump($information );
    
    // close cURL resource, and free up system resources
    curl_close($ch);
    
    echo $out;
    $fp = fopen('myfile.png', 'w');
    fwrite($fp, $out);
    fclose($fp);
    

    And it shows the file as PNG completely .