Jump to content

[SOLVED] Converting Image To Base64


TGWSE_GY

Recommended Posts

Ok guys,

 

I know that most of you would disagree with how I am wanting to store images in a mysql database via converting the files to Base64, however that is what I am trying to accomplish.

 

I am attempting to convert the images like this

 

	// Grab Images from image_upload.php form
$Image1 = $_FILES['formImage1'];
$Image2 = $_FILES['formImage2'];
$Image3 = $_FILES['formImage3'];
$Image4 = $_FILES['formImage4'];
$Image5 = $_FILES['formImage5'];

echo "<pre>";
echo "FILES:";
print_r($_FILES);
echo "</pre>";

//Encode Images to BASE64 to store in the database table
//NOTE ***** If you are going to calle the image from the database remember to use base64_decode explicit instructions can
//           be found at http://www.php.net/manual/en/function.base64-decode.php
if ($Image1['size'] > 0) {
$EncodedImage1 = base64_encode($Image1);
}
if ($Image2['size'] > 0) {
$EncodedImage2 = base64_encode($Image2);
}
if ($Image3['size'] > 0) {
$EncodedImage3 = base64_encode($Image3);
}
if ($Image4['size'] > 0) {
$EncodedImage4 = base64_encode($Image4);
}
if ($Image5['size'] > 0) {
$EncodedImage5 = base64_encode($Image5);
}

 

Apparently all the $EncodedImage variables are receiving is arrays, my question is simple what am I doing wrong. What am I suppose to pull out of the $Image['Array'] to encode. Also a lot of the examples I have been finding use fopen and I am trying to avoid fopen because my host has it turned off and requires me to use curl. So a non fopen solution would be great.

 

Thanks Guys 8)

Link to comment
Share on other sites

PFMaBismAd - Thanks for the help but I have already read that and I know about the file arrays, my question is what do I have to send to be converted to Base64, must I send each individual array items: type name and size?

 

I am not trying to sound stupid, but if I am missing something key that is in the php manual about file uploads then please let me know.

 

Thanks  ;D

Link to comment
Share on other sites

I'm not really certain on this one. 

 

When the file is uploaded it gets placed in a tempory place on the server.

 

