3 896 2021-02-10 19:59:56
收藏当网站图片尺寸过大、图片过多的情况下,会引起页面加载缓慢的问题。人工手动修改图片肯定工程量太大,所以最好的方法就是用程序来自动压缩裁剪图片了,分享一段自用的php图片压缩裁剪处理方法。
/** * 图片压缩处理 * @param string $sFile 图片路径 * @param int $iWidth 自定义图片宽度 * @param int $iHeight 自定义图片高度 */ function getThumb($sFile,$iWidth,$iHeight){ //判断该图片是否存在 if(!file_exists(public_path().$sFile)) return $sFile; //判断图片格式 $attach_fileext = get_filetype($sFile); if (!in_array($attach_fileext, array('jpg','png','jpeg'))){ return $sFile; } //压缩图片 $sFileNameS = str_replace(".".$attach_fileext, "_".$iWidth.'_'.$iHeight.'.'.$attach_fileext, $sFile); //判断是否已压缩图片,若是则返回压缩图片路径 if(file_exists(public_path().$sFileNameS)){ return $sFileNameS; } //解决手机端上传图片被旋转问题 if (in_array($attach_fileext, array('jpeg')) ){ adjustPicOrientation(public_path().$sFile); } //生成压缩图片,并存储到原图同路径下 resizeImage(public_path().$sFile, public_path().$sFileNameS, $iWidth, $iHeight); if(!file_exists(public_path().$sFileNameS)){ return $sFile; } return $sFileNameS; } /** *获取文件后缀名 */ function get_filetype($filename) { $extend = explode("." , $filename); return strtolower($extend[count($extend) - 1]); } /** * 解决手机上传图片被旋转问题 * @param string $full_filename 文件路径 */ function adjustPicOrientation($full_filename){ $exif = exif_read_data($full_filename); if($exif && isset($exif['Orientation'])) { $orientation = $exif['Orientation']; if($orientation != 1){ $img = imagecreatefromjpeg($full_filename); $mirror = false; $deg = 0; switch ($orientation) { case 2: $mirror = true; break; case 3: $deg = 180; break; case 4: $deg = 180; $mirror = true; break; case 5: $deg = 270; $mirror = true; break; case 6: $deg = 270; break; case 7: $deg = 90; $mirror = true; break; case 8: $deg = 90; break; } if ($deg) $img = imagerotate($img, $deg, 0); if ($mirror) $img = _mirrorImage($img); //$full_filename = str_replace('.jpg', "-O$orientation.jpg", $full_filename);新文件名 imagejpeg($img, $full_filename, 95); } } return $full_filename; } resizeImage(public_path().$sFile, public_path().$sFileNameS, $iWidth, $iHeight); /** * 生成图片 * @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); } }
本文地址:https://xzo.com.cn/develop/php/537.html
如果您认可我们的分享,有意与我们合作开展帝国cms网站建设与开发业务或插件定制,请联系右侧在线客服。我们能给您的,就是高质量的模板与售后。
标注了信息来源为下载鸥的文章皆为原创,如果是转载的优质文章,我们也都标注了出处。如果您喜欢我们的文章,请按照下载鸥所标注的文章出处进行标注,谢谢您的配合。
PHP是一种服务器端、跨平台、html嵌入式的脚本语言,其功能十分强大、入门简单。在过去,民间一直有说法称“PHP是最好的编程语言”,是PHP开发者对自身所使用语言的肯定。
PHP曾是最主流的网站web开发语言之一,现在也依然十分流行。
猜你喜欢如果用rand()来获取随机ID是非常消耗性能的。小网站几百几千条数据...
我们知道,str_replace是可以执行替换命令的,但一般是用于一对一替换,这个很好...
任何站点但凡有点流量就会遇到这种情况:首页被人用问号传了参数,恰恰还被收录...
互联网上每天都有很多爬虫到处爬取内容,你可能不知道他们是干嘛的,但其中肯定...
数组在实际开发过程中运用非常频繁,通过合理运用数组,我们可以顺利实现各种数...
多维数组在实际开发中比较常见,要foreach循环也很简单。...
在一些开发场景中,我们需要在每次发起请求后更新重置session_id以同步用户数...
通俗的说,现在有结果A B C D E,每个结果代表不同的意义,此时我们可以用if else...
PHP中,终止脚本运行有三种方式:主脚本程序中使用return、脚本中使用exit()、d...
在php的开发中,我们常常需要使用到header函数头来进行做标记...
都知道PHP无非就是增删改查,学会了增删改查就入门了PHP,精准像素分享一篇PHP...
join() 函数说明将数组合并为字符串,示例如下。...
PHP hex2bin() 函数把十六进制值转换为 ASCII 字符:<?php echo hex2bin("4...
xpath()函数运行对 XML 文档的 XPath 查询。如果成功,该函数返回 SimpleXMLE...
token功能简述:PHP 使用token验证可有效的防止非法来源数据提交访问,增加数据...
本文收录了php常用函数,便于调用。...