php 圖片壓縮處理

來源:互聯網
上載者:User

標籤:

<?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 = 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 圖片壓縮處理

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.