PHP 之 代理模式

來源:互聯網
上載者:User

標籤:代理模式 php 圖片代理

代理模式是很好用的,不過我們經常用JS來實現一些圖片的懶載入,而且現在有很多繼承好的js


對於PHP的,肯定不僅僅限於圖片,不過這次的例子還是PHP的圖片代理,是可以直接顯示圖片的,修改下路徑就好。


應用情境:1.圖片代理,2.遠程代理,3.智能指引,4.虛擬代理,5.動態代理


一般是開啟多線程。代理對象中一個線程向用戶端瀏覽器載入一個小圖片,第二個線程調用大圖片載入程式第三個線程,當使用者瀏覽大圖還沒有載入出來就顯示 相應的提示資訊  (這個樣本沒有利用線程)

這樣的話就完全將載入圖片放在了後台,同樣處理其他的業務也是可以借鑒


上代碼:

<?php//多用於功能列表,繼承一些公用的介面函數interface Image{public function getWidth();public function getHeight();public function getPath();/**       * @return string   the image‘s byte stream       */     public function dump();}//可以多體會下抽象對象的用法,不能執行個體化abstract class AbstractImage implements Image{protected $_width;protected $_height;protected $_path;protected $_data;protected $_type;public function getWidth(){return $this->_width;}public function getHeight(){return $this->_height;}public function getPath(){return $this->_path;}}//具體的實體物件 繼承抽象類別對於介面的重寫//可以直接使用抽象對象的通用屬性width,height,path,data//包括可以直接重新定義介面裡的函數//這是實際的圖片對象class RawImage extends AbstractImage{public function __construct($path){$this->_path = $path;//list() 函數用數組中的元素為一組變數賦值。按照數組的數字索引 依次賦值//注意,與 array() 類似,list() 實際上是一種語言結構,不是函數。list($this->_width,$this->_height) = getimagesize($path);$this->_type = getimagesize($path)[‘mime‘];//file_get_contents() 函數把整個檔案讀入一個字串中。//和 file() 一樣,不同的是 file_get_contents() 把檔案讀入一個字串。//file_get_contents() 函數是用於將檔案的內容讀入到一個字串中的首選方法。如果作業系統支援,還會使用記憶體映射技術來增強效能。$this->_data = file_get_contents($path);}public function dump_type(){return $this->_type;}public function dump(){return $this->_data;}}//它和實際的圖片對象繼承同一個抽象介面,基本上就是同樣的//這時候就可以增加很多人性化的功能,與圖片無關,與使用者體驗有關class ImageProxy extends AbstractImage{protected $_realImage;public function __construct($path){$this->_path = $path;list($this->_width,$this->_height) = getimagesize($path);$this->_type = getimagesize($path)[‘mime‘];//這裡就沒必要擷取圖片的真實資料,畢竟很大} /**       * Creates a RawImage and exploits its functionalities.       */     //這裡去擷取真實圖片的所有資料protected function _lazyLoad(){if($this->_realImage === null){$this->_realImage = new RawImage($this->_path);}}public function dump_type(){return $this->_type;}public function dump(){$this->_lazyLoad();return $this->_realImage->dump();}}//基本上一個很簡單的代理寫完了,如何發揮更多的效用,需要好好引進去很多處理思路,但是位置一定是寫在代理裡面//下面就是用戶端類class Client{public function tag(Image $img){ $type=$img->dump_type(); header("content-type:$type");             echo $img->dump();}}$path = ‘d:/image/timg3.jpg‘;$client = new Client();$image = new ImageProxy($path);//$image = new RawImage($path);$client->tag($image);?>



願法界眾生,皆得安樂。

本文出自 “一站式解決方案” 部落格,請務必保留此出處http://10725691.blog.51cto.com/10715691/1954719

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.