Encoding format (tmp) is not supported.

187次阅读
没有评论

这是一个基于laravel-admin版本1.8.19开发的一个健身类小程序的管理后台,表单上传图片提交时报错,报错信息摘要:Encoding format (tmp) is not supported.

[2024-12-14 23:55:56] local.ERROR: Encoding format (tmp) is not supported. {"userId":1,"exception":"[object] (Intervention\\Image\\Exception\\NotSupportedException(code: 0): Encoding format (tmp) is not supported. at D:\\WWW\\jianshen.test\\vendor\\intervention\\image\\src\\Intervention\\Image\\AbstractEncoder.php:200)

从报错信息得知这是intervention/image抛出的错误,为什么会抛出这样的错误,从Github中找到下面几条相关的issue:

具体原因就是intervention/image在3.x版本以前,在Windows系统环境下,上传后的临时文件后缀是.tmp,导致intervention/image保存的时候无法识别图片文件的编码。

intervention/image在3.0版本修复了该问题,但是从3.0版本开始,依赖的PHP版本8.1及以上,如果将PHP版本升级至8.1又会导致laravel5.8无法运行,要使laravel能在PHP8.1及以上的版本运行,那么就需要升级laravel版本了,升级laravel版本也可能会导致laravel-admin1.8.19无法正常运行。

总之,升级intervention/image可能会导致一系列依赖问题,只能暂时先修改laravel-admin来完成目前的需求。

处理方式一:修改intervention/image代码(有点粗暴)

直接在vendor\intervention\image\src\Intervention\Image\AbstractEncoder.php文件的138行下面,也就是case 'image/pjpeg':下一行添加case 'tmp':即可:

case 'jpg':
case 'jpeg':
case 'jfif':
case 'image/jp2':
case 'image/jpg':
case 'image/jpeg':
case 'image/pjpeg':
case 'tmp': // 新添加的
case 'image/jfif':

处理方式二:修改laravel-admin的代码

vendor\encore\laravel-admin\src\Form\Field\Image.php 文件内的 prepare 方法内的:

$this->callInterventionMethods($image->getRealPath());

修改为:

$this->callInterventionMethods($image->getRealPath(), $image->getClientOriginalExtension());

也就是把文件的扩展名传递过去,接着将

vendor\encore\laravel-admin\src\Form\Field\ImageField.php 文件内的 callInterventionMethods 方法内的:

public function callInterventionMethods($target)
{
    if (!empty($this->interventionCalls)) {
        $image = ImageManagerStatic::make($target);

        foreach ($this->interventionCalls as $call) {
            call_user_func_array(
                [$image, $call['method']],
                $call['arguments']
            )->save($target);
        }
    }

    return $target;
}

修改为:

public function callInterventionMethods($target, $originalExt = null) // 这一行
{
    if (!empty($this->interventionCalls)) {
        $image = ImageManagerStatic::make($target);

        foreach ($this->interventionCalls as $call) {
            call_user_func_array(
                [$image, $call['method']],
                $call['arguments']
            )->save(is_null($originalExt) ? $target : str_replace(pathinfo($target, PATHINFO_EXTENSION), $originalExt, $target)); // 这一行
        }
    }

    return $target;
}

正文完
 0
wujingquan
版权声明:本站原创文章,由 wujingquan 于2024-12-14发表,共计2274字。
转载说明:Unless otherwise specified, all articles are published by cc-4.0 protocol. Please indicate the source of reprint.
评论(没有评论)