1

I wrote this code to display an image:

<?php
$sel = " SELECT *
         FROM immagini
         WHERE id= ".$_GET['idx'];

$ris = mysqli_query($con,$sel);      
$count = mysqli_num_rows($ris);     

if($count >= 1)  echo "<h1>Dettagli dello studente: </h1>";

while ( $riga = mysqli_fetch_array($ris)) {         

    echo "ID  = ".$riga['id']; ?> <br > <?php
    echo "Nome = ".$riga['name']; ?> <br > <?php
    echo "Descrizione = ".$riga['descrizione']; ?> <br > <?php     

    $imageData= $riga["image"];
    header("content-type :image/jpeg");
    echo $imageData;                 
    }
?>

But the result is this:

Dettagli dello studente: ID = 2 Nome = julia Descrizione = Questo frattale ha delle caratteristiche particolari, in quanto..... ����JFIF,,��C !"$"$��C����� ���}!1AQa"q2���#B��R��$3br� %&'()*456789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz��������������������������������������������������������������������������� ���w!1AQaq"2�B���� #3R�br� $4�%�&'()56789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz��������������������������������������������������������������������������?��ĺޙ��}cW�6�Pm�$ύ�p����7���bjT��8�������?��?�A�.����W��>�����E�W3/�����-��K��XI~��}�%�6����A\�;�8$��^I�U}���k��]޺���L�< ��Q�����l�4��ܗ��ӳ������� �gK�f�g���� y4���F@�� ��gC+���-+7�(���}�� �fhA�o����*��������u���b���?m���t#�:u������]#Ȯ� �Tѕ����{���˯�7I�j�� �_R3�P�|d��+���u��%�+�1?�������+���u6c���w�m�����5H���o�m�����5@�5/�����w�j��j_���:7� �����Կ�to�����?��~�����+���T�R� ��ѿ�Ww�ƨ�����C������P� I�;�9�� ����;��P��_�3�q� ˯�7T��.��+�h��>��7'��w�j�r�ڋ��1W�����w�Ʃ�..� 4�B�R� ��ѿ�Ww�Ʃ?��~�����+���T�R� ��ѿ�Ww�ƨ�����C������P� K�7��F��]������o�������5@�5/����w�j��jO���9�� �����ԟ��so�����j-��?�����m�����7U(J;�;�����3o�����J �1����"�q��������{ ���L|���?� ���u�r�L����Qob������^)f�?���N�Y^;���mK R��8������3���X�Z;���)�X��?�e��7|3���#��sg8��*>�S������Ͽ��e~/�=9��������>�}��V�/�����$O�>w �����I���N��A>�R�����,��/��]�z}��&����ac�~O��4_�#(5Sϭ�����j����|dw���̵��97���틏�K��/�Gx�(���y���f��K��=H��F���?���.x��rB�mi�����7�Q�".F/�Ɲ�?����!�������8x��O��;�{����i������x���(��F'�ޛ�?����#�r�i��^����G��{9 �������8��G�����u�(u���m���G�g�Q��g�^��� ~�=����Q�i��.y�q�i�t�� u�4u�?����!�!��iK������{H������(#;�yj��i�:�GOz=�E��{$r?>[9I|F2?�i3�%Z���Ռϗ�����N_� �8���pO�x�t?ܭcFr�n#_�UY��t��/�� ��(S��e�wF���;�y�_N:�t'��8Ez��7)�d����;���3 'T�����N��'�,�wBx�3ұ���K+�Fr���鶝|���'��,�?�����@��;E����t���CR%��!�x�F�]n�����G�)�d�Q��H�

How can i solve this problem????? I think there is a problem with the conversion of the image. Thanksss.

  • As a side note, your opening and closing PHP tags is a bit unnecessary. You stay in PHP and echo the "ID =" but then break out of PHP to echo the BR. Instead of what you are doing: `echo "ID = ".$riga['id']; ?>
    ";`
    – James Mar 17 '15 at 16:56
  • 1
    Storing complete images in database is not good practice. Store the image in your filesystem (where it belongs) and reference the image in your database (store path etc.) –  Mar 17 '15 at 16:58
  • Does this even work? Since you're outputting stuff before the `header()` call. –  Mar 17 '15 at 17:01

4 Answers4

1
echo "ID  = ".$riga['id']; ?> <br > <?php
echo "Nome = ".$riga['name']; ?> <br > <?php
echo "Descrizione = ".$riga['descrizione']; ?> <br > <?php   

After so many echoes you are sending

 header("content-type :image/jpeg");

Which is meaningless there, hence the messed up output. If you want to send image content then you can only send that in response with proper headers. You cannot mix image and text both in one response.

TLDR; Remove every other echo than the image content with its header.

Hanky Panky
  • 46,730
  • 8
  • 72
  • 95
1

When you have to Output an Image out of a DB ( which is basically a bad idea though ) make sure that you do NOT echo anything, just send the Header like this:

    <?php

    $imageData= $riga["image"];
    header("content-type :image/jpeg");
    echo $imageData;                 

?>

but you should re-write your code a bit to make it even more usable.

0

I agree w/ Hanky Panky, you cannot mix image and text which need different output methods.

assuming base64 is used, instead of

$imageData= $riga["image"];
header("content-type :image/jpeg");
echo $imageData;     

can you try

$imageData= "<img src='data:image/jpeg;base64,".$riga["image"]."' />";
echo $imageData;

Also, How to display Base64 images in HTML? provides more info which could help you

Community
  • 1
  • 1
evren
  • 11
  • 2
0

In addition to the point made by Hanky Panky and WongFailHung, you can't output more than in image in response to the same request. Actually, you can send the data but you'll only see the first image.

symcbean
  • 47,736
  • 6
  • 59
  • 94