├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── composer.json ├── composer.lock ├── fixtures └── finder.png ├── lib └── AvatarsIo │ ├── Avatar.php │ ├── Client.php │ └── Exception.php └── test ├── AvatarsIo └── Tests │ └── AvatarTest.php └── bootstrap.php /.gitignore: -------------------------------------------------------------------------------- 1 | /composer.phar 2 | /vendor 3 | phpunit.xml -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: php 2 | 3 | script: phpunit 4 | 5 | before_script: 6 | - wget http://getcomposer.org/composer.phar 7 | - php composer.phar install 8 | 9 | php: 10 | - 5.3 11 | - 5.4 -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License 2 | 3 | Copyright (c) 2010 Joffrey Jaffeux 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | AVATARS IO PHP 2 | ============== 3 | 4 | THIS IS A VERY EARLY STAGE WRAPPER FOR AVATARS.IO. However, the API will remain stable you can use it and nothing will break in the future. 5 | 6 | The current version is only a fast code to learn the avatars.io API, however it works, coming next : 7 | - tests 8 | - injectable httpclient 9 | - more features (better handling of url generation for example) 10 | - refactoring 11 | 12 | I have no time to work on this at the moment, ping me or open issue if you need this. 13 | 14 | 15 | [![Build Status](https://secure.travis-ci.org/jjaffeux/avatars-io-php.png?branch=master)](http://travis-ci.org/jjaffeux/avatars-io-php) 16 | 17 | 18 | Setup 19 | ----- 20 | composer.json : 21 | ``` json 22 | { 23 | "require": { 24 | "jjaffeux/avatars-io-php": ">=1.0.0" 25 | } 26 | } 27 | ``` 28 | ``` php 29 | $avatar = new \AvatarsIo\Avatar(CLIENT ID, SECRET KEY); 30 | ``` 31 | 32 | General Usage 33 | ------------- 34 | 35 | ``` php 36 | $avatar->upload('filepath', 'identifier'); //identifier is optionnal 37 | $avatar->url('twitter', 'twitter username', 'size') //size is optionnal, can be small, medium, large 38 | ``` 39 | 40 | 41 | Bug tracker 42 | ----------- 43 | 44 | Have a bug? Please create an issue here on GitHub! 45 | 46 | 47 | Contributions 48 | ------------- 49 | 50 | * Fork 51 | * Write tests (phpunit in the directory to run the tests) 52 | * Write Code 53 | * Pull request 54 | 55 | Thanks for your help. 56 | 57 | 58 | Authors 59 | ------- 60 | 61 | **Joffrey Jaffeux** 62 | 63 | + http://twitter.com/joffreyjaffeux 64 | + http://github.com/jjaffeux 65 | 66 | License 67 | --------------------- 68 | 69 | MIT License -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "jjaffeux/avatars-io-php", 3 | "type": "library", 4 | "description": "AvatarIo api wrapper", 5 | "version": "1.0.0", 6 | "homepage": "https://github.com/jjaffeux/avatars-io-php", 7 | "keywords": ["avatars io", "avatars", "api", "facebook", "twitter", "instagram"], 8 | "license": "MIT", 9 | "authors": [ 10 | { 11 | "name": "Joffrey Jaffeux", 12 | "email": "j.jaffeux@gmail.com", 13 | "homepage": "http://www.twitter.com/joffreyjaffeux" 14 | } 15 | ], 16 | "require": { 17 | "php": ">=5.2", 18 | "kriswallsmith/buzz": "v0.7" 19 | }, 20 | "autoload": { 21 | "psr-0": { 22 | "AvatarsIo": "lib/" 23 | } 24 | } 25 | } -------------------------------------------------------------------------------- /composer.lock: -------------------------------------------------------------------------------- 1 | { 2 | "hash": "89f645edd1ab5a2de7d1618f90222004", 3 | "packages": [ 4 | { 5 | "package": "kriswallsmith/buzz", 6 | "version": "v0.7" 7 | } 8 | ], 9 | "packages-dev": null, 10 | "aliases": [ 11 | 12 | ], 13 | "minimum-stability": "dev", 14 | "stability-flags": [ 15 | 16 | ] 17 | } 18 | -------------------------------------------------------------------------------- /fixtures/finder.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jjaffeux/avatars-io-php/0b9c610ca89dcd4fdc0a2769ee1b53424c8de979/fixtures/finder.png -------------------------------------------------------------------------------- /lib/AvatarsIo/Avatar.php: -------------------------------------------------------------------------------- 1 | client_id = $client_id; 23 | $this->access_token = $access_token; 24 | $this->client = new Client(); 25 | } 26 | 27 | function upload($file, $identifier = '') 28 | { 29 | if ( ! file_exists($file) OR ! is_readable($file)) { 30 | throw new Cant_access_file(); 31 | } 32 | 33 | $response = $this->send_file($file, $identifier); 34 | 35 | if(empty($response->data->upload_info)) { 36 | return $response->data->url; 37 | } 38 | 39 | $this->set_aws_s3_acl($response->data->upload_info, $file); 40 | return $this->get_file_url($response->data->id); 41 | } 42 | 43 | function send_file($file, $identifier) 44 | { 45 | $content = array( 46 | 'data' => array( 47 | 'filename' => $file, 48 | 'md5' => md5_file($file), 49 | 'size' => filesize($file), 50 | 'path' => $identifier 51 | ) 52 | ); 53 | 54 | $this->response = $this->client->post( 55 | $content, 56 | array( 57 | 'Content-Type: application/json; charset=utf-8', 58 | 'x-client_id: ' . $this->client_id, 59 | 'Authorization: OAuth ' . $this->access_token 60 | ), 61 | 'token' 62 | ); 63 | } 64 | 65 | function set_aws_s3_acl($upload_info, $file) 66 | { 67 | $this->client->put( 68 | $upload_info, 69 | array( 70 | 'Authorization: ' . $upload_info->signature, 71 | 'Date: ' . $upload_info->date, 72 | 'Content-Type: ' . $upload_info->content_type, 73 | 'x-amz-acl: public-read' 74 | ), 75 | $file 76 | ); 77 | } 78 | 79 | function get_file_url($image_id) 80 | { 81 | $response = $this->client->post( 82 | '', 83 | array( 84 | 'x-client_id: ' . $this->client_id, 85 | 'Authorization: OAuth ' . $this->access_token 86 | ), 87 | 'token/' . $image_id . '/complete' 88 | ); 89 | 90 | return $response->data; 91 | } 92 | 93 | function url($service = 'twitter', $key, $size = 'default') 94 | { 95 | return self::base_uri . '/' . $service . '/' . $key . '?size=' . $size; 96 | } 97 | 98 | } -------------------------------------------------------------------------------- /lib/AvatarsIo/Client.php: -------------------------------------------------------------------------------- 1 | browser = new \Buzz\Browser(); 15 | } 16 | 17 | function post($content, $headers, $endpoint) 18 | { 19 | $request = $this->browser->post( 20 | Avatar::base_uri . '/'. self::version . '/' . $endpoint, 21 | $headers, 22 | json_encode($content) 23 | ); 24 | 25 | return $this->response($request); 26 | } 27 | 28 | function put($content, $headers, $file) 29 | { 30 | $request = $this->browser->put( 31 | $content->upload_url, 32 | $headers, 33 | file_get_contents($file) 34 | ); 35 | 36 | return $this->response($request); 37 | } 38 | 39 | private function response($request) 40 | { 41 | return json_decode($request->getContent()); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /lib/AvatarsIo/Exception.php: -------------------------------------------------------------------------------- 1 | avatar = new \AvatarsIo\Avatar('xxxxx', 'xxxxx'); 8 | } 9 | 10 | public function testUrl() 11 | { 12 | $url = $this->avatar->url('twitter', 'username'); 13 | $this->assertEquals('http://avatars.io/twitter/username?size=default', $url); 14 | } 15 | 16 | public function testUploadAvatar() 17 | { 18 | $this->markTestIncomplete( 19 | 'Tests are not isolated at the moment you must provide valid credentials.' 20 | ); 21 | $this->assertNotNull($this->avatar->upload('./fixtures/finder.png')); 22 | } 23 | 24 | public function testUploadAvatarWithIdentifier() 25 | { 26 | $this->markTestIncomplete( 27 | 'Tests are not isolated at the moment you must provide valid credentials.' 28 | ); 29 | $this->assertNotNull($this->avatar->upload('./fixtures/finder.png', 'finder')); 30 | } 31 | 32 | } -------------------------------------------------------------------------------- /test/bootstrap.php: -------------------------------------------------------------------------------- 1 |