how to decode this type of data in php?

I have one page created in PHP, i get parameter encoded format, basically parameter is encoded URL of image GIF , so i want to decode and see the image path or image URL

I received data like

HTTP/1.1 200 OK Server: nginx/0.7.65 Date: Fri, 22 May 2020 12:21:39 GMT 
Content-Type: image/gif
Connection: close X-Powered-By: PHP/5.3.2-1ubuntu4.7ppa5~lucid1
Set-Cookie: PHPSESSID=l5228rk9nk1v1im6ssd4svr502;
path=/
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0 
Pragma: no-cache

GIF89a3 ������������������
0�Ç7BD(�Ƌ�78с@�fܐ��Ɉ�H��#�pq�� #z@ ��N�� ���5���A�5E����ËnD��B���4(�� ��7\P��$ h�tq�ԆjD�a��.Z�ha)Y�9 ĸ��d��H,�w`�$P,�x (?�)�bǕox(`�H�R�h{���!�Ђ���^(@-��"o�a��]wP�)ߢ[$���C0��8CC��P�_�p�I1;

1 answer

  • answered 2020-05-22 12:45 Kunal Raut

    This string is basically the contents of the file achieved from the file_get_contents() function.Here you cannot get the actual path of the string again but you can view the gif by using imagegif() function,the only thing you need to do here is first encode the string and then decode it with the base64_encode() and base64_decode respectively.

    For more details:https://www.php.net/manual/en/function.imagegif.php

    Your code should be more like

    <?php
    $img = 'GIF89a3 ������������������
    0�Ç7BD(�Ƌ�78с@�fܐ��Ɉ�H��#�pq�� #z@ ��N�� ���5���A�5E����ËnD��B���4(�� ��7\P��$ h�tq�ԆjD�a��.Z�ha)Y�9 ĸ��d��H,�w`�$P,�x (?�)�bǕox(`�H�R�h{���!�Ђ���^(@-��"o�a��]wP�)ߢ[$���C0��8CC��P�_�p�I1;';
    
    $data = base64_encode($img);
    
    $data1 =base64_decode($data);
    
    $result = imagecreatefromstring($data1);
    if ($img !== false) {
        header('Content-Type: image/gif');
        imagegif($result);
        imagedestroy($result);
    }
    else {
        echo 'An error occurred.';
    }
    ?>