• PHP实现图片批量压缩


    set_time_limit(0);
    global $source_dir;
    global $target_dir;
    $source_dir = "D:/images/";//目标路径
    $target_dir = 'D:/finish/';//最终路径
    
    /*开始扫描文件夹*/
    scan_dir($source_dir);
    //exit('finish');
    
    
    function log_msg($msg){
        echo "$msg
    ";
    }
    
    /**
     * 转移文件
     * @param $path_file
     * @return array
     */
    function move_file($path_file){
        $path_file = rtrim($path_file,'/');
        if (!is_file($path_file)) return array('msg'=>'图片不存在','status'=>false);
    
        global $source_dir;
        global $target_dir;
    
        $file_name = substr($path_file,strrpos($path_file,'/')+1);
        $dir = substr($path_file,0,strrpos($path_file,'/')+1);
        $imginfo= getimagesize($path_file);
        $ext = strtolower(substr(end($imginfo),strrpos(end($imginfo),'/')+1));
        $new_dir = str_replace($source_dir,$target_dir,$dir);
        if (!is_dir($new_dir)){
            $rs = mkdir($new_dir,0777,true);
            if (!$rs) return array('msg'=>'创建文件夹失败','status'=>false);
        }
        /*新文件名*/
        $new_file_name = substr($file_name,0,strrpos($file_name,'.')).".jpg";
        $path_new_file = "{$new_dir}{$new_file_name}";
        switch ($ext){
            case "png":
                $file = imagecreatefrompng($path_file);
                imagejpeg($file,$path_new_file);
                break;
            case "gif":
                $file = imagecreatefromgif($path_file);
                imagejpeg($file,$path_new_file);
                break;
            case "jpg":
            case "jpeg":
            default:
                $file = imagecreatefromjpeg($path_file);
                imagejpeg($file,$path_new_file);
                break;
        }
        return array('msg'=>'success','status'=>true);
    }
    
    
    /**
     * 扫描文件夹
     * @param $path_dir
     */
    function scan_dir($path_dir){
        if (!is_dir($path_dir)) return false;
        $rs = scandir($path_dir);
        foreach ($rs as $k => $v){
            if ($v === '.' || $v === '..') continue;
            $path = $path_dir.rtrim($v,'/');
            if (is_file($path)){
                $rs = move_file($path);
                if ($rs['status']){
                    $rs = "finish";
                }else{
                    $rs = $rs['msg'];
                }
                log_msg("{$path}---{$rs}");
                continue;
            }
            if (is_dir($path."/")) scan_dir($path."/");
        }
    }
  • 相关阅读:
    maven公共库
    java截取当前屏幕图片
    JAVE视频处理
    jar在maven仓库里面没有时 , 把jar导入本地仓库步骤
    3 .shell 之linux四剑客sed/grep/awk/find
    Spring学习(四)-基于注解的Bean管理
    Spring学习(三)-Bean的种类,作用域,生命周期
    Spring学习(一)-基本入门
    dubbo服务连接zookeeper报错:java.net.ConnectException: Connection refused
    idea-常用设置二
  • 原文地址:https://www.cnblogs.com/pcx105/p/8602456.html
Copyright © 2020-2023  润新知