PHP图层裁剪服务搭建详解( 二 )


接下来 , 分析下php如何才能实现以上功能
总体架构

PHP图层裁剪服务搭建详解

文章插图
 
1、 后台上传图片 , 到文件服务器【不做任务处理】
2、 浏览器发送请求
3、 服务器解析请求 , 并选择指定的处理接口【压缩、裁剪、圆角、加水印 , 其它】
4、 根据请求加载原图片 , 并根据选择的接口生成最终图片
5、 返回给浏览器
实现步骤1、 环境介绍基于PHPstudy集成开发环境
域名:
项目目录: E:projectphpimage
开启 rewrite 重定向 , 优化URL
项目目录结构:
PHP图层裁剪服务搭建详解

文章插图
 
2、图片上传使用百度的 Web Uploader 上传图片即可,具体实现可以参考官网:
http://fex.baidu.com/webuploader/图片上传不是本文分析的重点, 文件上传功能已经实现
前端页面:
PHP图层裁剪服务搭建详解

文章插图
 
后端可以使用百度提供的 , 也可以自己实现 。
3、 Rewrite 重定向下图的案例可以看到 , 我们访问图片是能过网址加图片名称的方式
PHP图层裁剪服务搭建详解

文章插图
 
它是通过 Apache的重定向实现的 , ".htaccess" 这个文件就是配置文件 , 代码如下
<IfModule mod_rewrite.c> RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ img.php </IfModule>4、 入口文件实现所有的图片访问都是通过 "img.php" 文件来实现的 , 那么它是如何工作的..
<?php require_once "function.php"; require_once "init.php";"function.php" 文件
<?php function json($code=0,$msg=''){$arr = ['code'=>$code, 'msg'=>$msg]; exit(json_encode($arr,JSON_UNESCAPED_UNICODE)); }"init.php"文件
<?php define('ROOT_PATH', dirname(__FILE__)); //系统根目录 define('DS',DIRECTORY_SEPARATOR); //目录分隔符 define('IMAGE_PATH',ROOT_PATH . DS . 'img' . DS); //图片路径 define('PLUGIN_PATH',ROOT_PATH . DS . 'plugin' .DS); //插件路径 define('IMAGE_CACHE_PATH',ROOT_PATH . DS . 'img_cache' . DS); //处理过的图片缓存路径 ? include_once PLUGIN_PATH . 'plugin.php'; //加载插件 ? //get请求参数 $query_string = isset($_SERVER['QUERY_STRING'])?$_SERVER['QUERY_STRING']:''; ? //图片处理 , 以插件的形式处理图片 , 每个插件都是对 $im 进行处理 if($query_string != ''){include_once "route.php"; }else{ //输出图片,不做任何处理$plugin = new plugin();header('Content-type:'.$plugin->mime); //输出类型echo file_get_contents($plugin->img_file); exit; }5、 路由的实现这里的路由很简单 , 无非就是通过请求?号后面的第一个参数 , 然后根据参数名引入对应的插件文件 , 然后实现化插件 , 代码:
<?php //取得插件名称 $plugin_name = $query_string; if(strpos($query_string,'/') !== false){$plugin_name = substr($query_string,0,strpos($query_string,'/')); } //加载插件 $plugin_file = PLUGIN_PATH . $plugin_name . '.php'; //插件文件 if(!file_exists($plugin_file)){ json(4,'参数不正确'); }else{include_once $plugin_file;new $plugin_name($query_string); //实例化插件 }6、压缩图片压缩图片是通过 imageslim 这个插件来实现的 ,  文件名 "plugin/imageslim.php"
代码:
<?php class imageslim extends plugin{ public function __construct(){parent::__construct(); ?$create_fun = $this->create_fun; //打开图片函数名$out_fun = $this->out_fun; //输出图片函数名 ?$im = $create_fun($this->img_file); //创建图片对象if(!$im){ json(3,'打开图像失败'); } ?header('Content-type:'.$this->mime); //输出类型$out_fun($im,null,50); //输出图片imagedestroy($im); //销毁图片 } }可以看到 , 实现原理很简单 ,  imageslim 继承 plugin 插件 , 
plugin.php 插件实现一此成员变量的定义 , 及一些初始化操作
比如 , 文件名、文件类型、文件大小 , 代码如下:
<?php class plugin {public $img_name;public $img_file;public $img_mime;public $mime; public $extension;public $create_fun;public $out_fun;public $src_w; //原始图片的宽 public $src_h; //原始图片的高 ?public function __construct() {//文件检查 $this->img_name = trim($_SERVER['REDIRECT_URL'],'/');$this->img_file = IMAGE_PATH . $this->img_name; if(!file_exists($this->img_file)){json(1,'文件不存在'); } //图片类型检查 $this->img_mime = ['image/gif','image/png','image/jpeg','image/bmp','image/webp','image/x-icon','image/tiff']; //图片mime$this->mime = mime_content_type($this->img_file); // 返回 mime 类型 if(!in_array($this->mime,$this->img_mime)){json(2,'不是一张有效图片'); } ?$this->extension = pathinfo($this->img_file,PATHINFO_EXTENSION); //获取图片后缀 $mime_end = substr($this->mime,6);$this->create_fun = 'imagecreatefrom'.$mime_end; //打开图片函数名$this->out_fun = 'image'.$mime_end; //输出图片函数名 ? //原始图片尺寸 $image_info = getimagesize($this->img_file);$this->src_w = $image_info[0]; $this->src_h = $image_info[1]; } }


推荐阅读