0

PHP creates the text from metadata from image file which stores it in variable of the same name of IMG source. No Problems

JavaScript displays the gallery to show images, scroll & enlarge.
During enlargement it gives description of image from source attribute but always displays the text with � after every character. But PHP doesn't.

I've tried encoding which you will see from code & & echo header("Content-Type: text/html; charset=ISO-8859-1");

PHP:

    $path = "gallery/";
    $objs = new RecursiveIteratorIterator(new 
    RecursiveDirectoryIterator($path), 
    RecursiveIteratorIterator::SELF_FIRST);
    $num = 0;
    foreach ($objs as $pic){
        $pic = str_replace('gallery/', '', $pic);
        if ($pic == '..' or $pic == '.'){
            continue;
        }
        $exif = exif_read_data("$DIR/gallery/$pic", 0, true);
        $pic = str_replace('.JPG', '', $pic);
        ${$pic} = $exif['IFD0']['Comments']; ///// php Variable
        echo "<script> var $pic = '".$exif['IFD0']['Comments']."'; 
              var $pic = utf8_encode($pic);</script>"; 
        // JS of variable of same name.
    }
Sloppy but Works

JQuery:
    $('#mSide').on('click',function(){
        var src = $('#mPic').attr('src');
        var v = src.replace('/gallery/','');
        v = v.replace('.JPG','');
        v =  unescape(encodeURIComponent(v));
        $('#fullPic').show();
        $('#fullPic').append('<img class=\"fPic\" src=\"'+src+'\" 
    height=\"90%\" widdth=\"90%\" style=\"margin-left:0px;\" />');
        $('#fullPic').append('<span id=\"picSummary\" 
    style=\"color:#FFFFFF;\" >testing <textarea > '+  window[v] +'</textarea> 
    </span>');
        $('#fullPic').prepend('<input class=\"closeFullP headr\" 
   value=\"Close\" READONLY><br class=\"closeFullP\">');
   }); 

Sloppy but Works and window[v] is the call

Should display: this is the new val = NOTICE: This e-mail message may contain legally privileged and/or confidential information. If you are not the intended recipient, you are hereby notified that any dissemination of the contents of this message is strictly prohibited. If you have received this message in error, please immediately notify the sender at ########### and delete all copies of this e-mail message and its attachments.

Not: N�O�T�I�C�E�:� �T�h�i�s� �e�-�m�a�i�l� �m�e�s�s�a�g�e� �m�a�y� �c�o�n�t�a�i�n� �l�e�g�a�l�l�y� �p�r�i�v�i�l�e�g�e�d� �a�n�d�/�o�r� �c�o�n�f�i�d�e�n�t�i�a�l� �i�n�f�o�r�m�a�t�i�o�n�.� �I�f� �y�o�u� �a�r�e� �n�o�t� �t�h�e� �i�n�t�e�n�d�e�d� �r�e�c�i�p�i�e�n�t�,...............

KevyTeky
  • 29
  • 4
  • 1
    The JS file / the main page itself is most likely saved as UTF-8 encoded ..? Make sure the encoding of the files and headers match. – Teemu Feb 01 '19 at 20:38
  • thanks for a quick response Teemu. It all runs from same Index.php, I do have other php pages and a directory where I put the img files but no js files... all runs from src='https://www.w3schools.com/lib/w3.js' – KevyTeky Feb 01 '19 at 21:16
  • 1
    Does any of the EXIF fields specify an encoding? It looks like it might be UTF-16, and you have to convert to your page encoding. – Joni Feb 01 '19 at 21:27
  • Thanks Joni, just tried those suggestion, but now believing its a pure JS issue as PHP has no problem displaying... but JS does. I found this SO page from your comment but they are having the same issue but with PHP. page: https://stackoverflow.com/questions/18826489/wrong-encoding-when-getting-metadata-form-image-with-exif-read-data – KevyTeky Feb 01 '19 at 21:45
  • That's not UTF-8, it's UTF-16LE. – Sammitch Feb 01 '19 at 22:50

1 Answers1

1

Never use utf8_encode() or utf8_decode() they make awful assumptions and happily/silently corrupt your data. Always use a function that allows you to explicitly specify your input and output encodings.

The data you're extracting from EXIF is encoded as UTF16-LE, and your page is... something else. I can't tell what and you should probably figure that out and be consistent.

Suggested reading: UTF-8 all the way through

If your page is encoded as ISO-8859-1:

$proper = mb_convert_encoding($input, 'ISO-8859-1', 'UTF-16-LE');

If your page is encoded as UTF-8:

$proper = mb_convert_encoding($input, 'UTF-8', 'UTF-16-LE');

Ref: https://secure.php.net/manual/en/function.mb-convert-encoding.php

Sammitch
  • 30,782
  • 7
  • 50
  • 77
  • Thanks Sammitch will delve into this and see what works. – KevyTeky Feb 01 '19 at 23:22
  • This preg_replace fixed the issue: $store = preg_replace('/[\x00-\x1F\x7F-\xFF]/', '', $exif['IFD0']['Comments']); – KevyTeky Feb 04 '19 at 23:16
  • @user2612821 ok, but that's going to ruin any data that contains anything other than the english alphabet and common punctuation. It is an ugly kludge that you will ultimately regret. – Sammitch Feb 04 '19 at 23:30
  • Do you mean i should convert it anyway. I will as I notice this problem wasn't in PHP which made the variable only in JS which was a copy of that variable. Thanks Sammitch. – KevyTeky Feb 05 '19 at 16:44