CodeIgniter Forums
CI3 Encrypt/Decrypt Images - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forum-5.html)
+--- Forum: General Help (https://forum.codeigniter.com/forum-24.html)
+--- Thread: CI3 Encrypt/Decrypt Images (/thread-74327.html)

Pages: 1 2


CI3 Encrypt/Decrypt Images - nuke - 09-10-2019

I have successfully encrypted and decrypted other file types on the fly so, i believe my code is correct but I can post it if you want to see it.

How do you encrypt/decrypt images? Must you Base64_encode before encrypting and base64_decode after decrypting? When I do I get: (top portion of the file only, I actually get alot more)

����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�%�

So, it looks like it decode/decrypts but I keep getting the It has an Error message when I add the header for image/jpg before echoing it out to the browser.

Any and All assistance appreciated.


RE: CI3 Encrypt/Decrypt Images - InsiteFX - 09-11-2019

Read this article.

Encode Decode image using PHP


RE: CI3 Encrypt/Decrypt Images - nuke - 09-11-2019

(09-11-2019, 02:41 AM)InsiteFX Wrote: Read this article.

Encode Decode image using PHP
Thank you for your response.

I have disable the encrypt/decrypt portion of my code leaving just the base64_encode/decode portions and am still getting the above stated response. Here is the pieces I am using:

# 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);

# 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]);

# 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);
}

Again, Thanks for looking. Second brain is always useful.


RE: CI3 Encrypt/Decrypt Images - InsiteFX - 09-11-2019

Why are you putting it into an array? It should be just a string.


RE: CI3 Encrypt/Decrypt Images - nuke - 09-12-2019

(09-11-2019, 05:54 PM)InsiteFX Wrote: Why are you putting it into an array? It should be just a string.
Which line would you be referring to? The only array used is $arr_data to pull the base64 string portion:
// split the string on commas
// $arr_data[ 0 ] == "data:image/jpeg;base64"
// $arr_data[ 1 ] == <actual base64 string>


RE: CI3 Encrypt/Decrypt Images - InsiteFX - 09-12-2019

I think in your code you are missing this part.

PHP Code:
$decoded_str base64_decode($img_str); //pass the encoded string here
     
    $im 
imagecreatefromstring($decoded_str); 

Creating the image from a decoded string.


RE: CI3 Encrypt/Decrypt Images - nuke - 09-12-2019

(09-12-2019, 08:08 AM)InsiteFX Wrote: I think in your code you are missing this part.

PHP Code:
$decoded_str base64_decode($img_str); //pass the encoded string here
     
    $im 
imagecreatefromstring($decoded_str); 

Creating the image from a decoded string.
Isn't that what the line

$plain_txt = base64_decode($arr_data[1]);

is doing?


RE: CI3 Encrypt/Decrypt Images - InsiteFX - 09-12-2019

No, they are converting the string to an image I do not see you doing that.

See the imagecreatefromstring method they are using.


RE: CI3 Encrypt/Decrypt Images - nuke - 09-12-2019

(09-12-2019, 11:10 AM)InsiteFX Wrote: No, they are converting the string to an image I do not see you doing that.

See the imagecreatefromstring method they are using.
Yes, that is the next line:

# display
$image_resource = imagecreatefromstring($plain_txt);

Then a couple of lines down

imagejpeg($image_resource);

Please, what am I missing? Is it something about what imagecreatefromstring() or imagejpeg() actually do?

Thanks for helping me understand and your patients.


RE: CI3 Encrypt/Decrypt Images - nuke - 09-16-2019

Solved:
Removed the explode line and returned the base64 encoded str with the data:image/jpg;base64, portion still attached.
Placed that in a variable to populate a view with the str data as an image src value
Displayed the page by output'ing my template.

Big picture, stopped trying to display the image directly from the controller as I had done with pdf's previously.