This article mainly introduces the implementation of binary conversion for php images, and introduces in detail the principle and implementation skills of binary conversion between images, which is of great practical value, for more information about how to implement binary conversion of php images, see the following example. Share it with you for your reference. The specific implementation method is as follows:
Here, we convert the uploaded file to binary when uploading the file and save it to the data field. we can display it in the same way next time we read it.
The html code is as follows:
The code is as follows:
Save the image to the database. the code is as follows:
The code is as follows:
<? Php
// Because the uploaded image is saved in a temporary file
// You only need to read the file to obtain the uploaded image.
$ Fp = fopen ($ _ FILES ["myFile"] ["tmp_name"], "rb ");
$ Buf = addslashes (fread ($ fp, $ _ FILES ["myFile"] ["size"]);
// Create a PDO object
$ Dbh = new PDO ("mysql: host = localhost; port =
3306; dbname = test "," root "," 123456 ″);
// Execute the insert operation and save the result in a variable
$ Result = $ dbh-> query ("insert into img (images) VALUES ('$ buf ')");
// Obtain the number of affected rows
If ($ result-> rowCount ()> 0 ){
Echo ("data inserted. ");
} Else {
Echo ("The INSERT operation cannot be performed. ");
}
// Explicitly close the PDO connection
$ Dbh = NULL;
?>
Show. phpThe code is as follows:
The code is as follows:
<? Php
$ Conn = @ mysql_connect ("localhost", "root", "123456") or die ("server connection error! "); // Link to the database
@ Mysql_select_db ("test", $ conn) or die ("no database found! ");
$ Query = "select * from img where Id =". $ _ GET ['id'];
$ Result = mysql_query ($ query );
$ Num = mysql_num_rows ($ result );
$ Data = mysql_result ($ result, 0, "images ");
Header ("Content-type: image/". $ num ['imgtype']);
Echo $ data;
?>
Alternatively, the code is as follows:
The code is as follows:
Supplement:
1. fopen function.
The fopen () function is used to open a file or URL. The syntax is as follows:
Int fopen (string filename, string mode );
The mode parameter of the string can be the following:
The "r" file is read-only, and the file pointer points to the beginning.
The "r +" file opening method can be read/write, and the file pointer points to the beginning.
The "w" file opening mode is writing, the file pointer refers to the starting point, and the length of the original file is set to 0. If the file does not exist, create a new file.
The "w +" file opening method can be read/write, the file pointer points to the start, and the length of the original file is set to 0. If the file does not exist, create a new file.
"A" writes files, and the file pointer points to the end of the file. If the file does not exist, create a new file.
The "a +" file opening method can be read/write, and the file pointer points to the end of the file. If the file does not exist, create a new file.
"B" can be used if the operating system has different text and binary files. this parameter is not required for UNIX systems.
2. Addslashes function
The Addslashes function is used to add a string to a diagonal line. The syntax is as follows. note: I removed this function during the test and it is successful. you can understand it by yourself.
String addslashes (string str );
This function adds a slash to the quotation mark (') of the string to be processed by the database for smooth operation of the database query. the modified characters include single quotation marks (') double quotation marks ("), backslash (), and NULL (the null byte ).
3. fread functions
The fread function is used to read the specified length of a bit group or to the end of the file EOF. The syntax is as follows:
String fread (int fp, int length); can be safely used for binary files
Fread () reads a maximum of length bytes from the file pointer. this function reads a maximum of length bytes, or reaches the EOF, or (for network streams) when a package is available, or (after opening the user space stream) when the user has read 8192 bytes, it will stop reading the file, depending on the first situation.
Convert binary to image
Note: $ newFilePath is used to process the generated image name and path. the code is as follows:
The code is as follows:
Using newfilepath+'1.jpg ';
$ Data = $ GLOBALS [HTTP_RAW_POST_DATA]; // Obtain the binary raw data from post.
If (emptyempty ($ data )){
$ Data = file_get_contents ("php: // input ");
}
$ NewFile = fopen ($ newFilePath, "w"); // open the file to prepare for writing
Fwrite ($ newFile, $ data); // write binary streams to files
Fclose ($ newFile); // close the file
You can store the read binary stream to the database, or directly write it into an image to obtain the binary header file to know what type of file it belongs to. the code is as follows:
The code is as follows:
$ Bin = substr ($ content, 0, 2 );
$ StrInfo = @ unpack ("C2chars", $ bin );
$ TypeCode = intval ($ strInfo ['chars1']. $ strInfo ['chars2']);
$ FileType = '';
Switch ($ typeCode)
{
Case 7790:
$ FileType = 'exe ';
Break;
Case 7784:
$ FileType = 'midi ';
Break;
Case 8297:
$ FileType = 'rar ';
Break;
Case 255216:
$ FileType = 'jpg ';
Break;
Case 7173:
$ FileType = 'GIF ';
Break;
Case 6677:
$ FileType = 'bmp ';
Break;
Case 13780:
$ FileType = 'PNG ';
Break;
Default:
Echo 'unknown ';
}
I hope this article will help you with PHP programming.