0

After a week of Google and Stackoverflow suggestions, I have to ask. What am i doing wrong?

Overall goal is to encrypt data at rest with CodeIgniter 3 encrypt/decrypt but I have boiled the code to remove those step to try to get the basics to work.

Currently, I upload a jpg, save it to disk, read the file, base64_encode it and save it back. Then I read the file, base64_decode it, use imagecreatefromstring() and imagejpeg() to output it.

I have two results. Without adding the Content-Type header I see

����JFIFxx��>CREATOR: gd-jpeg v1.0 (using IJG JPEG v80), default quality ��C $.' ",#(7),01444'9=82<.342��C 2!!22222222222222222222222222222222222222222222222222����"�� ���}!1AQa"q2���#B��R��$3br� %&'()*456789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz��������������������������������������������������������������������������� ���w!1AQaq"2�B���� #3R�br� $4�%�

--snipped-- I see no errors displayed that would be adding headers and I am not saving this out to a file that might contain spaces before the php tags.

When I add it I get:

This image "

Boiled down code

# open file to read it
$fhr = fopen($path_file, 'r+b');

# read entire file contents
$file_as_text = fread($fhr, filesize($path_file));
$type = pathinfo($path_file, PATHINFO_EXTENSION);

# encode
#$file_as_text = 'data:image/' . $type . ';base64,' . base64_encode($file_as_text);
$file_as_text = base64_encode($file_as_text);

# open for writing
$fhw = fopen($path_file, 'w+b');

# write the file
fwrite($fhw, $file_as_txt);

#On decode
$cipher_txt = fread($fh, filesize($path_file));

# decode
#$arr_data = explode(',', $cipher_txt);
#$plain_txt = base64_decode($arr_data[1]);
$plain_txt = base64_decode($cipher_txt);

# display
$image_resource = imagecreatefromstring($plain_txt);

if( $image_resource !== FALSE )
{
        header("Content-type: image/jpeg");
        header("Content-length: " . (string)(filesize($path_file)));
        imagejpeg($image_resource);
        # also tried
        # echo $plain_txt;
        # echo '<img src="' . imagejpeg($image_resource) . '" />';
        # before decode
        # echo '<img src="data:image/jpeg;base64,' . cipher_txt . '" />';
        # even tried the ob_start() -> ob_get_clean() suggestions without luck
}

All suggestions are appreciated.

nuke
  • 11
  • 5
  • Does `$file_as_text` contain `data:image/jpeg;base64,` at the start? It should only be the image data itself if so ([example](https://stackoverflow.com/a/15291233/2244284)). – Gavin Sep 15 '19 at 16:06
  • Possible duplicate of [How to convert an image to base64 encoding?](https://stackoverflow.com/questions/3967515/how-to-convert-an-image-to-base64-encoding) – SergkeiM Sep 15 '19 at 16:17
  • @gavin No, it is a viewable image (jpg) file when first read in. – nuke Sep 15 '19 at 22:08
  • @froxz as you can see I tried the code from that post but for some reason it isn't working for me. It seems it breaks for me when trying to turn the string back into an image for display. – nuke Sep 15 '19 at 22:10

1 Answers1

0

Solution that worked for me was to remove the

$image_resource = imagecreatefromstring($plain_txt);

# and
header("Content-type: image/jpeg");
header("Content-length: " . (string)(filesize($path_file)));
imagejpeg($image_resource);

Then to send the base64_decoded string to an img tag as it's src value. That value had to include the data:image/jpeg;base64, portion in the string.

Thanks to all who looked.

nuke
  • 11
  • 5