That place is stated in $_FILES['formImage1']['tmp_name'] (I think, you can probably see the location when you do that print_r() statement.

 

You have to open/read that file to get the contents as a string which you can then manipulate with base64 and insert into your database.

 

If you can't use fopen and fread, perhaps you can do a file_get_contents() to read the file and put it into a string for you?

Link to comment
Share on other sites

sloth456 thanks for the help, this is what I used code wise

 

	$Image1 = $_FILES['formImage1'];
$Image2 = $_FILES['formImage2'];
$Image3 = $_FILES['formImage3'];
$Image4 = $_FILES['formImage4'];
$Image5 = $_FILES['formImage5'];

echo "<pre>";
echo "FILES:";
print_r($_FILES);
echo "</pre>";

$imgtest = file_get_contents($Image1['tmp_name']);
echo $imgtest;

 

and this is what echo $imgtest returned and since I have never done this I am unsure if the response is correct but this is what I got,

�PNG  ��� IHDR������������\X����tEXtSoftware�Adobe ImageReadyq�e<��IDATx��yp՝��{��4:-�:lˇl,ダ�R�!$녰�ݭS��M- Ea�GR��neS��T�`�]�,E�1>0�|`|H���4ci���L{4�$klK���+Uw��׿��}��{�����E ���t���.A�'R?A�� ��O�~� ��� H�A�'R?A�� ��O�~� ��� H�A�'R?A�� ��O�~� ��� H�A�'R?A�� ��O�~� ��� H��� H�A�'R?A|��.�!�h�?Cj��c�j�-*��Ѩ&S�_C��|g]��*��+Q��D^p�Wr�I����}�ޓ�!��k�~/-+�1�QQ�!9R g��3�[wR~#����TW���oG�h�(�1�2�Q�T_�oG/u�������בּzƻ#��� V�$bJ��!X�_��q� v��{VS%SR�4^WaqO��Y�i��fS=SL���m��sS�2�'b����RN����^EUEL�OwTA��*튊 v�*bʨ�ʊE,m������2�ov���&G5U1e���)n�өSE�.�^�E;�15�O��#J�..q���x�W��Rs"���E����X?�15��~`oQ{�>1u��L�ז�QUSF��œ���O����"���w���x9o������vr��S/�Bl��Ț� d� #��X�Y�jX�2��g��o�clG3a����೟e옎�[�'$� ����L����nf4�Z`g�S(p�22W�����8��#�k��% �^���<�ԁ���R��%V�^��>��X��F-"'���j�׶1ф��\R>M���-��j���"�gMk�%��j'B�����r-�e?�XHl`���G��;}l#3���U[P����N �h�j�B�2�rd`&aw����"�-g��o��7�p��xx@��F�dQ~����������]�v�*�22�"�Z=8\�n��V��YeM^�o\;-�b��g������ 9'�������G����2�u�cۙ|>J K�^؎��.��=�%�*Df,�-ff���}��������|6g灼N<�>6l܍�a�ʾ�~�Hή���oîȎ��W % J�83Ԍ��)C��/^�< *����� Z���qv��af~������|�2���/���yX�(<ZH�(�8�3�6V79�,2::�2�Ӡl��d �K�~\���4( �yV(���ci}��I�~�h�{�u�����l}�3]�&º�V2}�]h-_���%zU��Ѹ��l oxY��%7L��ܼ��[��N��k�/�ʧQh��E2���{Y��sBfH큩 �4��j8"*��,r��,f��,8�.~mظ��X���G��3�V2���qE�Y�g�o�At�jK�J���A{�mAN�8�4���ҝ�K�o�}��'ހ�YP��;���m��O��ο�j��qfz!�ୗ�}�o���~%:���T%ڽ�����o��V��ݵ�h\;=�þ�`�pu��%���o�=��8��s;�D�8BUx,\MV�pZ��oZņ��� � ���uƆbf~���\5Cs�"?�6(�U3��͜��ư�؅��j�-�y��̳��p,�Ts/sK�<���l�)������3�"����ώi����eC�|��K֙���%�<3��D�k��kV�Zr��_>�O���u�m~�_��jW���?��Y+�u��W"��O���׸<�vmWl����%Q�)N�B���n����1�����]ӷ�N�?�kkc�\]�r�%W��X�Z���Cb�8m1��GMd$�^fi����6����G��\X��&����$�"��n��Os���ۮ��h��w̙�RW�����گ���n֙�+"1[�^S8-�t/H��:G�����Ԃ|���_��6q�����U� ,f=o�]FO�q���_�B0�`�D�S#�E���GF��l���P~,w��ݥ#�����ytv"l�&{�<[�ָ\�I��_,\��33�լ��x]�U˯~�8c���o�,�h��X�i�*Gߡp�el�g���9�ޭ�y ��s� �T�F�H�7����53���C¸�Ɍq��4Pp;a�P^պ���t֖�*!�8�� L4�y���K��(�� ���n/�SS:&3������->�6���X3m�U�@E��� yؼJ�z���c���ZE�V���[�k����x��8����pc����>M�F�Y��GJ����B~4��H��;�׶�YB1��O�3c�h�� t��nH�+B�'���-�:�L63S,K6���+�4�lSaz3����R���Weow88�S2�G|���c��G>�����%�p%���?��3l�vT?���hu_ 3�f.u��Y`ƺ@��s�;�4���"^(xrކC�eZ�+i>�1�}�I��9%�?�wOΟ��k}�G>'��8�m����`� �N1�a-:|fK4��Ge��Y��%?�@���R���]�S,0c=�i,���b Z�O��|]�=�|Q�D���W��>T"�r��zͲ���~2;��"�F�+k#-4��N)Ђ��4�Іڰ/X>K�o�y�U�˖N��fܛ���'�&�'�%ѝ}�E�ͺ}niK����3c�����|a��coW^eU�T�Ĝ�lMX�c�8��3��_ҐN!�����C��m��C�����Z]�.h� ;�ϸG������z�qm<具 wm9��іsҳ�k�}�@�P|�Aq݃��^#r����/w�~�ܵ���c�I��9���_��뼠Ek6?��"zɷ9w]v� ��[:_�y�toz܉k���@Ψ��E�����a��|?}��Ǐ>��+����|ZN���?.k*�ҟ���J���H�sj�Q�(�ۢ5+N�p���EOX����BW������gW�N��oH��ڝ$�LZ�����om��u=�qGc��?|q �x�m�d�eUU��uM�S�\�H�_1��M|��V^��q�Ԭ��u���ܯ� ������|�`��F-5=�����$^r��sy��Ewd���Q�?z?G��GP�Ά|Sa�Γ��i]�s�?}&�oi[��OF�XBIȪ� @M5��E�W���"�V)q���1@S�y=�vs�}���c L�t-����ڀȅU˶� ��-�3Ò�4��৸Q���͹���0����pj�ؗ�����T���5��z��B�nik�j4}���IY1�w���I���9�w�]���w������D������1=��)��-گq�o}�?��ѾJ��FOz\��f�a�<�$}�\6��>�(���H�\��ǡ��<������D�1��u������޹G?<���r��!N>�+��2�'z�ؗ�/�8uѿ���{��^�k���p�NPyQ%aн�����O�_�M��[R�vx����i�X\�NyQ�Bi�b��\"�\w�S�!�S�2�ɽ\��K��r���9-��Q�=�����5/�-v�� �@�!�zNr~n�˺��^l���&H�G�3nH�{���?��Vߎݙ�!�(��/�\��KҜť���n�\��#�t� ��J��~�m@�ו�� 8�e�28����醁����p;������!{���/{��5fO� �7�mXЍ�6�1-|�v��'���Lj~��N�����������9��sz��I(S �ʬ�pL؍+����;j�Ӭư��Q��$�'���df�4c�?��օ��V��9��ҋ��;�Lb�F�,ߛ�L��-E�j]�o�G-��%�\�M-���s�}���w=���������^IZ,�(I�(�_uf��>��H��� ���z��5C�z�uiC,��8b�Ƹ�>�܆��̼f�~�$���S�hB��I^~��I=Q��ݛI� �{߼1�]"®zeѥϥ^~��a��-�P�f�������� )�;�?vw�M?�uVW|c���O��+o:���CZ�.�&�Z�����ܭO�WfsJ��+���a9�Ѭ9�G�$�q[K(s3��^�"q[ݜ��0�[���5���Bc!��E\�mb�ӽ����I�������X��ޢ|y▰oŝ�o:��׬J$�- �\Z>gq�l�`U4%�%UN�IYK�oBMĕ���8�gL�G�XD���HP���H�׬�$W��rKN��N��v�^��D�U�Z�+: �����+:�XE:��c�{��K�'ι��qK�Y��p����*��^5��hH�=PT��>��������Z�:��ͮ�?:��*��0�'�) �_��� H�A�'R?A�� ��O�~� ��� H�A�'R?A�� ��O�~� ��� H�A�'R?A�� ��O�~� ��� H�A�'R?A�� ��O�~� ��~� ��� H�A�'R?A�� ��O�~� ��� H�A�'R?A�� ��O�~� ��� ��/��4�E�n�����IEND�B`�

 

Is that what I am looking to be returned and if so what do I do to put it all back together to display?

 

Thanks

Link to comment
Share on other sites

I believe that string is correct.

 

You can encode it and stick it into your database.

 

When you pull it out you will want to first decode it and then to make it so that the browser actually spits out an image rather than the raw data you need to change the header content type.

 

So for a png image you would do this:

 

header('Content-Type: image/png');
//go grab string from database and decode
echo $theresultingimage;

Link to comment
Share on other sites

Thank you so much sloth456 for your help I will let you know what the results are here in just a little bit, I am working on coding it now. It was like pulling teeth getting help yesterday, I think it was because a lot of people have problems with this method.

 

Thanks Again 8)

Link to comment
Share on other sites

You need to upload the image to the server first, grab the image content from the file, then converting it to base64 and deleting it from the server and then insert the datab into the db when necessary.

 

http://fundisom.com/phparadise/php/image_handling/base64_image_encode

 

@sloth456 - the $_FILES array does not contain the actual image, just the information about the image.

Link to comment
Share on other sites

You need to upload the image to the server first, grab the image content from the file, then converting it to base64 and deleting it from the server and then insert the datab into the db when necessary.

 

@sloth456 - the $_FILES array does not contain the actual image, just the information about the image.

 

Yeah, I know - which is why I told him to use file_get_contents to grab the image content from the file, whose location is stated in the $_FILE array.  I'm totally aware that the $_FILE array doesn't actually contain the image.

Link to comment
Share on other sites

Now I have debuged this code and it works however when the script gets to the end I am getting an Parse error: syntax error, unexpected $end in /home/.deceasing/thegayestever/thegayestcommunityever.com/dev/php/includes/content/members_home/user_images/upload.php on line 61 error. Any ideas?

 

 
<?php
session_start();
if (isset($_SESSION['Usr'])) {
// Grab Images from image_upload.php form
$Image1 = $_FILES['formImage1'];
$Image2 = $_FILES['formImage2'];
$Image3 = $_FILES['formImage3'];
$Image4 = $_FILES['formImage4'];
$Image5 = $_FILES['formImage5'];

//Encode Images to BASE64 to store in the database table
//NOTE ***** If you are going to calle the image from the database remember to use base64_decode explicit instructions can
//           be found at http://www.php.net/manual/en/function.base64-decode.php
// Get database connection info
include('config.php');

// Connect to Database
$con = mysql_connect($Host, $Login, $Pass);

mysql_select_db($con);

// Get the username from the session
$User = $_SESSION['Usr'];

//Get the users CustID
$GetCustID = mysql_query("SELECT CustID FROM basic WHERE Usr = '$User'");
$CustID = $GetCustID;

if ($Image1['size'] > 0) {
	$Image1Type = $Image1['type'];
	$GetContentsImage1 = file_get_contents($Image1['tmp_name']); 
	$EncodedImage1 = base64_encode($GetContentsImage1);
	mysql_query("INSERT INTO profile_user_images ( CustID,ImageType,Image,DefaultProfileImage ) VALUES ($CustID, $Image1Type, $EncodedImage1, ''");
}
if ($Image2['size'] > 0) {
	$Image2Type = $Image2['type'];
	$GetContentsImage = file_get_contents($Image2['tmp_name']);
	$EncodedImage2 = base64_encode($GetContentsImage2);
	mysql_query("INSERT INTO profile_user_images ( CustID,ImageType,Image,DefaultProfileImage ) VALUES ($CustID, $Image2Type, $EncodedImage2, ''");
}
if ($Image3['size'] > 0) {
	$Image3Type = $Image3['type'];
	$GetContentsImage = file_get_contents($Image3['tmp_name']);
	$EncodedImage3 = base64_encode($GetContentsImage3);
	mysql_query("INSERT INTO profile_user_images ( CustID,ImageType,Image,DefaultProfileImage ) VALUES ($CustID, $Image3Type, $EncodedImage3, ''");
}
if ($Image4['size'] > 0) {
	$Image4Type = $Image4['type'];
	$GetContentsImage = file_get_contents($Image4['tmp_name']);
	$EncodedImage4 = base64_encode($GetContentsImage4);
	mysql_query("INSERT INTO profile_user_images ( CustID,ImageType,Image,DefaultProfileImage ) VALUES ($CustID, $Image4Type, $EncodedImage4, ''");
}
if ($Image5['size'] > 0) {
	$Image5Type = $Image5['type'];
	$GetContentsImage = file_get_contents($Image5['tmp_name']);
	$EncodedImage5 = base64_encode($GetContentsImage5);
	mysql_query("INSERT INTO profile_user_images ( CustID,ImageType,Image,DefaultProfileImage ) VALUES ($CustID, $Image5Type, $EncodedImage5, ''");
}

?>

Link to comment
Share on other sites

Same code as above but will repost it, apparently the script is getting past my if statements however it is not posting the information to the database.

 

Here is my code

 
<?php
session_start();

if (isset($_SESSION['Usr'])) {
// Grab Images from image_upload.php form
$Image1 = $_FILES['formImage1'];
$Image2 = $_FILES['formImage2'];
$Image3 = $_FILES['formImage3'];
$Image4 = $_FILES['formImage4'];
$Image5 = $_FILES['formImage5'];

//Encode Images to BASE64 to store in the database table
//NOTE ***** If you are going to calle the image from the database remember to use base64_decode explicit instructions can
//           be found at http://www.php.net/manual/en/function.base64-decode.php
// Get database connection info
include('config.php');

// Connect to Database
$con = mysql_connect($Host, $Login, $Pass);

mysql_select_db($con);

// Get the username from the session
$User = $_SESSION['Usr'];

//Get the users CustID
$GetCustID = mysql_query("SELECT CustID FROM basic WHERE Usr = '$User'");
$CustID = $GetCustID;

if ($Image1['size']>0) {
	$Image1Type = $Image1['type'];
	$GetContentsImage1 = file_get_contents($Image1['tmp_name']); 
	$EncodedImage1 = base64_encode($GetContentsImage1);
	mysql_query("INSERT INTO profile_user_images ( CustID,ImageType,Image ) VALUES ($CustID, $Image1Type, $EncodedImage1");
}
if ($Image2['size'] > 0) {
	$Image2Type = $Image2['type'];
	$GetContentsImage = file_get_contents($Image2['tmp_name']);
	$EncodedImage2 = base64_encode($GetContentsImage2);
	mysql_query("INSERT INTO profile_user_images ( CustID,ImageType,Image,DefaultProfileImage ) VALUES ($CustID, $Image2Type, $EncodedImage2, ''");
}
if ($Image3['size'] > 0) {
	$Image3Type = $Image3['type'];
	$GetContentsImage = file_get_contents($Image3['tmp_name']);
	$EncodedImage3 = base64_encode($GetContentsImage3);
	mysql_query("INSERT INTO profile_user_images ( CustID,ImageType,Image,DefaultProfileImage ) VALUES ($CustID, $Image3Type, $EncodedImage3, ''");
}
if ($Image4['size'] > 0) {
	$Image4Type = $Image4['type'];
	$GetContentsImage = file_get_contents($Image4['tmp_name']);
	$EncodedImage4 = base64_encode($GetContentsImage4);
	mysql_query("INSERT INTO profile_user_images ( CustID,ImageType,Image,DefaultProfileImage ) VALUES ($CustID, $Image4Type, $EncodedImage4, ''");
}
if ($Image5['size'] > 0) {
	$Image5Type = $Image5['type'];
	$GetContentsImage = file_get_contents($Image5['tmp_name']);
	$EncodedImage5 = base64_encode($GetContentsImage5);
	mysql_query("INSERT INTO profile_user_images ( CustID,ImageType,Image,DefaultProfileImage ) VALUES ($CustID, $Image5Type, $EncodedImage5, ''");
}
}
?>

 

Here is my DB Table setup

Field                Type     Collation         Null    Key     Default  Extra           Privileges            Comment
-------------------  -------  ----------------  ------  ------  -------  --------------  --------------------  -------
CustID               int(9)   (NULL)            NO              (NULL)                   select,insert,update         
ImageID              int(50)  (NULL)            NO      PRI     (NULL)   auto_increment  select,insert,update         
ImageType            text     ascii_general_ci  YES             (NULL)                   select,insert,update         
Image                blob     (NULL)            YES             (NULL)                   select,insert,update         
DefaultProfileImage  int(1)   (NULL)            YES             (NULL)                   select,insert,update         

 

I am simply omitting the columns that I do not need to insert into, however I think that is whats causing my problem but I am not sure.

 

I know this probably belongs in the MYSQL boards but because it is relevant to this project and others are already familiar with what I am doing here.

 

Thanks 8)

Link to comment
Share on other sites

Okay only the first IF STATEMENT works. I don't see why it would be skipping the rest but it is. I have checked and the file arrays are holding the information.

 

here is my code:

 
<?php
session_start();

if (isset($_SESSION['Usr'])) {
// Grab Images from image_upload.php form
$Image1 = $_FILES['formImage1'];
$Image2 = $_FILES['formImage2'];
$Image3 = $_FILES['formImage3'];
$Image4 = $_FILES['formImage4'];
$Image5 = $_FILES['formImage5'];

//Encode Images to BASE64 to store in the database table
//NOTE ***** If you are going to calle the image from the database remember to use base64_decode explicit instructions can
//           be found at http://www.php.net/manual/en/function.base64-decode.php
// Get database connection info
include('config.php');

// Connect to Database
$con = mysql_connect($Host, $Login, $Pass);

mysql_select_db("login_ums",$con);

// Get the username from the session
$User = $_SESSION['Usr'];

//Get the users CustID
$GetCustID = mysql_query("SELECT CustID FROM basic WHERE Usr = '$User'");
$CustID = $GetCustID;

if ($Image1['size']>0) {
	$Image1Type = $Image1['type'];
	$GetContentsImage1 = file_get_contents($Image1['tmp_name']); 
	$EncodedImage1 = base64_encode($GetContentsImage1);
	mysql_query("INSERT INTO profile_user_images ( CustID,ImageType,Image ) VALUES ('$CustID', '$Image1Type', '$EncodedImage1')") or die(mysql_error());
}
if ($Image2['size'] > 0) {
	$Image2Type = $Image2['type'];
	$GetContentsImage = file_get_contents($Image2['tmp_name']);
	$EncodedImage2 = base64_encode($GetContentsImage2);
	mysql_query("INSERT INTO profile_user_images ( CustID,ImageType,Image ) VALUES ('$CustID', '$Image2Type', '$EncodedImage2')") or die(mysql_error());
}
if ($Image3['size'] > 0) {
	$Image3Type = $Image3['type'];
	$GetContentsImage = file_get_contents($Image3['tmp_name']);
	$EncodedImage3 = base64_encode($GetContentsImage3);
	mysql_query("INSERT INTO profile_user_images ( CustID,ImageType,Image ) VALUES ('$CustID', '$Image3Type', '$EncodedImage3')") or die(mysql_error());
}
if ($Image4['size'] > 0) {
	$Image4Type = $Image4['type'];
	$GetContentsImage = file_get_contents($Image4['tmp_name']);
	$EncodedImage4 = base64_encode($GetContentsImage4);
	mysql_query("INSERT INTO profile_user_images ( CustID,ImageType,Image ) VALUES ('$CustID', '$Image4Type', '$EncodedImage4')") or die(mysql_error());
}
if ($Image5['size'] > 0) {
	$Image5Type = $Image5['type'];
	$GetContentsImage = file_get_contents($Image5['tmp_name']);
	$EncodedImage5 = base64_encode($GetContentsImage5);
	mysql_query("INSERT INTO profile_user_images ( CustID,ImageType,Image ) VALUES ('$CustID', '$Image5Type', '$EncodedImage5')") or die(mysql_error());
}
}

 

Thanks

 

Link to comment
Share on other sites

  • 2 weeks later...

Been a while since I came on here. You still got that problem?

 

I'm not sure why it would be skipping, although I do see a bit of code that looks a little odd:

 

//Get the users CustID
   $GetCustID = mysql_query("SELECT CustID FROM basic WHERE Usr = '$User'");
   $CustID = $GetCustID;

 

Later on you try to insert $CustID into your database, is this working?

 

Normally I would have done this

 

//Get the users CustID
   $GetCustID = mysql_query("SELECT CustID FROM basic WHERE Usr = '$User'");

//start sloths code

$row = mysql_fetch_array($GetCustID);

//end sloths code
   
//$CustID = $GetCustID;

$CustID = $row['CustID'];

 

Maybe that solves the problem you're having?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.