Image upload storage strategies

后端 未结 7 619
天命终不由人
天命终不由人 2020-12-04 07:01

When a user uploads an image to my site, the image goes through this process;

  • user uploads pic
  • store pic metadata in db, giving the image a unique id
相关标签:
7条回答
  • 2020-12-04 07:34

    You can convert a username to md5 and set a folder from 2-3 first letters of md5 converted username for the avatars and for images you can convert and playing with time , random strings , ids and names

    8648b8f3ce06a7cc57cf6fb931c91c55 - devcline

    Also a first letter of the username or id for the next folder or inverse

    It will look like

    Structure:

    stream/img/86/8b8f3ce06a7cc57cf6fb931c91c55.png    //simplest
    stream/img/d/2/0bbb630d63262dd66d2fdde8661a410075.png //first letter and id folders
    stream/img/864/d/8b8f3ce06a7cc57cf6fb931c91c55.png // with first letter of the nick
    stream/img/864/2/8b8f3ce06a7cc57cf6fb931c91c55.png   //with unique id
    stream/img/2864/8b8f3ce06a7cc57cf6fb931c91c55.png    //with unique id in 3 letters
    stream/img/864/2_8b8f3ce06a7cc57cf6fb931c91c55.png   //with unique id in picture name
    

    Code

    $username = substr($username_md5, 1); // to cut first letter from the md5 converted nick
    $username_first = $username[0]; // the first letter
    $username_md5 = md5($username); // md5 for username
    $randomname = uniqid($userid).md5(time());  //for generate a random name based on ID
    

    you can try also with base64

     $image_encode = strtr(base64_encode($imagename), '+/=', '-_,');
     $image_decode = base64_decode(strtr($imagename, '-_,', '+/='));
    

    Steam And dokuwiki use this structure.

    0 讨论(0)
提交回复
热议问题