欢迎光临 - 我的站长站,本站所有资源仅供学习与参考,禁止用于商业用途或从事违法行为!

php教程

php图片压缩裁剪处理方法

php教程 我的站长站 2020-09-28 共58人阅读

当网站图片尺寸过大、图片过多的情况下,会引起页面加载缓慢的问题。人工手动修改图片肯定工程量太大,所以最好的方法就是用程序来自动压缩裁剪图片了,分享一段自用的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);
}
}
标签 解压压缩
相关推荐
  • 解压压缩
  • 单文件在线解压和压缩PHP源码
    单文件在线解压和压缩PHP源码

    源码介绍在线解压和压缩PHP源码,单文件源码,无需数据库,只需上传至指定目录即可立即使用。源码截图

    php源码 9 4个月前
  • 帝国CMS二次开发压缩与解压缩功能使用教程

    在使用帝国CMS建站,用到压缩与解压缩功能的很少,所以很少相关的文章。使用这些功能的基本是一些网页展示下载站一样。在帝国CMS系统文件中,e/class/phpzip.inc.php是专门处理文件压缩功能的,阅读代码你会发现,它没有解压缩功能。网站有个模板版块提供各...

    帝国cms教程 151 5年前
  • python暴力破解压缩包密码脚本

    import rarfileimport zipfileimport py7zrc = 0a = 0print("本软件只支持 7z , zip , rar格式的压缩包")zd = input("请输入字典位置:")try: ozd = open(zd,"r",encoding="utf8").read().split("\n")except: print("字典读取失败"...

    python教程 168 2年前
  • photoshopCS6 极简阉割70M破解版 解压即用
    photoshopCS6 极简阉割70M破解版 解压即用

    photoshopCS6 极简阉割100M破解版 解压即用,随身保存在移动硬盘的PS,压缩包仅仅70多M。删除一切没必要的功能,只保留左侧主功能,小巧方便携带。...

    开发软件 744 4年前
  • ARCHPR 4.50解压密码破解神器
    ARCHPR 4.50解压密码破解神器

    工具说明ARCHPR 4.50是国外一款解压密码破解神器,多项选择,暴力破解。解压密码破解截图安装教程1:首先,我们先把 EXE 文件按照提示安装好2:关键一步:填写注册码,在软件界面“帮助H”“输入注册码”注册...

    软件分享 199 2年前