保持高宽比

This commit is contained in:
Edward Yang 2022-07-12 18:35:25 +08:00
parent 8e0665926a
commit 6d738ce34b
1 changed files with 9 additions and 3 deletions

View File

@ -42,14 +42,20 @@ class ImageService
public function resize(int $width = 100, int $height = 100): string
{
$extension = pathinfo($this->imagePath, PATHINFO_EXTENSION);
$newImage = 'cache/' . mb_substr($this->image, 0, mb_strrpos($this->image, '.')) . '-' . (int)$width . 'x' . (int)$height . '.' . $extension;
$newImage = 'cache/' . mb_substr($this->image, 0, mb_strrpos($this->image, '.')) . '-' . $width . 'x' . $height . '.' . $extension;
$newImagePath = public_path($newImage);
if (!is_file($newImagePath) || (filemtime($this->imagePath) > filemtime($newImagePath))) {
$this->createDirectories($newImage);
$img = Image::make($this->imagePath);
$img->resize($width, $height);
$img->save($newImagePath);
$img->resize($width, $height, function ($constraint) {
$constraint->aspectRatio();
});
$canvas = Image::canvas($width, $height);
$canvas->insert($img, 'center');
$canvas->save($newImagePath);
}
return asset($newImage);
}