温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

PHP 之 代理模式

发布时间:2020-07-30 19:13:08 来源:网络 阅读:450 作者:jackdongting 栏目:开发技术

代理模式是很好用的,不过我们经常用JS来实现一些图片的懒加载,而且现在有很多继承好的js


对于PHP的,肯定不仅仅限于图片,不过这次的例子还是PHP的图片代理,是可以直接显示图片的,修改下路径就好。


应用情境:1.图片代理,2.远程代理,3.智能指引,4.虚拟代理,5.动态代理


一般是开启多线程。代理对象中一个线程向客户端浏览器加载一个小图片,第二个线程调用大图片加载程序第三个线程,当用户浏览大图还没有加载出来就显示 相应的提示信息  (这个示例没有利用线程

这样的话就完全将加载图片放在了后台,同样处理其他的业务也是可以借鉴


上代码:

<?php
	//多用于功能列表,继承一些公用的接口函数
	interface Image{
		public function getWidth();
		public function getHeight();
		public function getPath();

	/**  
     * @return string   the p_w_picpath'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) = getp_w_picpathsize($path);
			$this->_type = getp_w_picpathsize($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) = getp_w_picpathsize($path);
			$this->_type = getp_w_picpathsize($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:/p_w_picpath/timg3.jpg';
	$client = new Client();


	$p_w_picpath = new ImageProxy($path);
	//$p_w_picpath = new RawImage($path);
	$client->tag($p_w_picpath);
?>



愿法界众生,皆得安乐。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI