保持高宽比
This commit is contained in:
parent
8e0665926a
commit
6d738ce34b
|
|
@ -42,14 +42,20 @@ class ImageService
|
||||||
public function resize(int $width = 100, int $height = 100): string
|
public function resize(int $width = 100, int $height = 100): string
|
||||||
{
|
{
|
||||||
$extension = pathinfo($this->imagePath, PATHINFO_EXTENSION);
|
$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);
|
$newImagePath = public_path($newImage);
|
||||||
if (!is_file($newImagePath) || (filemtime($this->imagePath) > filemtime($newImagePath))) {
|
if (!is_file($newImagePath) || (filemtime($this->imagePath) > filemtime($newImagePath))) {
|
||||||
$this->createDirectories($newImage);
|
$this->createDirectories($newImage);
|
||||||
$img = Image::make($this->imagePath);
|
$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);
|
return asset($newImage);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue