我的laravel项目中有简单的调整大小图像功能。它应该上传原始图像并制作它的缩略图。
表单提交后我得到了两张图片,但原始图片放在错误的目录中。这是我的控制器中的功能
if (Input::hasFile('image') && !Input::get('remove_image')) {
$file = Input::file('image');
$filename = str_random(20);
$image = new ImageResize($file);
$original = $filename . '.'. $file->getClientOriginalExtension();
$thumb = $filename . '-thumb.' . $file->getClientOriginalExtension();
$file->move(public_path() . '/uploads', $original);
$imagePath = '/uploads/' . $original;
$thumbPath = '/uploads/' . $thumb;
$image->crop(200, 200);
$image->save('uploads/' . $thumb);
}
基本上,当我上传image.jpg
时,我会收到两张图片image.jpg
和image-thumb.jpg
。两张图片都应保存在uploads
中,即public/uploads/
但只有缩略图保存在那里。
原始图片保存在**bootstrap**/uploads/
中。为什么要进入 bootstrap ... 目录?我在任何地方都没有提到它?
答案 0 :(得分:2)
您可以尝试将public_path()
方法替换为url('/')
。不确定这会有所帮助,但我没有使用public_path()
答案 1 :(得分:2)
Image::make($request->file('image'))->resize(462, 462)->save('upload_path/filename.jpg'));
尝试此代码..
答案 2 :(得分:2)
仅将目录保留在要保存原件的位置。尝试更改正在移动图像的这一行
$file->move(public_path() . '/uploads', $original);
用这个(删除公共路径)
$file->move('uploads', $original);
答案 3 :(得分:2)
使用图像干预调整大小并保存图像
Image::make($avatar)->resize(250, 250)->save(public_folder('/uploads/'.$filename));
调整大小功能会调整图像大小,保存功能会将图像保存到上传文件夹。为$ filename变量提供所需的文件名。