zoukankan      html  css  js  c++  java
  • IImage--factory

    <?php
    /* 实例4 */
    /* 使用工厂类解析图像工作 */
    interface IImage {
        function getWidth();
        function getHeight();
        function getData();
    }
    class Image_PNG implements IImage {
        protected $_width, $height, $_data;
        
        public function __construct($file) {
            $this->_file = $file;
            $this->_parse();
        }
        
        private function _parse() {
            //完成PNG格式的解析工作
            //并填充$_width, $_height, $_data
            $this->_data = getimagesize($this->_file);
            list($this->_width, $this->_height) = $this->_data;
        }
        
        public function getWidth() {
            return $this->_width;
        }
        
        public function getHeight() {
            return $this->_height;
        }
        
        public function getData() {
            return $this->_data;
        }
    }
    class Image_JPGE implements IImage {
        
        protected $_width, $_height, $_data;
        
        public function __construct($file) {
            $this->_file = $file;
            $this->_parse();
        }
        
        private function _parse() {
            //完成JPGE格式的解析工作
            //并填充$_width, $_height, $_data
            //$this->_width = imagesx($this->_file);
            //$this->_height = imagesx($this->_file);
            $this->_data = getimagesize($this->_file);
            list($this->_width, $this->_height) = $this->_data;
        }
        
        public function getWidth(){
            return $this->_width;
        }
        
        public function getHeight(){
            return $this->_height;
        }
        
        public function getData() {
            return $this->_data;
        }
    }
    class ImageFactory {
        public static function factory($file) {
            $filename = pathinfo($file);
            switch(strtolower($filename['extension'])){
                case 'jpg':
                    $return = new Image_JPEG($file);
                    break;
                case 'png':
                    $return = new Image_PNG($file);
                    break;
                default:
                    echo '图片类型不正确';
                    break;
            }
            if($return instanceof IImage) {
                return $return ;
            } else {
                echo '出错了';
                exit();
            }
        }
        
    }
    $image = ImageFactory::factory('path/my.png');
    var_dump($image->getWidth());
    echo '<br>';
    print_r($image->getheight());
    echo '<br>';
    print_r($image->getData());
  • 相关阅读:
    January 25th, 2018 Week 04th Thursday
    January 24th, 2018 Week 04th Wednesday
    January 23rd, 2018 Week 04th Tuesday
    January 22nd, 2018 Week 04th Monday
    January 21st, 2018 Week 3rd Sunday
    January 20th, 2018 Week 3rd Saturday
    January 19th, 2018 Week 3rd Friday
    January 18th, 2018 Week 03rd Thursday
    January 17th, 2018 Week 03rd Wednesday
    January 16th, 2018 Week 03rd Tuesday
  • 原文地址:https://www.cnblogs.com/yhdsir/p/4649230.html
Copyright © 2011-2022 走看看