我如何在Laravel-6中更新多个图像?

时间:2019-11-08 08:27:09

标签: mysql database laravel-6 image-optimization laravel-6.2

我要更新laravel中的多个图像,当我要更新laravel中的新图像时,以前的图像应从文件夹中自动删除。 这是我的图片更新代码...

$thumbDirPath = date("Ymd").'/thumb/'.$chr;
$smallDirPath = date("Ymd").'/small/'.$chr;

if($r->hasFile('propertyImages')){
$propImage = [];
for ($v=0; $v < count($r->propertyImages); $v++) {
    $thums      = $r->propertyImages;
    $filnameSmall = 'small-'.uniqid($chr).'-'.$v.'.'.$thums[$v]->getClientOriginalExtension();
    $img = Image::make($thums[$v]->getRealPath());
    $img->resize(300, null, function ($constraint) {
        $constraint->aspectRatio();
    });
    $smallPath = public_path().'/'.$type.'/'.date("Ymd").'/small'.'/'.$chr.'/'.$filnameSmall;
    if (file_exists($smallPath)) {
     $this->removeImage($smallPath);
 }
 $img->save($smallPath);

 $optimizerChain = OptimizerChainFactory::create();
 $optimizerChain->optimize($smallPath);

 $filnameLarge = 'thumb-'.uniqid($chr).'-'.$v.'.'.$thums[$v]->getClientOriginalExtension();
 $img = Image::make($thums[$v]->getRealPath());
 $img->resize(1080, null, function ($constraint) {
    $constraint->aspectRatio();
});
 $thumbPath = public_path().'/'.$type.'/'.date("Ymd").'/thumb'.'/'.$chr.'/'.$filnameLarge;

 if (file_exists($thumbPath)) {
    $this->removeImage($thumbPath);
}
$img->save($thumbPath);
$optimizerChain->optimize($thumbPath);

$propImage[$v]['small'] = $filnameSmall;
$propImage[$v]['smallDirPath'] = $smallDirPath;
$propImage[$v]['large'] = $filnameLarge;
$propImage[$v]['thumbDirPath'] = $thumbDirPath;
}
}else{
$propImage = NULL;
}

0 个答案:

没有答案