首页 > 代码库 > php 图片压缩处理

php 图片压缩处理

技术分享
<?phprequire dirname(__FILE__)./../includes/common.inc.php;    $_clean = array();    $_info = array();    $_data = array();    $_clean[shopId] =_check($_POST[shopId]);    $_clean[userId] =_check($_POST[userId]);    $oldpic = $_FILES[picFile][tmp_name];    if ( $_clean[shopId]!=‘‘ && $_clean[userId]!=‘‘ ) {        $_info[state] = 0;        $_info[info] = ‘‘;        $max_file_size=500000;  //大小500kb        if($max_file_size <$oldpic["size"]){              $_info[state] = 2;            $_info[info] = 文件太大;            echo _json($_info);            die;            }else{                $_sql1 = "SELECT * FROM tb_easy_Comments WHERE user = ‘{$_clean[‘userId‘]}‘ and contentId = ‘{$_clean[‘contentId‘]}‘";                $_result1 = _query($_sql1);                $_row1 = _num_rows($_result1);                    if ( $_row1 > 0 ) {                    $_info[state] = 0;                    $_info[info] = 您已经评论过;                    echo _json($_info);                    die;                    }else{                        $width = 200;                        $height = 200;                        header(Content-Type: image/jpeg,image/png,image/gif);                        //获取到图片的宽和高                        list($width_orig, $height_orig) = getimagesize($oldpic);                        $ratio_orig = $width_orig/$height_orig;                        //算比例                        if ($width/$height > $ratio_orig) {                           $width = $height*$ratio_orig;                        } else {                           $height = $width/$ratio_orig;                        }                        //创建                        $image_p = imagecreatetruecolor($width, $height);                        $image = imagecreatefrompng($oldpic);                        imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);                        $pic = date(YmdHis).rand(1000,9999)...jpg;                        imagejpeg($image_p,"D:/www/web/php_easyhunt/images/easyCommentsPic/".$pic , 100);                            $_data[info] ="http://". $_SERVER[SERVER_NAME]./images/easyCommentsPic/.$pic;                        $_info[data] = $_data;                        echo _json($_info);                    }            }        }else{        $_data[state] = 5;        $_data[info] = 参数有误!;    }        $_info[data] = $_data;        echo _json($_info);//请勿删除以下内容   以后备用    //    require dirname(__FILE__).‘/../includes/common.inc.php‘;//    $_clean = array();//    $_info = array();//    $_data = http://www.mamicode.com/array();>//    $_clean[‘shopId‘] =_check($_POST[‘shopId‘]);//    $_clean[‘userId‘] =_check($_POST[‘userId‘]);//    //    $oldpic = $_FILES[‘picFile‘][‘name‘];//    //上传后的名称//    $newpic = date(‘YmdHis‘).rand(1000,9999).$oldpic; //    //    if ( $_clean[‘shopId‘]!=‘‘ && $_clean[‘userId‘]!=‘‘ ) {//    //        $_info[‘state‘] = 0;//        $_info[‘info‘] = ‘‘;//        $max_file_size=500000;  //大小500kb//        if($max_file_size <$oldpic["size"]){  //            $_info[‘state‘] = 2;//            $_info[‘info‘] = ‘文件太大‘;//            echo _json($_info);//            die;//            }//            //接收文件目录  //            $target_path = "D:/www/web/php_easyhunt/images/companyImages/";        //            if(move_uploaded_file($_FILES[‘picFile‘][‘tmp_name‘], $target_path. $newpic)) { //            }//            $_data[‘info‘] ="http://". $_SERVER[‘SERVER_NAME‘].‘/images/companyImages/‘.$newpic;    //    }else{//        $_data[‘state‘] = 5;//        $_data[‘info‘] = ‘参数有误!‘;////    }//    //    $_info[‘data‘] = $_data;//    //    echo _json($_info);////        $image_p = imagecreatetruecolor($width, $height);//        $type = substr($_absolutePath,strrpos($_absolutePath, ‘.‘)+1); //       if($type == "jpg" || $type == "JPEG"||$type == "jpeg") {//           $image = imagecreatefromjpeg($_absolutePath);//       } else if ($type == "png") {//           $image = imagecreatefrompng($_absolutePath);//       } else if ($type == "gif") {//           $image = imagecreatefromgif($_absolutePath);//        }//////        function scaleImage(){//        /*$_absolutePath="http://115.28.233.45/mobile_client".$_path;*///        $_absolutePath="http://115.28.233.45/mobile_client/imgs/comments/4_220/201412270932136d2fcd32-7764-4081-9ae5-6f85bbfadf71.JPEG";//        $width = 200;//        $height = 200;//        header(‘Content-Type: image/jpeg,image/png,image/gif‘);//        //获取到图片的宽和高//        list($width_orig, $height_orig) = getimagesize($_absolutePath);//        $ratio_orig = $width_orig/$height_orig;//        //算比例//        if ($width/$height > $ratio_orig) {//            $width = $height*$ratio_orig;//        } else {//            $height = $width/$ratio_orig;//        }//        //创建//        $image_p = imagecreatetruecolor($width, $height);//        $type = substr($_absolutePath,strrpos($_absolutePath, ‘.‘)+1); //       if($type == "jpg" || $type == "JPEG"||$type == "jpeg") {//           $image = imagecreatefromjpeg($_absolutePath);//       } else if ($type == "png") {//           $image = imagecreatefrompng($_absolutePath);//       } else if ($type == "gif") {//           $image = imagecreatefromgif($_absolutePath);//        }//       imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);//       $pic ="smalll_".substr($_absolutePath,strrpos($_absolutePath, ‘/‘)+1);//       imagejpeg($image_p,"imgs/comments/".$pic , 100);//    }?>
View Code

 

php 图片压缩处理