PHP 圖片操作(按照指定尺寸壓縮,按照比例裁剪)

來源:互聯網
上載者:User

標籤:height   mime   log   col   width   sam   nbsp   source   function   

提供二個常用的圖片處理方法:

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); //建立一個彩色的底圖        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 圖片操作(按照指定尺寸壓縮,按照比例裁剪)

相關文章

聯繫我們

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