我需要在上传文章图片时,自动上传原始图片大小并在thumb
文件夹中调整图片大小。
但是在这个方法中我收到这个错误(thumb
文件夹不能自动创建):
Can't write image data to path (storage/upload/images/articles/1400/05/06/thumb/Bx1WRTtp9IgSZgnP8VE11627500167.jpg)
上传图片方法:
protected function articleUploadImage($file, $folder, $size)
{
$d = jdate();
$year = $d->format('Y');
$month = $d->format('m');
$day = $d->format('d');
$direct = 'storage/upload/images/'. $folder . '/' . $year . '/' . $month . '/' . $day;
$directWithResize = 'storage/upload/images/'. $folder . '/' . $year . '/' . $month . '/' . $day . '/thumb';
$extension = $file->getClientOriginalExtension();
$fileName = Str::random(20) . time() . '.' . $extension;
// if (!is_dir($directWithResize)) {
// mkdir($directWithResize);
// }
//$path = Image::make($file->getRealPath());
//$path->resize(100, 100);
//$path->save(public_path($directWithResize . '/' . $fileName));
Image::make($file->getRealPath())->fit($size, null, function ($constraint) {
$constraint->aspectRatio();
})->save($direct . '/thumb/' . $fileName);
//$path = $file->store($direct);
//$newPath = $direct;
return $direct;
}
在上传图像之前,您应该创建一个目录。
protected function articleUploadImage($file, $folder, $size)
{
$d = jdate();
$year = $d->format('Y');
$month = $d->format('m');
$day = $d->format('d');
$direct = 'storage/upload/images/'. $folder . '/' . $year . '/' . $month . '/' . $day;
$extension = $file->getClientOriginalExtension();
$fileName = Str::random(20) . time() . '.' . $extension;
if (!file_exists($direct . '/thumb/')) {
mkdir($direct . '/thumb/', 666, true);
}
Image::make($file->getRealPath())->fit($size, null, function ($constraint) {
$constraint->aspectRatio();
})->save($direct . '/thumb/' . $fileName);
return $direct;
}