上傳圖片時壓縮圖片 - 後端做法

來源:互聯網
上載者:User

標籤:壓縮圖片   後端   cti   建立   exists   將不   link   compress   param   

    /**     * 函數:調整圖片尺寸或產生縮圖 v 1.1     * @param $Image 需要調整的圖片(含路徑)     * @param $Dw 調整時最大寬度;縮圖時的絕對寬度     * @param $Dh 調整時最大高度;縮圖時的絕對高度     * @param $Type 1,調整尺寸; 2,產生縮圖     * @return bool     */    public function compressImg($image, $Dw, $Dh, $type)    {        if (!file_exists($image)) {            return false;        }        // 如果需要產生縮圖,則將原圖拷貝一下重新給$Image賦值(產生縮圖操作)        // 當Type==1的時候,將不拷貝原影像檔,而是在原來的影像檔上重建縮小後的映像(調整尺寸操作)        if ($type != 1) {            copy($image, str_replace(".", "_x.", $image));            $image = str_replace(".", "_x.", $image);        }        // 取得檔案的類型,根據不同的類型建立不同的對象        $ImgInfo = getimagesize($image);        switch ($ImgInfo[2]) {            case 1:                $Img = @imagecreatefromgif($image);                break;            case 2:                $Img = @imagecreatefromjpeg($image);                Break;            case 3:                $Img = @imagecreatefrompng($image);                break;        }        // 如果對象沒有建立成功,則說明非圖片檔案        if (empty($Img)) {            // 如果是產生縮圖的時候出錯,則需要刪掉已經複製的檔案            if ($type != 1) {                unlink($image);            }            return false;        }        // 如果是執行調整尺寸操作則        if ($type == 1) {            $w = imagesx($Img);            $h = imagesy($Img);            $width = $w;            $height = $h;            if ($width > $Dw) {                $Par = $Dw / $width;                $width = $Dw;                $height = $height * $Par;                if ($height > $Dh) {                    $Par = $Dh / $height;                    $height = $Dh;                    $width = $width * $Par;                }            } elseif ($height > $Dh) {                $Par = $Dh / $height;                $height = $Dh;                $width = $width * $Par;                if ($width > $Dw) {                    $Par = $Dw / $width;                    $width = $Dw;                    $height = $height * $Par;                }            } else {                $width = $Dw;                $height = $Dh;            }            $nImg = imagecreatetruecolor($Dw, $Dh); // 建立一個真彩色畫布            $white = imagecolorallocate($nImg, 255, 255, 255);            // 填充白底色            imagefill($nImg, 0, 0, $white);            if ($h / $w > $Dh / $Dw) { // 高比較大                $width = $w * ($Dh / $h);                $IntNW = $Dw - $width;                $Dx = $IntNW / 2;                $Dy = 0;            } else { // 寬比較大                $height = $h * ($Dw / $w);                $IntNH = $Dh - $height;                $Dx = 0;                $Dy = $IntNH / 2;            }            imagecopyresampled($nImg, $Img, $Dx, $Dy, 0, 0, $width, $height, $w, $h); // 重採樣拷貝部分映像並調整大小            imagejpeg($nImg, $image); // 以JPEG格式將映像輸出到瀏覽器或檔案            return true;        } else { // 如果是執行產生縮圖操作則            $w = imagesx($Img);            $h = imagesy($Img);            $nImg = imagecreatetruecolor($Dw, $Dh);            $white = imagecolorallocate($nImg, 255, 255, 255);            // 填充白底色            imagefill($nImg, 0, 0, $white);            if ($h / $w > $Dh / $Dw) { // 高比較大                $width = $w * ($Dh / $h);                $IntNW = $Dw - $width;                imagecopyresampled($nImg, $Img, $IntNW / 2, 0, 0, 0, $width, $Dh, $w, $h);            } else { // 寬比較大                $height = $h * ($Dw / $w);                $IntNH = $Dh - $height;                imagecopyresampled($nImg, $Img, 0, $IntNH / 2, 0, 0, $Dw, $height, $w, $h);            }            imagejpeg($nImg, $image);            return true;        }    }

需要注意的是: 如果是要產生寬高相等的圖片,輸出到頁面的時候也要設定寬高等比例的

上傳圖片時壓縮圖片 - 後端做法

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.