fixed for -- Unsupported image type image/vnd.microsoft.icon. GD driver is only able to decode JPG, PNG, GIF, BMP or WebP files.

This commit is contained in:
Edward Yang 2022-07-13 14:31:15 +08:00
parent 8e16e3a484
commit 3aa42e8055
1 changed files with 18 additions and 13 deletions

View File

@ -12,6 +12,7 @@
namespace Beike\Services; namespace Beike\Services;
use Intervention\Image\Facades\Image; use Intervention\Image\Facades\Image;
use Intervention\Image\Exception\NotReadableException;
class ImageService class ImageService
{ {
@ -43,23 +44,27 @@ 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); try {
$newImage = 'cache/' . mb_substr($this->image, 0, mb_strrpos($this->image, '.')) . '-' . $width . 'x' . $height . '.' . $extension; $extension = pathinfo($this->imagePath, PATHINFO_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))) {
create_directories(dirname($newImage)); create_directories(dirname($newImage));
$img = Image::make($this->imagePath); $img = Image::make($this->imagePath);
$img->resize($width, $height, function ($constraint) { $img->resize($width, $height, function ($constraint) {
$constraint->aspectRatio(); $constraint->aspectRatio();
}); });
$canvas = Image::canvas($width, $height); $canvas = Image::canvas($width, $height);
$canvas->insert($img, 'center'); $canvas->insert($img, 'center');
$canvas->save($newImagePath); $canvas->save($newImagePath);
}
return asset($newImage);
} catch (NotReadableException $e) {
return $this->originUrl();
} }
return asset($newImage);
} }