php的縮放映像類使用

來源:互聯網
上載者:User

標題: php的縮放映像類使用
作者:李英江
日期: 2006-11-25 14:54:05
轉載請保留作者內容: http://www.cgsir.com

  使用縮圖有個好處就是可以減少使用者下載的時間,為此要在上傳圖片時產生一張比原圖小的圖片,一般的PHP空間都支援GD圖形庫,我的網站上GD版本是2.0.28,你可以自己寫映像產生程式,php手冊有現成的例子,應該不難,但為了更快完成這功能,試用網上下載的一個縮放映像類,感覺也蠻方便的。
  注意:確認你的php支援GD庫,你可以通過使用 <?php phpinfo(); ?>查看你的GD版本,以下我的網站GD版本

如果你的PHP沒有這些內容,那麼說明你的php還不支援這個庫,我針對windows 和linux分別作說明。

Windows:
  找到你的php.ini的設定檔,開啟GD動態連結程式庫就可以了。也就是說找到“;extension=php_gd2.dll”把分號去掉,重起apache就可以了。

Linux:
  # tar -zxf gd-2.0.33.tar.gz
  # cd gd-2.0.33
  # ./configure --prefix=/usr/local/gd2 --with-jpeg=/usr/local/jpeg6/ --with-png --with-zlib --with-freetype=/usr/local/freetype/
  # make; make install

使用縮放映像類:
  1. 包含類 include_once('image_class.php');   // 用於產生縮圖類
  2. 產生縮圖 120*90  0為不載圖 $small_img = new resizeimage($image_full_dir, 120, 90, 0); ($image_full_dir映像儲存路徑)

以下是完整映像類image_class.php 源碼:

<?
/***************************************/
/*功  能:利用PHP的GD庫產生高品質的縮圖*/
/*運行環境:PHP5.01/GD2*/
/*類說明:可以選擇是/否裁圖。

          如果裁圖則產生的圖的尺寸與您輸入的一樣。
          原則:儘可能多保持原圖完整

          如果不裁圖,則按照原圖比例產生新圖
          原則:根據比例以輸入的長或者寬為基準*/
/*參 數:$img:源圖片地址
         $wid:新圖的寬度
         $hei:新圖的高度
         $c:是否裁圖,1為是,0為否*/

/***************************************/
class resizeimage
{
    //圖片類型
    var $type;
    //實際寬度
    var $width;
    //實際高度
    var $height;
    //改變後的寬度
    var $resize_width;
    //改變後的高度
    var $resize_height;
    //是否裁圖
    var $cut;
    //源圖象
    var $srcimg;
    //靶心圖表象地址
    var $dstimg;
    //臨????建的圖象
    var $im;

    function resizeimage($img, $wid, $hei,$c)
    {
        $this->srcimg = $img;
        $this->resize_width = $wid;
        $this->resize_height = $hei;
        $this->cut = $c;
        //圖片的類型
        $this->type = substr(strrchr($this->srcimg,"."),1);
        //初始化圖象
        $this->initi_img();
        //靶心圖表象地址
        $this -> dst_img();
        //--
        $this->width = imagesx($this->im);
        $this->height = imagesy($this->im);
        //產生圖象
        $this->newimg();
        ImageDestroy ($this->im);
    }
    function newimg()
    {
        //改變後的圖象的比例
        $resize_ratio = ($this->resize_width)/($this->resize_height);
        //實際圖象的比例
        $ratio = ($this->width)/($this->height);
        if(($this->cut)=="1")
        //裁圖
        {
            if($ratio>=$resize_ratio)
            //高度優先
            {
                $newimg = imagecreatetruecolor($this->resize_width,$this->resize_height);
                imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, $this->resize_width,$this->resize_height, (($this->height)*$resize_ratio), $this->height);
                ImageJpeg ($newimg,$this->dstimg);
            }
            if($ratio<$resize_ratio)
            //寬度優先
            {
                $newimg = imagecreatetruecolor($this->resize_width,$this->resize_height);
                imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, $this->resize_width, $this->resize_height, $this->width, (($this->width)/$resize_ratio));
                ImageJpeg ($newimg,$this->dstimg);
            }
        }
        else
        //不裁圖
        {
            if($ratio>=$resize_ratio)
            {
                $newimg = imagecreatetruecolor($this->resize_width,($this->resize_width)/$ratio);
                imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, $this->resize_width, ($this->resize_width)/$ratio, $this->width, $this->height);
                ImageJpeg ($newimg,$this->dstimg);
            }
            if($ratio<$resize_ratio)
            {
                $newimg = imagecreatetruecolor(($this->resize_height)*$ratio,$this->resize_height);
                imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, ($this->resize_height)*$ratio, $this->resize_height, $this->width, $this->height);
                ImageJpeg ($newimg,$this->dstimg);
            }
        }
    }
    //初始化圖象
    function initi_img()
    {
        if($this->type=="jpg")
        {
            $this->im = imagecreatefromjpeg($this->srcimg);
        }
        if($this->type=="gif")
        {
            $this->im = imagecreatefromgif($this->srcimg);
        }
        if($this->type=="png")
        {
            $this->im = imagecreatefrompng($this->srcimg);
        }
    }
    //圖象目標地址
    function dst_img()
    {
        $full_length  = strlen($this->srcimg);
        $type_length  = strlen($this->type);
        $name_length  = $full_length-$type_length;
        $name         = substr($this->srcimg,0,$name_length-1);
        $this->dstimg = $name."s.".$this->type;
    }
}
?>

 

相關文章

聯繫我們

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