Easy thumbnail creation and storage on Amazon S3

    No site can do without generating thumbnails of images. On the Internet you can find a million articles on this topic. Perhaps someone will come in handy and this solution.

    Requirements:
    • Images are stored on remote servers. We only have links to these images.
    • The thumbnail should be formed of any given size at the time of direct access to it
    • Pest protection should be provided.
    • The thumbnail must be stored on Amazon S3 and be available on a subdomain of the main site. The number of buckets on S3 and, accordingly, subdomains is unlimited


    Implementation:
    Assume that the name of our site is domain.com

    1) On Amazon S3, we create 4 buckets with the names ic1.domain.com , ic2.domain.com , ic3.domain.com , ic4.domain.com (the names of subdomains may be different , in our case, this is an abbreviation for image cache)

    2) Go to the settings of each bucket and check the Enable website hosting box

    3) In the Edit Redirection Rules field, specify the rules for redirects from non-existent pages:

    404domain.coms3/thumbs/403domain.coms3/thumbs/


    4) Go to the control panel of the domain.com domain name and create subdomains similar to the names of buckets. In CNAME, write Endpoint from the settings of the bucket.

    What we just did:
    Created 4 subdomains on which thumbnails will be stored. Each thumbnail will have an address like: ic1.domain.com/miniatura.jpg

    As soon as this file is first accessed, Amazon will see that there is no such file in the bucket ic1.domain.com and will redirect the user to the path you specified, which is concatenated from HostName + ReplaceKeyPrefixWith : domain.com/s3/thumb/miniatura.jpg

    On the server, we catch a request through a router or .htaccess file, create a thumbnail of the transferred file, send it to Amazon S3 in the desired bucket and display the thumbnail for the first user.

    Other users - will receive it directly from Amazon S3.

    Let's go further.
    It is still unclear exactly how to pass parameters to minimize and a link to the picture itself.
    What do we have:

    Of course, the link of the form: ic1.domain.com/?Img_url=http://blablabla.com/photo15.jpg&Thumb_w=100&Thumb_h=100absolutely does not suit us, because Firstly, Amazon will not understand us, and secondly, there is no protection from pests.

    There may be a million options for creating such links. We decided to do without intermediate tables or records and looked towards RC4 cryptography . That sounds scary. But let's see what happened:
    ic2.domain.com/e2/PUuxR1p~D~Jgl5PrnPMLh4OA0sO899rjZgzgWFU_.jpg
    Pretty nice link. At least not much worse than the md5 hash. But much more informative. If we decrypt the crypt, we get:
    blablabla.com/photo15:100@100
    What we need - all the data on the photo, sizes and plus everything, without knowing the password from the crypt - no one can generate “extra thumbnails”

    The question remained for small - to implement a function that will take a link to the image, size and return a link to a thumbnail (and of course all other scripts).

    Following the link to github, some implementation of the task: github.com/wolflingorg/s3thumb

    Structure:
    CS3Thumb.php - the main class
    S3.php - the class for working with Amazon S3
    CRC4Crypt.php - the class for encryption in RC4
    CThumb.php - the class for creating thumbnails

    How to use:
    $Thumb = new CS3Thumb($backets, $accessKey, $secretKey, $cryptpsw = 'password');
    

    Where:
    $ backets - an array with the names of buckets (they are also subdomains)
    $ accessKey and $ secretKey - access to Amazon
    $ cryptpsw - password for encrypting links

    In order to get a link to the image, use:
    $Thumb -> url("http://blablabla.com/photo15.jpg", 100, 100);
    

    In order to create a thumbnail, move it to S3 and display it to the first user:
    $Thumb -> process("e2/PUuxR1p~D~Jgl5PrnPMLh4OA0sO899rjZgzgWFU_.jpg");
    

    where e2 / PUuxR1p ~ D ~ Jgl5PrnPMLh4OA0sO899rjZgzgWFU_.jpg - what will be in the link after domain.com/s3/thumbs/(*), after the redirect from Amazon

    I hope the article will be useful. Thanks.

    Also popular now: