MySQL-use PHP to cut the local images to be uploaded to the database

Source: Internet
Author: User
Tags imagejpeg
Gets the upload temporary file path $uploadedfile = $_files[' UploadFile ' [' Tmp_name '];//gets the source file resource handle. The receive parameter is the picture path, the return handle $source = Imagecreatefromjpeg ($uploadedfile);//Gets the file's long width list ($width, $height) = getimagesize ($ UploadedFile), if ($width > $height) {$xoord = Ceil (($width-$height)/2); $width = $height;} else {$yoord = Ceil (($height- $width)/2); $height = $width;} Create a picture. The receive parameters are wide-height, which returns the generated resource handle $thumb = Imagecreatetruecolor (100,100);//Large map//The source file is cut all fields and scaled down to the target picture. The first two are resource handles imagecopyresampled ($thumb, $source, 0,0, $xoord, $yoord, 100,100, $width, $height);//Picture shows imagejpeg ($thumb, ", 100);

The above is the picture cutting process, assuming I can directly through the $db->select|update|insert to operate the database, how do I want to improve the above code, to upload the image after the cut into 2 into the MySQL database?

Reply content:

Gets the upload temporary file path $uploadedfile = $_files[' UploadFile ' [' Tmp_name '];//gets the source file resource handle. The receive parameter is the picture path, the return handle $source = Imagecreatefromjpeg ($uploadedfile);//Gets the file's long width list ($width, $height) = getimagesize ($ UploadedFile), if ($width > $height) {$xoord = Ceil (($width-$height)/2); $width = $height;} else {$yoord = Ceil (($height- $width)/2); $height = $width;} Create a picture. The receive parameters are wide-height, which returns the generated resource handle $thumb = Imagecreatetruecolor (100,100);//Large map//The source file is cut all fields and scaled down to the target picture. The first two are resource handles imagecopyresampled ($thumb, $source, 0,0, $xoord, $yoord, 100,100, $width, $height);//Picture shows imagejpeg ($thumb, ", 100);

The above is the picture cutting process, assuming I can directly through the $db->select|update|insert to operate the database, how do I want to improve the above code, to upload the image after the cut into 2 into the MySQL database?

Although it has been suggested that you do not exist in the database, but if you insist, I still tell you the way to do it, first you want to save the image of the field must be a blob format, assuming you are ready for the database, in the code you need to make the following changes

Starting with the last line, use a temporary file to do the relay $tmp = '/tmp/'. MD5 (UNIQID ()); Imagejpeg ($thumb, $tmp, 100);//Convert data to a string $str = Mysql_real_escape_string (file_get_contents ($tmp)); /This is just a test of SQL, you can modify $sql = sprintf ("INSERT into Test (' Blob_column ') VALUES ('%s ')", $str); mysql_query ($sql);// Don't forget to delete the temporary document unlink ($TMP);
  • Related Article

    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.