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.