
/**
 * 
 */
package org.tio.sitexxx.service.utils;

import cn.hutool.core.img.ImgUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import net.coobird.thumbnailator.Thumbnails;
import org.tio.http.common.UploadFile;
import org.tio.sitexxx.service.model.main.Img;
import org.tio.sitexxx.service.vo.Const;

import java.awt.image.BufferedImage;
import java.io.File;

/**
 * @author tanyaowu
 *
 */
public class ImgUtils {
	/**
	 * 用目标宽度计算缩放比
	 * @param maxWidth
	 * @param initBufferedImage
	 * @return
	 */
	public static float calcScaleWithWidth(int maxWidth, BufferedImage initBufferedImage) {
		int initWidth = initBufferedImage.getWidth();
		int newWidth = Math.min(maxWidth, initWidth);
		return (float) newWidth / (float) initWidth;
	}

	/**
	 * 
	 * @param subDir
	 * @param uid
	 * @param uploadFile
	 * @param bi
	 * @param scale
	 * @return
	 * @throws Exception
	 * @author tanyaowu
	 */
	public static Img processImg(String subDir, Integer uid, UploadFile uploadFile, BufferedImage bi, float scale) throws Exception {
		String ext = FileUtil.extName(uploadFile.getName());
		if (StrUtil.isBlank(ext)) {
			ext = "gif";
		}
		boolean needScale = !"gif".equals(ext); //gif的不压缩
		String imgUrlWithoutExt = UploadUtils.newFile(subDir, uid, uploadFile.getName()); //不带后缀的文件路径
		String imgUrl = imgUrlWithoutExt + "." + ext;
		String smImgUrl;
		byte[] imgBytes = uploadFile.getData();

		if (needScale) {
			smImgUrl = imgUrlWithoutExt + "_sm." + ext;
		} else {
			smImgUrl = imgUrl;
		}

		String imgFilePath = Const.RES_ROOT + imgUrl; //文件路径
		String smImgFilePath = Const.RES_ROOT + smImgUrl;

		File imgFile = new File(imgFilePath);
		FileUtil.mkParentDirs(imgFilePath);
		File smImgFile = null;
		if (needScale) {
			smImgFile = new File(smImgFilePath);
			FileUtil.mkParentDirs(smImgFile);
		} else {
			smImgFile = imgFile;
		}

		Img img = new Img();
		if (needScale) {
			double quality = 0.5f;
			Thumbnails.of(bi).scale(1f) //指定图片的大小，值在0到1之间，1f就是原图大小，0.5就是原图的一半大小，这里的大小是指图片的长宽
			        .outputQuality(quality) //图片的质量，值也是在0到1，越接近于1质量越好，越接近于0质量越差
			        .toFile(imgFilePath);
			if (scale < 1f) {
				Thumbnails.of(bi).scale(scale).outputQuality(quality).toFile(smImgFilePath);
			} else {
				FileUtil.copy(imgFilePath, smImgFilePath, true);
			}
		} else {
			FileUtil.writeBytes(imgBytes, imgFile);
		}

		BufferedImage smBi = ImgUtil.read(smImgFile);
		img.setCoverheight(smBi.getHeight());
		img.setCoversize((int) FileUtil.size(smImgFile));
		img.setCoverwidth(smBi.getWidth());

		img.setCoverurl(smImgUrl);
		img.setUrl(imgUrl);
		img.setUid(uid);

		String filename = uploadFile.getName();//FileUtil.getName(imgFile);
		img.setFilename(filename);
		img.setHeight(bi.getHeight());
		img.setWidth(bi.getWidth());
		img.setSize(FileUtil.size(imgFile));

		img.setTitle(filename);

		return img;
	}

	public static Img processImg(String subDir, Integer uid, UploadFile uploadFile, float scale) throws Exception {
		byte[] imgBytes = uploadFile.getData();
		BufferedImage bi = ImgUtil.toImage(imgBytes);
		return processImg(subDir, uid, uploadFile, bi, scale);
	}

	/**
	 * @deprecated
	 * @param subDir
	 * @param uploadFile
	 * @return
	 * @author xufei
	 * 2020年1月14日 下午4:21:45
	 */
	public static Img processImg(String subDir, UploadFile uploadFile) {
		String ext = FileUtil.extName(uploadFile.getName());
		if (StrUtil.isBlank(ext)) {
			ext = "gif";
		}
		String imgUrlWithoutExt = UploadUtils.dateFile(subDir); //不带后缀的文件路径
		String imgUrl = imgUrlWithoutExt + "." + ext;
		byte[] imgBytes = uploadFile.getData();
		String imgFilePath = Const.RES_ROOT + imgUrl; //文件路径
		File imgFile = new File(imgFilePath);
		FileUtil.mkParentDirs(imgFilePath);
		BufferedImage bi = ImgUtil.toImage(imgBytes);
		Img img = new Img();
		FileUtil.writeBytes(imgBytes, imgFile);
		img.setCoverheight(bi.getHeight());
		img.setCoversize((int) FileUtil.size(imgFile));
		img.setCoverwidth(bi.getWidth());
		img.setCoverurl(imgUrl);
		img.setUrl(imgUrl);
		String filename = uploadFile.getName();//FileUtil.getName(imgFile);
		img.setFilename(filename);
		img.setHeight(bi.getHeight());
		img.setWidth(bi.getWidth());
		img.setSize(FileUtil.size(imgFile));
		img.setTitle(filename);
		return img;
	}

}
