图片压缩最后压缩并转成jpg

这段代码主要涉及图片处理,包括图片大小调整和压缩。resizeImage()函数根据指定的最大宽度和高度来调整图片尺寸,getThumb()函数则用于生成指定大小的缩略图,通过先压缩图片再转换格式以减少网站流量消耗。关键步骤包括使用imagecreatefromxxx函数创建图像资源,imagejpeg()函数保存JPEG格式的图像,以及imagedestroy()释放内存。
部署运行你感兴趣的模型镜像
/**
 * 生成图片
 * @param string $im 源图片路径
 * @param string $dest 目标图片路径
 * @param int $maxwidth 生成图片宽
 * @param int $maxheight 生成图片高
 */
function resizeImage($im, $dest, $maxwidth, $maxheight) {
 $img = getimagesize($im);
 switch ($img[2]) {
 case 1:
  $im = @imagecreatefromgif($im);
  break;
 case 2:
  $im = @imagecreatefromjpeg($im);
  break;
 case 3:
  $im = @imagecreatefrompng($im);
  break;
 }
 
 $pic_width = imagesx($im);
 $pic_height = imagesy($im);
 $resizewidth_tag = false;
 $resizeheight_tag = false;
 if (($maxwidth && $pic_width > $maxwidth) || ($maxheight && $pic_height > $maxheight)) {
 if ($maxwidth && $pic_width > $maxwidth) {
  $widthratio = $maxwidth / $pic_width;
  $resizewidth_tag = true;
 }
 
 if ($maxheight && $pic_height > $maxheight) {
  $heightratio = $maxheight / $pic_height;
  $resizeheight_tag = true;
 }
 
 if ($resizewidth_tag && $resizeheight_tag) {
  if ($widthratio < $heightratio)
  $ratio = $widthratio;
  else
  $ratio = $heightratio;
 }
 
 
 if ($resizewidth_tag && !$resizeheight_tag)
  $ratio = $widthratio;
 if ($resizeheight_tag && !$resizewidth_tag)
  $ratio = $heightratio;
 $newwidth = $pic_width * $ratio;
 $newheight = $pic_height * $ratio;
 
 if (function_exists("imagecopyresampled")) {
  $newim = imagecreatetruecolor($newwidth, $newheight);
  imagecopyresampled($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $pic_width, $pic_height);
 } else {
  $newim = imagecreate($newwidth, $newheight);
  imagecopyresized($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $pic_width, $pic_height);
 }
 
 imagejpeg($newim, $dest);
 imagedestroy($newim);
 } else {
 imagejpeg($im, $dest);
 }
}
 
/**
 * 图片压缩处理
 * @param string $sFile 源图片路径
 * @param int $iWidth 自定义图片宽度
 * @param int $iHeight 自定义图片高度
 * @return string 压缩后的图片路径
 */
function getThumb($sFile,$iWidth,$iHeight){
 //图片公共路径
 $public_path = '';
 //判断该图片是否存在
 if(!file_exists($public_path.$sFile)) return $sFile;
 //判断图片格式(图片文件后缀)
 $extend = explode("." , $sFile);
 $attach_fileext = strtolower($extend[count($extend) - 1]);
 if (!in_array($attach_fileext, array('jpg','png','jpeg'))){
 return '';
 }
 //压缩图片文件名称
 $sFileNameS = str_replace(".".$attach_fileext, "_".$iWidth.'_'.$iHeight.'.'.$attach_fileext, $sFile);
 //判断是否已压缩图片,若是则返回压缩图片路径
 if(file_exists($public_path.$sFileNameS)){
 return $sFileNameS;
 }
 
 //生成压缩图片,并存储到原图同路径下
 resizeImage($public_path.$sFile, $public_path.$sFileNameS, $iWidth, $iHeight);
 if(!file_exists($public_path.$sFileNameS)){
 return $sFile;
 }
 return $sFileNameS;
}


先把图片压缩到指定小值,再把png大图片转化成jpg压缩,能节省网站流量一笔消耗
//png图片转化成jpg方法
$png = imagecreatefrompng($filePath);
第一步:将图片转化为文件代码
$png = imagecreatefrompng($filePath);
如果执行失败返回false;
第二步:将文件代码转化为jpg格式图片
imagejpeg($png, $newfilepath, 75);
这里大概说明一下,最后一个参数是转化后的质量的意思,值为0到100之间。
如果函数执行失败,也返回false;
第三步:释放文件代码
imagedestroy($png);
请大家注意,这一步一定不能掉,否则会出现内存泄露。



您可能感兴趣的与本文相关的镜像

Llama Factory

Llama Factory

模型微调
LLama-Factory

LLaMA Factory 是一个简单易用且高效的大型语言模型(Large Language Model)训练与微调平台。通过 LLaMA Factory,可以在无需编写任何代码的前提下,在本地完成上百种预训练模型的微调

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值