溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

php怎么將圖片壓縮成指定大小

發(fā)布時間:2020-06-17 14:14:23 來源:億速云 閱讀:621 作者:鴿子 欄目:編程語言

1、按照指定的尺寸壓縮圖片

/**
     * 按照指定的尺寸壓縮圖片
     * @param $source_path  原圖路徑
     * @param $target_path  保存路徑
     * @param $imgWidth     目標寬度
     * @param $imgHeight    目標高度
     * @return bool|string
     */
    function resize_image($source_path,$target_path,$imgWidth,$imgHeight)
    {
        $source_info = getimagesize($source_path);
        $source_mime = $source_info['mime'];
        switch ($source_mime)
        {
            case 'image/gif':
                $source_image = imagecreatefromgif($source_path);
                break;

            case 'image/jpeg':
                $source_image = imagecreatefromjpeg($source_path);
                break;

            case 'image/png':
                $source_image = imagecreatefrompng($source_path);
                break;

            default:
                return false;
                break;
        }
        $target_image     = imagecreatetruecolor($imgWidth, $imgHeight); //創(chuàng)建一個彩色的底圖
        imagecopyresampled($target_image, $source_image, 0, 0, 0, 0, $imgWidth, $imgHeight, $source_info[0], $source_info[1]);
        //保存圖片到本地
        $dir = '../'.$target_path. '/'. date("Ymd") . '/';
        if (!is_dir($dir)) {
            mkdir($dir, 0777);
        }

        $fileName = $dir.date("YmdHis").uniqid().'.jpg';
        if(!imagejpeg($target_image,'./'.$fileName)){
            $fileName = '';
        }
        imagedestroy($target_image);
        return $fileName;
    }

2、按照比例裁剪圖片

/**
     * 圖像裁剪
     * @param $title string 原圖路徑
     * @param $content string 需要裁剪的寬
     * @param $encode string 需要裁剪的高
     * @param $target_path string 需要保存的路徑
     */
    function image_cropper($source_path, $target_width, $target_height, $target_path)
    {
        $source_info     = getimagesize($source_path);
        $source_width     = $source_info[0];
        $source_height     = $source_info[1];
        $source_mime     = $source_info['mime'];
        $source_ratio     = $source_height / $source_width;
        $target_ratio     = $target_height / $target_width;


        if ($source_ratio > $target_ratio) // 源圖過高
        {
            $cropped_width = $source_width;
            $cropped_height = $source_width * $target_ratio;
            $source_x = 0;
            $source_y = ($source_height - $cropped_height) / 2;

        }elseif ($source_ratio < $target_ratio){  // 源圖過寬

            $cropped_width = $source_height / $target_ratio;
            $cropped_height = $source_height;
            $source_x = ($source_width - $cropped_width) / 2;
            $source_y = 0;
        }else{ // 源圖適中

            $cropped_width = $source_width;
            $cropped_height = $source_height;
            $source_x = 0;
            $source_y = 0;
        }

        switch ($source_mime)
        {
            case 'image/gif':
                $source_image = imagecreatefromgif($source_path);
                break;

            case 'image/jpeg':
                $source_image = imagecreatefromjpeg($source_path);
                break;

            case 'image/png':
                $source_image = imagecreatefrompng($source_path);
                break;

            default:
                return false;
                break;
        }

        $target_image = imagecreatetruecolor($target_width, $target_height);
        $cropped_image = imagecreatetruecolor($cropped_width, $cropped_height);

        // 裁剪
        imagecopy($cropped_image, $source_image, 0, 0, $source_x, $source_y, $cropped_width, $cropped_height);
        // 縮放
        imagecopyresampled($target_image, $cropped_image, 0, 0, 0, 0, $target_width, $target_height, $cropped_width, $cropped_height);

        //保存圖片到本地(兩者選一)
        $dir = '../../'.$target_path. '/'. date("Ymd") . '/';
        if (!is_dir($dir)) {
            mkdir($dir, 0777);
        }

        $fileName = $dir.date("YmdHis").uniqid().'.jpg';
        if(!imagejpeg($target_image,'./'.$fileName)){
            $fileName = '';
        }
        imagedestroy($target_image);
        return $fileName;
    }

以上就是php圖片壓縮成指定大小的方法的詳細內容,更多請關注億速云其它相關文章!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內容。

AI