package org.dromara.mp.util;

import cn.hutool.core.codec.Base64;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;

/**
 * @author sunzexing
 * @version 1.0
 * @title ImgUtil
 * @description
 * @create 2024-08-19 11:28
 */
public class ImgUtil {



    public static byte[] scaleImg(InputStream in,float scale){
        if(in == null){
            return null;
        }
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        try{
            cn.hutool.core.img.ImgUtil.scale(in,out,scale);
        }catch(Exception e){
            e.printStackTrace();
            return null;
        }finally {
            try{
                in.close();
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        return out.toByteArray();
    }


    public static String scaleImgToBase64(InputStream in,float scale){
        byte[] bytes = scaleImg(in,scale);
        if(bytes == null){
            return null;
        }
        return Base64.encode(bytes);
    }

}
