The library will allow you to use the Yandex.Photo API service in projects written in PHP and covers all the features that the API currently provides.
The page that is dedicated to the work of the library:
code.websaints.netUsing this library you can:
- Organize a photo gallery or a system of photo galleries on the site
- Manage the gallery, including loading and deleting photos, creating and deleting albums, as well as managing metadata
The Yandex.Fotki service should be used because:
- You do not spend space on storing images on the server of your project
- 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.
- There is a high probability that the Yandex servers on which materials will be stored are more reliable than your hosting provider’s
- There is a high probability that the user will download photos more quickly than they download from your server.
Under the cat you will find an example of the work and its code, supplied with brief comments.
Example
Gallery of favorite photos of Vladislav Mamontovich')
Immediately make a reservation:
- No, you can not create image hosting on the basis of this service, as it will violate clause 1.2.4.
User Agreement for the Yandex.Photo API. The example chosen precisely for reasons of clarity, and is ... not entirely correct. I hope that Yandex employees will treat him condescendingly. Or, at least, they will warn you before blocking the unfortunate Vladislav Mamontovich.
The example consists of 2 files:
- example.html - gallery display
- upload_image.html - upload images
I will not focus attention on anything other than the code, where the work with Yandex.Fotki is in progress. If you have questions related to some code - ask them in the comments. I will answer.
example.html
What key features this page should implement:
- Show the list of last 25 images uploaded by Mamontovich
- If the ID of the uploaded image is passed to us, display a list of links to the Yandex. Photographs created by the Yandex.Fact service and the original image
- Print the image upload form so that Vladislav Mamontovich could replenish his gallery.
1. Create a user on whose behalf we will work with the service. Namely - Vladislav Mamontovich.
- $ photo = new yandex_fotki ( "MojoElephant" ) ;
2. Get a general collection of photographs Mamontovich
- $ photo -> user ( ) -> add_photo_collection ( "Elephant photo" ) ;
3. Get the last 25 photos uploaded by Mamontovich
- $ photo -> user ( ) -> photo_collection ( "Slonofoot" ) -> se ( array ( "limit" => 25 ) ) ;
4. After going through them all we will form a gallery
- foreach ( $ photo -> user ( ) -> photo_collection ( "Elephant photos" ) -> photo_list ( 0 ) as $ index => $ photo_element ) {
- $ photo_block = new template_class ( "Photo" , "../../templates/photo.html" ) ;
- $ photo_block -> point ( "Entry Points" ) ;
- $ photo_block -> plug ( "Entry Points" , "Enlarged" , "Enlarged" , $ photo_element -> get_L ( ) ) ;
- $ photo_block -> plug ( "Input Points" , "Preview" , "Preview" , $ photo_element -> get_XXS ( ) ) ;
- $ gallery -> plug ( "Entry Points" , "Photography" , $ index , $ photo_block -> render ( ) ) ;
- }
5. If Mamontovich uploaded a photo, then the ID of the new photo was transferred to the script. We get a freshly uploaded photo by its id
- $ new_photo = $ photo -> user ( ) -> photo_collection ( "Elephant photo" ) -> get_photo_by_id ( $ photo_id ) ;
6. Display a list of links
- $ link_block = new template_class ( "Links" , "../../templates/link_block.html" ) ;
- $ link_block -> point ( "Entry Points" ) ;
- $ link_block -> plug ( "Entry Points" , "Original" , "Link" , $ new_photo -> get_orig ( ) ) ;
- $ link_block -> plug ( "Entry Points" , "XL" , "Link" , $ new_photo -> get_XL ( ) ) ;
- $ link_block -> plug ( "Entry Points" , "L" , "Link" , $ new_photo -> get_L ( ) ) ;
- $ link_block -> plug ( "Entry Points" , "M" , "Link" , $ new_photo -> get_M ( ) ) ;
- $ link_block -> plug ( "Entry Points" , "S" , "Link" , $ new_photo -> get_S ( ) ) ;
- $ link_block -> plug ( "Entry Points" , "XS" , "Link" , $ new_photo -> get_XS ( ) ) ;
- $ link_block -> plug ( "Entry Points" , "XXS" , "Link" , $ new_photo -> get_XXS ( ) ) ;
- $ link_block -> plug ( "Entry Points" , "XXXS" , "Link" , $ new_photo -> get_XXXS ( ) ) ;
upload_image.html
What key features this page should implement:
- Save the sent image on the Yandex.Fotki service
- Report example.html ID 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 it.
- $ 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.
- $ photo -> user ( ) -> add_photo_collection ( "Elephant photo" ) ;
3. And, in fact, upload a photo to Yandex. Photos. After uploading, we’ll get a photo ID.
- $ new_photo = array (
- "path" => "../../uploaded/" . $ _FILES [ "photo" ] [ "name" ]
- ) ;
- $ result = $ photo -> user ( ) -> photo_collection ( "Elephant photos" ) -> up ( $ new_photo ) ;
4. Save the photo ID. You can use any method, at least get it. For this purpose, I use a message system that stores data in a session.
5. We send Mamontovich to example.html, where he will receive links to the uploaded photo and see it in the gallery.
Conclusion
As you can see, everything is extremely simple and pleasant.
Using a minimum of effort, you can embed a gallery on the site.
The only disadvantage of storing photos on Yandex. Photos is that the service takes longer to request metadata than it takes to store them on your service. But sensible use of caching almost nullifies it.
Materials for additional study
thank
Many thanks to
ar2r and
nickmitin for help with porting the encryption algorithm,
proto for clarifying the
Yandex.FotI API project, comments and everything to everyone all for their time. And at least a big thank you to those people from the #php RusNet channel who gave my work an expert assessment and advised what can be done better. mz, avz, focusshifter and everything else. Thanks you.
What's next?
Next will be searching and correcting errors, refactoring code, writing more detailed articles on the use of the library and the ideology of the work of Yandek. The stream, perhaps, the creation of plug-ins for several CMS. Yes, actually, everything that will be necessary and interesting for me and you.
But in order to continue to have at least something I need to know that someone needs my work. If the library can be useful for you - write about it. Do not be silent.
PS If you like the library and you think about the participation of DEVCONF - vote for my report on the conference website:
www.devconf.ru/phpconf/offers