CI3 Encrypt/Decrypt Images |
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.
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 )
(09-11-2019, 02:41 AM)InsiteFX Wrote: Read this article.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.
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 )
(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>
I think in your code you are missing this part.
PHP Code: $decoded_str = base64_decode($img_str); //pass the encoded string here 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 )
(09-12-2019, 08:08 AM)InsiteFX Wrote: I think in your code you are missing this part.Isn't that what the line $plain_txt = base64_decode($arr_data[1]); is doing?
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 )
(09-12-2019, 11:10 AM)InsiteFX Wrote: No, they are converting the string to an image I do not see you doing that.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.
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. |
Welcome Guest, Not a member yet? Register Sign In |