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;
use Intervention\Image\Facades\Image;
use Intervention\Image\Exception\NotReadableException;
class ImageService
{
@ -43,23 +44,27 @@ 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, '.')) . '-' . $width . 'x' . $height . '.' . $extension;
try {
$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);
if (!is_file($newImagePath) || (filemtime($this->imagePath) > filemtime($newImagePath))) {
create_directories(dirname($newImage));
$img = Image::make($this->imagePath);
$newImagePath = public_path($newImage);
if (!is_file($newImagePath) || (filemtime($this->imagePath) > filemtime($newImagePath))) {
create_directories(dirname($newImage));
$img = Image::make($this->imagePath);
$img->resize($width, $height, function ($constraint) {
$constraint->aspectRatio();
});
$img->resize($width, $height, function ($constraint) {
$constraint->aspectRatio();
});
$canvas = Image::canvas($width, $height);
$canvas->insert($img, 'center');
$canvas->save($newImagePath);
$canvas = Image::canvas($width, $height);
$canvas->insert($img, 'center');
$canvas->save($newImagePath);
}
return asset($newImage);
} catch (NotReadableException $e) {
return $this->originUrl();
}
return asset($newImage);
}