Shortest possible encoded string with decode possibility (shorten url) using only PHP

前端 未结 13 1738
甜味超标
甜味超标 2020-12-28 19:14

I\'m looking for a method that encodes an string to shortest possible length and lets it be decodable (pure PHP, no SQL). I have working sc

13条回答
  •  温柔的废话
    2020-12-28 19:57

    In your question you state that it should be pure PHP and not use a database, and there should be a possibility to decode the strings. So bending the rules a bit:

    • The way I am interpreting this question is that we don't care about security that much but, we do want the shortest hashes that lead back to images.
    • We can also take "decode possibility" with a pinch of salt by using a one way hashing algorithm.
    • We can store the hashes inside a JSON object, then store the data in a file, so all we have to do at the end of the day is string matching

    ```

    class FooBarHashing {
    
        private $hashes;
    
        private $handle;
    
        /**
         * In producton this should be outside the web root
         * to stop pesky users downloading it and geting hold of all the keys.
         */
        private $file_name = './my-image-hashes.json';
    
        public function __construct() {
            $this->hashes = $this->get_hashes();
        }
    
        public function get_hashes() {
            // Open or create a file.
            if (! file_exists($this->file_name)) {
                fopen($this->file_name, "w");
            }
            $this->handle = fopen($this->file_name, "r");
    
    
            $hashes = [];
            if (filesize($this->file_name) > 0) {
                $contents = fread($this->handle, filesize($this->file_name));
                $hashes = get_object_vars(json_decode($contents));
            }
    
            return $hashes;
        }
    
        public function __destroy() {
            // Close the file handle
            fclose($this->handle);
        }
    
        private function update() {
            $handle = fopen($this->file_name, 'w');
            $res = fwrite($handle, json_encode($this->hashes));
            if (false === $res) {
                //throw new Exception('Could not write to file');
            }
    
            return true;
        }
    
        public function add_hash($image_file_name) {
            $new_hash = md5($image_file_name, false);
    
            if (! in_array($new_hash, array_keys($this->hashes) ) ) {
                $this->hashes[$new_hash] =  $image_file_name;
                return $this->update();
            }
    
            //throw new Exception('File already exists');
        }
    
        public function resolve_hash($hash_string='') {
            if (in_array($hash_string, array_keys($this->hashes))) {
                return $this->hashes[$hash_string];
            }
    
            //throw new Exception('File not found');
        }
    }
    

    ```

    Usage example:

    add_hash('img=/dir/dir/hi-res-img.jpg&w=700&h=500');
    
    // Then when the user requests the hash the query string is returned.
    echo $hashing->resolve_hash('65992be720ea3b4d93cf998460737ac6');
    

    So the end result is a string that is only 32 chars long, which is way shorter than the 52 we had before.

提交回复
热议问题