首頁 php教程 php手册 php图片缩放代码-按比例缩放或截取指定大小的缩略图 非常好用的一个方法

php图片缩放代码-按比例缩放或截取指定大小的缩略图 非常好用的一个方法

Jun 13, 2016 am 10:46 AM
function php 程式碼 圖片 截取 指定 方法 用的 縮放

 

/**********************************************************

 * function resize:

 *

 *  = creates a resized image based on the max width

 *    specified as well as generates a thumbnail from

 *    a rectangle cut from the middle of the image.

 *

     *    @dir    = directory image is stored in

     *    @newdir = directory new image will be stored in

     *    @img    = the image name

     *    @newimg = the image new name

     *    @max_w  = the max width of the resized image

     *    @max_h  = the max height of the resized image

     *    @th_x  = The starting point of the x-coordinate

     *    @th_y  = The starting point of the y-coordinate

     *    @th_w  = the width of the thumbnail

     *    @th_h  = the height of the thumbnail

     *    @cut = Whether to need cut out

     *    @center = whether cut from the middle of the image

 *

 **********************************************************/ 

 

function resize($dir, $newdir, $img, $newimg, $max_w, $max_h, $th_x = '', $th_y = '', $th_w = '', $th_h = '',$cut = FALSE,$center = FALSE) 

   // set destination directory 

   if (!$newdir) $newdir = $dir; 

   if (!$newimg) $newimg = $img; 

 

   // get original images width and height 

   list($or_w, $or_h, $or_t) = getimagesize($dir.$img); 

    

   switch($or_t){ 

    // original image 

    case 1: 

        $or_image = imagecreatefromgif($dir.$img); 

        break; 

    case 2: 

        $or_image = imagecreatefromjpeg($dir.$img); 

        break; 

    case 3: 

        $or_image = imagecreatefrompng($dir.$img); 

        break; 

    default: 

        return '不支持的图像格式';  www.2cto.com

        break;  

   } 

 

   // make sure image is a jpeg 

   // if ($or_t == 2) { 

    

       // obtain the image's ratio 

//       $ratio = ($or_h / $or_w); 

        $ratio = ($max_h / $max_w); 

 

       // original image 

//       $or_image = imagecreatefromjpeg($dir.$img); 

 

       // resize image?  

       if ($or_w > $max_w || $or_h > $max_h) { 

 

           // resize by height, then width (height dominant) 

           if ($max_h

               $rs_h = $max_h; 

               $rs_w = $rs_h / $ratio; 

           } 

           // resize by width, then height (width dominant) 

           else { 

               $rs_w = $max_w; 

               $rs_h = $ratio * $rs_w; 

           } 

 

           // copy old image to new image 

           $rs_image = imagecreatetruecolor($rs_w, $rs_h); 

           imagecopyresampled($rs_image, $or_image, 0, 0, 0, 0, $rs_w, $rs_h, $or_w, $or_h); 

       } 

       // image requires no resizing 

       else { 

           $rs_w = $or_w; 

           $rs_h = $or_h; 

 

           $rs_image = $or_image; 

       } 

 

       // generate resized image 

       DrawImage($rs_image, $newdir.$newimg,$or_t,100); 

 

        

       //裁剪生成指定的大小start------------------- 

       if ($cut){ 

            $th_image = imagecreatetruecolor($th_w, $th_h); 

 

           // cut out a rectangle from the resized image and store in thumbnail 

           if ($center){ 

                $new_w = (($rs_w / 2) - ($th_w / 2)); 

                $new_h = (($rs_h / 2) - ($th_h / 2)); 

           }else { 

                $new_w = $th_x; 

                $new_h = $th_y; 

           } 

            

     

           imagecopyresampled($th_image, $rs_image, 0, 0, $new_w, $new_h, $rs_w, $rs_h, $rs_w, $rs_h); 

     

           // generate thumbnail 

           DrawImage($th_image, $newdir.'th_'.$newimg,$or_t,100); 

           @ImageDestroy($th_image); 

       } 

        

       //生成指定的大小end------------------- 

 

//       return true; 

   //}  

 

   // Image type was not jpeg! 

   /* else {

       return false;

   } */ 

   @ImageDestroy($rs_image); 

 

/**

*   @resource image 

*   @filename

*   @quality

*   @type

**/ 

 

function DrawImage($resource,$filename,$type,$quality){ 

    switch($type){ 

    // original image 

    case 1: 

        $or_image = imagegif($resource,$filename); 

        break; 

    case 2: 

        $or_image = imagejpeg($resource,$filename,$quality); 

        break; 

    case 3: 

        $or_image = imagepng($resource,$filename); 

        break; 

      

   } 

 

摘自 aa705123123的专栏

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
2 週前 By 尊渡假赌尊渡假赌尊渡假赌
倉庫:如何復興隊友
4 週前 By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒險:如何獲得巨型種子
3 週前 By 尊渡假赌尊渡假赌尊渡假赌

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
2 週前 By 尊渡假赌尊渡假赌尊渡假赌
倉庫:如何復興隊友
4 週前 By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒險:如何獲得巨型種子
3 週前 By 尊渡假赌尊渡假赌尊渡假赌

熱門文章標籤

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

適用於 Ubuntu 和 Debian 的 PHP 8.4 安裝和升級指南 適用於 Ubuntu 和 Debian 的 PHP 8.4 安裝和升級指南 Dec 24, 2024 pm 04:42 PM

適用於 Ubuntu 和 Debian 的 PHP 8.4 安裝和升級指南

CakePHP 專案配置 CakePHP 專案配置 Sep 10, 2024 pm 05:25 PM

CakePHP 專案配置

CakePHP 日期和時間 CakePHP 日期和時間 Sep 10, 2024 pm 05:27 PM

CakePHP 日期和時間

CakePHP 檔案上傳 CakePHP 檔案上傳 Sep 10, 2024 pm 05:27 PM

CakePHP 檔案上傳

CakePHP 路由 CakePHP 路由 Sep 10, 2024 pm 05:25 PM

CakePHP 路由

討論 CakePHP 討論 CakePHP Sep 10, 2024 pm 05:28 PM

討論 CakePHP

如何設定 Visual Studio Code (VS Code) 進行 PHP 開發 如何設定 Visual Studio Code (VS Code) 進行 PHP 開發 Dec 20, 2024 am 11:31 AM

如何設定 Visual Studio Code (VS Code) 進行 PHP 開發

CakePHP 快速指南 CakePHP 快速指南 Sep 10, 2024 pm 05:27 PM

CakePHP 快速指南

See all articles