If you're willing to use something else instead of javascript, I could suggest you to use Image Intervention package.
(如果您愿意使用其他语言代替javascript,我建议您使用Image Intervention软件包。)
Here's a simple example: (这是一个简单的例子:)
public function store(Request $request) {
$image = $request->image; //get your image
$resizedImage = Image::make($image); //create an instance of Image Intervention
$residedImage->resize('x', 'y') //Resize (or use any other function) to wanted size.
$resizedImage->save();
//Save image to database, or do something else with it.
YourModel::create($resizedImage);
}
Note: Don't forget to import the class at the top of your controller.
(注意:不要忘记将类导入控制器的顶部。)
use InterventionImageFacadesImage;
UPDATE:
(更新:)
If you want a user to select the size of your images, here's the simple solution:
(如果您希望用户选择图像的大小,这是简单的解决方案:)
Add two input fields in your HTML, where user will type in the size he wants.
(在HTML中添加两个输入字段,用户将在其中输入所需大小。)
<input type="text" name="width" class="input" required>
<input type="text" name="height" class="input" required>
Get the sizes in your controller method:
(在您的控制器方法中获取大小:)
public function store(Request $request) {
$image = $request->image; //get your image
$width = $request->input('width'); //get width
$height = = $request->input('height'); //get height
$resizedImage = Image::make($image); //create an instance of Image Intervention
$residedImage->resize($width, $height) //resize
$resizedImage->save();
//Save image to database, or do something else with it.
YourModel::create($resizedImage);
}
Note: Don't forget to validate user's input, so they cant' submit false data.
(注意:不要忘记验证用户的输入,因此他们不能提交错误的数据。)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…