Javascript 如何使用Filepond制作缩略图,以及正常大小的上载

Javascript 如何使用Filepond制作缩略图,以及正常大小的上载,javascript,php,filepond,Javascript,Php,Filepond,我已经设法让Filepond上传文件,将它们放在正确的文件夹中,并向mysql数据库添加必要的详细信息。然而,由于原始图像的分辨率非常高,我需要制作缩略图。较小版本的图像应该放在名为“缩略图”的子文件夹中 我使用的是PHP锅炉板,有些文档没有介绍其中使用的方法。例如,为了测试,我尝试这样做: FilePond.setOptions({ maxFileSize: '25MB', instantUpload: true, server: 'ap

我已经设法让Filepond上传文件,将它们放在正确的文件夹中,并向mysql数据库添加必要的详细信息。然而,由于原始图像的分辨率非常高,我需要制作缩略图。较小版本的图像应该放在名为“缩略图”的子文件夹中

我使用的是PHP锅炉板,有些文档没有介绍其中使用的方法。例如,为了测试,我尝试这样做:

    FilePond.setOptions({
        maxFileSize: '25MB',
        instantUpload: true,
        server: 'api/',
        imageTransformVariants: {
            'thumb_medium_': transforms => {
                transforms.resize.size.width = 384;
                return transforms;
            },
            'thumb_small_': transforms => {
                transforms.resize.size.width = 128;
                return transforms;
            }
        }

    });
    let pond = FilePond.create(document.querySelector('input[type="file"]'));
这给了我一个错误:

index.php?id=1383:67 Uncaught (in promise) TypeError: Cannot read property 'size' of undefined
    at thumb_medium_ (index.php?id=1383:67)
    at filepond-plugin-image-transform.js:898
    at filepond-plugin-image-transform.js:987
    at new Promise (<anonymous>)
    at filepond-plugin-image-transform.js:986
    at filepond-plugin-image-transform.js:1066
    at Array.map (<anonymous>)
    at filepond-plugin-image-transform.js:1065
    at new Promise (<anonymous>)
    at filepond-plugin-image-transform.js:941
index.php?id=1383:67未捕获(承诺中)类型错误:无法读取未定义的属性“size”
在thumb\u medium\ux(index.php?id=1383:67)
在filepond plugin image transform.js:898
在filepond plugin image transform.js:987
在新的承诺()
在filepond plugin image transform.js:986
在filepond plugin image transform.js:1066
在Array.map()处
在filepond plugin image transform.js:1065
在新的承诺()
在filepond plugin image transform.js:941
显然,我可以为缩略图进行第二次Filepond上传,但我怀疑应该有一种方法可以自动完成。如何使变体工作,以及如何使它们位于不同的文件夹和相同的文件名中,而不是给它们一个前缀?

您使用的是什么?我认为Doka允许您在客户端设置转换,然后服务器将执行它们——尽管我不确定。另一种方法是编写一个简单的php脚本来为您进行压缩,因为您只需要上传一个图像

如果您想自己压缩服务器上的图像,可以使用下面的方法


函数压缩图像($source,$destination,$newWidth=384){
//获取图像和详细信息
$info=getimagesize($source);
$width=$info[0];
$height=$info[1];
$ratio=$width/$height;
//计算新高度
$newHeight=$newWidth/$ratio;
//创建临时空白图像
$compressedImg=imagecreatetruecolor($newWidth,$newHeight);
如果($info['mime']=='image/jpeg'){
$image=imagecreatefromjpeg($source);
imagecopyresampled($compressedImg,$image,0,0,0,$newWidth,$newHeight,$width,$height);
//如果需要,调整质量
图像JPEG($compressedImg,$destination,75);
返回true;
}elseif($info['mime']=='image/png'){
$image=imagecreatefrompng($source);
imagecopyresampled($compressedImg,$image,0,0,0,$newWidth,$newHeight,$width,$height);
//根据需要调整压缩(0未压缩,9最高压缩)
imagepng($compressedImg,$destination,9);
返回true;
}
返回false;
}
压缩图像('/path/to/image/filename.jpg','/path/to/image/thumbnails/filename.jpg');
免责声明-我尚未测试此代码

您正在使用吗?我认为Doka允许您在客户端设置转换,然后服务器将执行它们——尽管我不确定。另一种方法是编写一个简单的php脚本来为您进行压缩,因为您只需要上传一个图像

如果您想自己压缩服务器上的图像,可以使用下面的方法


函数压缩图像($source,$destination,$newWidth=384){
//获取图像和详细信息
$info=getimagesize($source);
$width=$info[0];
$height=$info[1];
$ratio=$width/$height;
//计算新高度
$newHeight=$newWidth/$ratio;
//创建临时空白图像
$compressedImg=imagecreatetruecolor($newWidth,$newHeight);
如果($info['mime']=='image/jpeg'){
$image=imagecreatefromjpeg($source);
imagecopyresampled($compressedImg,$image,0,0,0,$newWidth,$newHeight,$width,$height);
//如果需要,调整质量
图像JPEG($compressedImg,$destination,75);
返回true;
}elseif($info['mime']=='image/png'){
$image=imagecreatefrompng($source);
imagecopyresampled($compressedImg,$image,0,0,0,$newWidth,$newHeight,$width,$height);
//根据需要调整压缩(0未压缩,9最高压缩)
imagepng($compressedImg,$destination,9);
返回true;
}
返回false;
}
压缩图像('/path/to/image/filename.jpg','/path/to/image/thumbnails/filename.jpg');

免责声明-我尚未测试此代码

您是否已注册调整大小插件?是否已注册调整大小插件?