• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
CI3 Encrypt/Decrypt Images

#1
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.
Reply

#2
Read this article.

Encode Decode image using PHP
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply

#3
(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.
Reply

#4
Why are you putting it into an array? It should be just a string.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply

#5
(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>
Reply

#6
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.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply

#7
(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?
Reply

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

See the imagecreatefromstring method they are using.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply

#9
(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.
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.