Save a picture to a database to generate a thumbnail image

Source: Internet
Author: User

Pictures uploaded to the database

The common.php under the app

Apply Public files 
* $name The name value for the form upload
* $filePath the folder name below uploads/for saving in the Portal folder public, no words will be created automatically
* $width Specify the thumbnail width
* $height Specify the thumbnail height
* Automatically generated thumbnails saved in the Thumb folder under the $filepath folder, automatically created
* @return Array One is the picture path, one is the thumbnail path, as follows:
* Array (2) {
["img"] = + string ("Uploads/img/20171211\3d4ca4098a8fb0f90e5f53fd7cd71535.jpg")
["thumb_img"] = + string ("Uploads/img/thumb/20171211/3d4ca4098a8fb0f90e5f53fd7cd71535.jpg")
}
*/
functionUploadFile ($name,$filePath,$width,$height)
{
$file= request () -file ($name);
if ($file) {
$filePaths= Root_path. ' Public '. DS. ' uploads '. DS.$filePath;
if (!file_exists ($filePaths)) {
mkdir ($filePaths,0777,true);
}
$info= $file -Move ($filePaths);
if ($info) {
$imgpath= ' uploads/'.$filePath.'/'.$info -getsavename ();
$image= \think\Image::Open ($imgpath);
$date _path= ' uploads/'.$filePath.'/thumb/'.Date (' YMD ');
if (!file_exists ($date _path)) {
mkdir ($date _path,0777,true);
}
$thumb _path= $date _path.'/'.$info -GetFileName ();
$image -Thumb ($width,$height) -Save ($thumb _path);
$data [' img ']= $imgpath;
$data [' thumb_img ']= $thumb _path;
return$data;
}else{
//upload failed to get error message
return$file -GetError ();
}
}
}


inside the controller.
Add to
Public function Add () {
return $this->fetch ("add");
}
Public Function Doadd () {
$path = uploadfile (' file ', ' images ', 80,80);
$data = input (' post. ');
$data = Array_merge ($data, $path);
$result = Db (' menu ')->insert ($data);
if ($result) {
$this->success ("Add Success", '/index/reply/index ');
}else{
$this->error ("Add Failure", '/index/reply/index ');
}

}

Save a picture to a database to generate a thumbnail image

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.