IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> PHP知识库 -> PHP压缩图片 -> 正文阅读

[PHP知识库]PHP压缩图片

/**
 * 图片压缩处理
 * @param string $oldImg 源图片路径
 * @param int $width 自定义图片宽度
 * @param int $height 自定义图片高度
 * @return string 压缩后的图片路径
 */
function getThumb($oldImg,$width = 0,$height = 0){
    //判断该图片是否存在
    if(!file_exists($oldImg)){
        return false;
    }
    //判断图片格式(图片文件后缀)
    $pathArr = pathinfo($oldImg);
    $extend = $pathArr['extension'];
    if(!in_array($extend,['jpg','png','jpeg'])){
        return false;
    }
    //未设置宽高 则以原尺寸压缩
    $oldImgInfo = getimagesize($oldImg);
    if(!$width){
        $width = $oldImgInfo[0];
    }
    if(!$height){
        $height = $oldImgInfo[1];
    }
    //压缩图片文件名称
    $newImg = str_replace('.'.$extend, '_thumb_'.$width.'_'.$height.'.'.$extend, $oldImg);
    //判断是否已压缩图片,若是则返回压缩图片路径
    if(file_exists($newImg)){
        return $newImg;
    }
    //生成压缩图片,并存储到原图同路径下
    resizeImage($oldImg, $newImg, $width, $height);
    if(!file_exists($newImg)){
        return $oldImg;
    }
    return $newImg;
}

/**
 * 生成图片
 * @param string $oldImgPath 源图片路径
 * @param string $newImgPath 目标图片路径
 * @param int $width 生成图片宽
 * @param int $height 生成图片高
 */
function resizeImage($oldImgPath, $newImgPath, $width, $height) {
    $img = getimagesize($oldImgPath);
    switch ($img[2]) {
        case 1:
            $resource = @imagecreatefromgif($oldImgPath);
            break;
        case 2:
            $resource = @imagecreatefromjpeg($oldImgPath);
            break;
        case 3:
            $resource = @imagecreatefrompng($oldImgPath);
            break;
        default:
            $resource = @imagecreatefrompng($oldImgPath);
            break;
    }
    //原图的宽
    $pic_width = imagesx($resource);
    //原图的高
    $pic_height = imagesy($resource);
    //判断是否压缩
    $resize_width_tag = $resize_height_tag = false;
    //默认压缩率1,即等尺寸压缩
    $width_ratio = $height_ratio = 1;
    if(($width && $pic_width > $width) || ($height && $pic_height > $height)){
        //宽度是否压缩
        if($width && $pic_width > $width){
            $width_ratio = $width / $pic_width;
            $resize_width_tag = true;
        }
        //高度是否压缩
        if($height && $pic_height > $height){
            $height_ratio = $height / $pic_height;
            $resize_height_tag = true;
        }
        //选择压缩率
        if($resize_width_tag && $resize_height_tag){
            if ($width_ratio < $height_ratio){
                $ratio = $width_ratio;
            } else{
                $ratio = $height_ratio;
            }
        }elseif ($resize_width_tag && !$resize_height_tag){
            $ratio = $width_ratio;
        }elseif ($resize_height_tag && !$resize_width_tag){
            $ratio = $height_ratio;
        }else{
            $ratio = 1;
        }
        $new_width = $pic_width * $ratio;
        $new_height = $pic_height * $ratio;
        if(function_exists("imagecopyresampled")){
            $newim = imagecreatetruecolor($new_width, $new_height);
            imagecopyresampled($newim, $resource, 0, 0, 0, 0, $new_width, $new_height, $pic_width, $pic_height);
        }else{
            $newim = imagecreate($new_width, $new_height);
            imagecopyresized($newim, $resource, 0, 0, 0, 0, $new_width, $new_height, $pic_width, $pic_height);
        }
        imagejpeg($newim, $newImgPath);
        imagedestroy($newim);
    } else {
        imagejpeg($resource, $newImgPath);
    }
}

  PHP知识库 最新文章
Laravel 下实现 Google 2fa 验证
UUCTF WP
DASCTF10月 web
XAMPP任意命令执行提升权限漏洞(CVE-2020-
[GYCTF2020]Easyphp
iwebsec靶场 代码执行关卡通关笔记
多个线程同步执行,多个线程依次执行,多个
php 没事记录下常用方法 (TP5.1)
php之jwt
2021-09-18
上一篇文章      下一篇文章      查看所有文章
加:2021-08-11 11:55:25  更:2021-08-11 11:55:52 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/13 20:39:30-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码