Mysql-use php to cut the local image to be uploaded and store it in the database

Source: Internet
Author: User
Tags imagejpeg
{Code ...} the above is the image cutting process. suppose I can operate the database directly through $ db-& amp; gt; select | update | insert. how can I improve the above code, after cutting the image to be uploaded, convert it to a binary format and save it to the mysql database.
// Obtain the path of the temporary file to be uploaded $ uploadedfile =$ _ FILES ['uploadfile'] ['tmp _ name']; // Obtain the source file resource handle. The receiving parameter is the image path, and the returned handle $ source = ImageCreateFromJPEG ($ uploadedfile); // Obtain the length and width list ($ width, $ height) = GetImageSize ($ uploadedfile) of the file ); if ($ width> $ height) {$ xoord = ceil ($ width-$ height)/2); $ width = $ height ;} else {$ yoord = ceil ($ height-$ width)/2); $ height = $ width;} // create an image. The receiving parameters are respectively width and height, and the generated resource handle $ thumb = ImageCreatetruecolor (100,100) is returned; // Large image // cut all source file fields and narrow them down to the target image. The first two are resource handles ImageCopyResampled ($ thumb, $ source, 100,100, $ xoord, $ yoord, $ width, $ height); // ImageJPEG ($ thumb, '', 100 );

The above is the process of image cutting. if I can operate the database directly through $ db-> select | update | insert, how can I improve the above code, after cutting the image to be uploaded, convert it to a binary format and save it to the mysql database.

Reply content:
// Obtain the path of the temporary file to be uploaded $ uploadedfile =$ _ FILES ['uploadfile'] ['tmp _ name']; // Obtain the source file resource handle. The receiving parameter is the image path, and the returned handle $ source = ImageCreateFromJPEG ($ uploadedfile); // Obtain the length and width list ($ width, $ height) = GetImageSize ($ uploadedfile) of the file ); if ($ width> $ height) {$ xoord = ceil ($ width-$ height)/2); $ width = $ height ;} else {$ yoord = ceil ($ height-$ width)/2); $ height = $ width;} // create an image. The receiving parameters are respectively width and height, and the generated resource handle $ thumb = ImageCreatetruecolor (100,100) is returned; // Large image // cut all source file fields and narrow them down to the target image. The first two are resource handles ImageCopyResampled ($ thumb, $ source, 100,100, $ xoord, $ yoord, $ width, $ height); // ImageJPEG ($ thumb, '', 100 );

The above is the process of image cutting. if I can operate the database directly through $ db-> select | update | insert, how can I improve the above code, after cutting the image to be uploaded, convert it to a binary format and save it to the mysql database.

Although it is recommended that you do not store the image in the database, if you stick to it, I will tell you how to implement it. First, you must save the image fieldblobFormat. if you are ready for the database, make the following changes in the code:

// Start from the last line and use a temporary file for transfer $ tmp = '/tmp /'. md5 (uniqid (); ImageJPEG ($ thumb, $ tmp, 100); // convert the data into a string $ str = mysql_real_escape_string (file_get_contents ($ tmp )); // This is just a test SQL statement. you can modify $ SQL = sprintf ("INSERT INTO test ('blob _ column ') VALUES (' % s ')", $ str); mysql_query ($ SQL); // do not forget to delete the temporary file unlink ($ tmp );

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.