Class library for working with the Yandex.Fotki API service

    The library will allow you to use the Yandex.Fotki API service in projects written in PHP and covers all the features that the API currently provides.
    Page about the library:

    Using this library you can:
    • Organize a photo gallery or photo gallery system on the site
    • Manage your gallery, including uploading and deleting photos, creating and deleting albums, and managing metadata

    The Yandex.Fotki service should be used, since:
    • You do not waste space storing images on your project server
    • For the gallery to work, you do not need to use a database or other tool to store and manage metadata
    • The service creates preview images with a width of 800,500,300,150,100,75 and 50px and you do not need to do it yourself.
    • It is highly likely that the Yandex servers on which the materials will be stored are more reliable than your hosting provider
    • There is a high probability that the user’s upload speed will be higher than when downloading them from your server

    Under the cut you will find an example of work and its code, provided with brief comments.


    Gallery of Vladislav Mamontovich’s favorite photographs

    ’ll make a reservation: - No, you won’t be able to create image hosting based on this service, as this will violate paragraph 1.2.4. user agreement of the Yandex.Fotki API service. The example chosen is just that for reasons of clarity and is ... not entirely correct. I hope that Yandex employees will treat him condescendingly. Or at least they will warn before blocking the unfortunate Vladislav Mamontovich.

    The example consists of 2 files:
    1. example.html - display gallery
    2. upload_image.html - upload images

    I will not focus on anything other than the code where work directly with Yandex.Fotki is going on. If you have questions related to any code - ask them in the comments. I will answer for sure.


    What are the key features this page should implement:
    • Show a list of the last 25 images uploaded by Mamontovich
    • If we are given the identifier of the downloaded image, list the links to the preview images created by the Yandex.Fotki service and the original image
    • Display the image upload form so that Vladislav Mamontovich could replenish his gallery

    1. Create a user on behalf of whom we will work with the service. Namely - Vladislav Mamontovich.

    1. $ photo = new yandex_fotki ("MojoElephant");

    2. Get a common collection of photos of Mamontovich

    1. $ photo-> user () -> add_photo_collection ("Elephant Photo");

    3. Get the last 25 photos uploaded by Mamontovich

    1. $ photo-> user () -> photo_collection ("Elephant Photo") -> se (array ("limit" => 25));

    4. After going through them all we will form a gallery

    1. foreach ($ photo-> user () -> photo_collection ("Elephant Photo") -> photo_list (0) as $ index => $ photo_element) {
    2. $ photo_block = new template_class ("Photo", "../../ templates / photo.html");
    3. $ photo_block-> point ("Entry Points");
    4. $ photo_block-> plug ("Entry Points", "Enlarged", "Enlarged", $ photo_element-> get_L ());
    5. $ photo_block-> plug ("Entry Points", "Preview", "Preview", $ photo_element-> get_XXS ());
    6. $ gallery-> plug ("Entry Points", "Photo", $ index, $ photo_block-> render ());
    7. }

    5. If Mamontovich uploaded the photo, then the identifier of the new photo was transferred to the script. We get a freshly loaded photo by its id

    1. $ new_photo = $ photo-> user () -> photo_collection ("Elephant Photo") -> get_photo_by_id ($ photo_id);

    6. Display a list of links

    1. $ link_block = new template_class ("Links", "../../ templates / link_block.html");
    2. $ link_block-> point ("Input Points");
    3. $ link_block-> plug ("Input Points", "Original", "Link", $ new_photo-> get_orig ());
    4. $ link_block-> plug ("Input Points", "XL", "Link", $ new_photo-> get_XL ());
    5. $ link_block-> plug ("Input points", "L", "Link", $ new_photo-> get_L ());
    6. $ link_block-> plug ("Input Points", "M", "Link", $ new_photo-> get_M ());
    7. $ link_block-> plug ("Input Points", "S", "Link", $ new_photo-> get_S ());
    8. $ link_block-> plug ("Input Points", "XS", "Link", $ new_photo-> get_XS ());
    9. $ link_block-> plug ("Input Points", "XXS", "Link", $ new_photo-> get_XXS ());
    10. $ link_block-> plug ("Entry Points", "XXXS", "Link", $ new_photo-> get_XXXS ());


    What are the key features this page should implement:
    • Save the sent image on the Yandex.Fotki service
    • Report example.html identifier of the image uploaded to the server

    1. Since uploading photos is possible only for an authenticated user, we create an object of class yandex_fotki and authenticate.

    1. $ photo = new yandex_fotki ("MojoElephant", "password");

    2. After that, you need to get a collection of photos, in which we will add a new photo of Mamontovich.

    1. $ photo-> user () -> add_photo_collection ("Elephant Photo");

    3. And, in fact, upload the photo to Yandex.Fotki. After downloading, we will get the photo ID.

    1. $ new_photo = array (
    2. "path" => "../../ uploaded /".$_ FILES [" photo "] [" name "]
    3. );
    4. $ result = $ photo-> user () -> photo_collection ("Elephant Photo") -> up ($ new_photo);

    4. Save the photo ID. You can use any method, at least get it through. For this purpose, I use a message system that stores data in a session.

    5. We direct Mamontovich to example.html, where he will receive links to the uploaded photo and see it in the gallery.


    As you can see, everything is extremely simple and pleasant.
    Using a minimum of effort, you can implement a gallery on the site.
    The only minus of storing photos on Yandex.Fotki is that it takes longer for the service to request metadata than to store it on your service. But the wise use of caching is almost nullifying it.

    Materials for further study


    Many thanks to ar2r and nickmitin for their help with porting the encryption algorithm, proto for clarifying the Yandex.Fotki API project , comments and to all everyone else for the time that was given to me. And no less thanks to those people from the #php RusNet channel who gave my work an expert evaluation and advised what could be done better. mz, avz, focusshifter and all the rest. Thanks you.

    The code is highlighted by which was invented by antonshevchuk

    What's next?

    Next will be the search and correction of errors, code refactoring, writing more detailed articles on the use of the library and the ideology of Yandex. The flow, possibly creating plug-ins for several CMS. Yes, in fact, everything that will be needed and interesting to me and you.
    But in order for there to be at least something else, I need to know that someone needs my work. If the library may be useful to you, write about it. Do not be silent.

    PS If you liked the library and you think about DEVCONF participation - vote for my report on the conference website:

    Also popular now: