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

前端 未结 13 1782
甜味超标
甜味超标 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:42

    You say that you want the size there so that if you decide some day that the preview images are too small, you want to increase the size - the solution here is to hard code the image size into the php script and eliminate it from the url. If you want to change the size in the future, change the hardcoded values in the php script (or in a config.php that you include into the script).

    You've also said that you are already using files to store image data as a JSON object, like: name, title, description. Exploiting this, you don't need a database and can use the JSON file name as the key for looking up the image data.

    When the user visits a url like this:

    www.mysite.com/share/index.php?ax9v
    

    You load ax9v.json from the location you are already storing the json files, and within that json file the image's real path is stored. Then load the image, resize it according to the hardcoded size in your script and send it to the user.

    Drawing from the conclusions in https://blog.codinghorror.com/url-shortening-hashes-in-practice/ , to get the smallest search string part of the url you would need to iterate valid character combinations as new files are uploaded (eg. the first one is "AAA" then "AAB", "AAC", etc.) instead of using a hashing algorithm. Your solution would then have only 3 characters in the string for the first 238,328 photos you upload.

    I had started to prototype a php solution on phpfiddle but the code disappeared (don't use phpfiddle).

提交回复
热议问题