hollodk /
imagetastic
| 1 | <?php |
||
| 2 | |||
| 3 | namespace Imagetastic; |
||
| 4 | |||
| 5 | use Google\Auth\Credentials\ServiceAccountCredentials; |
||
| 6 | use Google\Auth\Middleware\AuthTokenMiddleware; |
||
| 7 | use GuzzleHttp\Client as HttpClient; |
||
| 8 | use GuzzleHttp\HandlerStack; |
||
| 9 | use Imagine\Filter\Basic\WebOptimization; |
||
| 10 | |||
| 11 | class Client |
||
| 12 | { |
||
| 13 | private $key; |
||
| 14 | private $project; |
||
| 15 | |||
| 16 | public function __construct($key, $project) |
||
| 17 | { |
||
| 18 | $this->key = $key; |
||
| 19 | $this->project = $project; |
||
| 20 | } |
||
| 21 | |||
| 22 | public function process($imageUrl, array $thumb) |
||
| 23 | { |
||
| 24 | try { |
||
| 25 | $res = new \StdClass(); |
||
| 26 | $res->height = null; |
||
| 27 | $res->width = null; |
||
| 28 | $res->ratio = null; |
||
| 29 | $res->done = false; |
||
| 30 | |||
| 31 | $filename = tempnam('/tmp', 'mh-'); |
||
| 32 | $thumbPath = tempnam('/tmp', 'mh-thumb-'); |
||
| 33 | |||
| 34 | $this->download($imageUrl, $filename); |
||
| 35 | |||
| 36 | $identifier = uniqid(); |
||
| 37 | |||
| 38 | $gooPath = 'original'; |
||
| 39 | $gooThumbPath = sprintf('thumb_%sx%s', $thumb['width'], $thumb['height']); |
||
| 40 | |||
| 41 | $dimentions = @getimagesize($filename); |
||
| 42 | |||
| 43 | if (!$dimentions) { |
||
| 44 | throw new \Exception('Image cannot be read properly'); |
||
| 45 | } |
||
| 46 | |||
| 47 | $res->height = $dimentions[1]; |
||
| 48 | $res->width = $dimentions[0]; |
||
| 49 | $res->mime = $dimentions['mime']; |
||
| 50 | $res->ratio = $res->width/$res->height; |
||
| 51 | |||
| 52 | switch ($res->mime) { |
||
| 53 | case 'image/jpeg': |
||
| 54 | $extension = 'jpeg'; |
||
| 55 | break; |
||
| 56 | |||
| 57 | case 'image/png': |
||
| 58 | $extension = 'png'; |
||
| 59 | break; |
||
| 60 | |||
| 61 | case 'image/gif': |
||
| 62 | $extension = 'gif'; |
||
| 63 | break; |
||
| 64 | } |
||
| 65 | |||
| 66 | $path = $identifier.'.'.$extension; |
||
|
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
Loading history...
|
|||
| 67 | $thumbPath .= '.'.$extension; |
||
| 68 | |||
| 69 | $filter = new WebOptimization(); |
||
| 70 | $imagine = new \Imagine\Gd\Imagine(); |
||
| 71 | |||
| 72 | $image = $filter->apply( |
||
| 73 | $imagine->open($filename) |
||
| 74 | ->thumbnail( |
||
| 75 | new \Imagine\Image\Box($thumb['width'], $thumb['height']), |
||
| 76 | \Imagine\Image\ImageInterface::THUMBNAIL_OUTBOUND |
||
| 77 | ) |
||
| 78 | ) |
||
| 79 | ; |
||
| 80 | |||
| 81 | switch ($res->mime) { |
||
| 82 | case 'image/jpeg': |
||
| 83 | $image->save($thumbPath, ['jpeg_quality' => 70]); |
||
| 84 | |||
| 85 | exec('jpegoptim '. $filename); |
||
| 86 | exec('jpegoptim '. $thumbPath); |
||
| 87 | |||
| 88 | break; |
||
| 89 | |||
| 90 | case 'image/png': |
||
| 91 | $image->save($thumbPath, ['png_compression_level' => 9]); |
||
| 92 | break; |
||
| 93 | |||
| 94 | case 'image/gif': |
||
| 95 | $image->save($thumbPath); |
||
| 96 | break; |
||
| 97 | |||
| 98 | default: |
||
| 99 | throw new \Exception('Quality drop for '.$res->mime.' not supported'); |
||
| 100 | } |
||
| 101 | |||
| 102 | $r = $this->upload($filename, $gooPath, $path, $res->mime); |
||
| 103 | $res->originalPath = $r->public_link; |
||
| 104 | |||
| 105 | $r = $this->upload($thumbPath, $gooThumbPath, $path, $res->mime); |
||
| 106 | $res->thumbPath = $r->public_link; |
||
| 107 | |||
| 108 | $res->done = true; |
||
| 109 | |||
| 110 | } catch (\Imagine\Exception\RuntimeException $e) { |
||
| 111 | $res->error = $e->getMessage(); |
||
| 112 | |||
| 113 | } catch (\GuzzleHttp\Exception\ServerException $e) { |
||
| 114 | $res->error = $e->getMessage(); |
||
| 115 | |||
| 116 | } catch (\Exception $e) { |
||
| 117 | $res->error = $e->getMessage(); |
||
| 118 | } |
||
| 119 | |||
| 120 | $this->cleanup([ |
||
| 121 | $filename, |
||
|
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
| 122 | $thumbPath, |
||
| 123 | ]); |
||
| 124 | |||
| 125 | return $res; |
||
| 126 | } |
||
| 127 | |||
| 128 | public function download($url, $filename) |
||
| 129 | { |
||
| 130 | if (preg_match("/http:\/\//", $url)) { |
||
| 131 | $httpClient = new HttpClient(); |
||
| 132 | $response = $httpClient->request('GET', $url); |
||
| 133 | |||
| 134 | file_put_contents($filename, $response->getBody()->getContents()); |
||
| 135 | |||
| 136 | } else { |
||
| 137 | file_put_contents($filename, file_get_contents($url)); |
||
| 138 | } |
||
| 139 | } |
||
| 140 | |||
| 141 | public function delete($object) |
||
| 142 | { |
||
| 143 | $client = $this->getClient(); |
||
| 144 | |||
| 145 | $url = sprintf('https://www.googleapis.com/storage/v1/b/%s/o/%s', |
||
| 146 | $this->project, |
||
| 147 | urlencode($object) |
||
| 148 | ); |
||
| 149 | |||
| 150 | $response = $client->request('DELETE', $url); |
||
| 151 | |||
| 152 | return json_decode($response->getBody()->getContents()); |
||
| 153 | } |
||
| 154 | |||
| 155 | public function list() |
||
| 156 | { |
||
| 157 | $client = $this->getClient(); |
||
| 158 | |||
| 159 | $url = sprintf('https://www.googleapis.com/storage/v1/b/%s/o', |
||
| 160 | $this->project |
||
| 161 | ); |
||
| 162 | |||
| 163 | $response = $client->request('GET', $url); |
||
| 164 | |||
| 165 | return json_decode($response->getBody()->getContents()); |
||
| 166 | } |
||
| 167 | |||
| 168 | public function upload($localFile, $path, $filename, $mime) |
||
| 169 | { |
||
| 170 | $client = $this->getClient(); |
||
| 171 | |||
| 172 | $destination = $path.'/'.$filename; |
||
| 173 | |||
| 174 | $gooUrl = 'https://storage.googleapis.com/'.$this->project.'/'; |
||
| 175 | $publicLink = $gooUrl.$destination; |
||
| 176 | |||
| 177 | $url = sprintf('https://www.googleapis.com/upload/storage/v1/b/%s/o?uploadType=media&name=%s', |
||
| 178 | $this->project, |
||
| 179 | urlencode($destination) |
||
| 180 | ); |
||
| 181 | |||
| 182 | $response = $client->request('POST', $url, [ |
||
| 183 | 'headers' => [ |
||
| 184 | 'Content-Type' => $mime, |
||
| 185 | 'Content-Length' => filesize($localFile), |
||
| 186 | ], |
||
| 187 | 'body' => file_get_contents($localFile) |
||
| 188 | ]); |
||
| 189 | |||
| 190 | $o = new \StdClass(); |
||
| 191 | $o->public_link = $publicLink; |
||
| 192 | $o->response = json_decode($response->getBody()->getContents()); |
||
| 193 | |||
| 194 | return $o; |
||
| 195 | } |
||
| 196 | |||
| 197 | private function cleanup($files) |
||
| 198 | { |
||
| 199 | foreach ($files as $file) { |
||
| 200 | if (file_exists($file)) { |
||
| 201 | unlink($file); |
||
| 202 | } |
||
| 203 | } |
||
| 204 | } |
||
| 205 | |||
| 206 | private function getClient() |
||
| 207 | { |
||
| 208 | $sa = new ServiceAccountCredentials( |
||
| 209 | 'https://www.googleapis.com/auth/cloud-platform', |
||
| 210 | $this->key |
||
| 211 | ); |
||
| 212 | |||
| 213 | $middleware = new AuthTokenMiddleware($sa); |
||
| 214 | $stack = HandlerStack::create(); |
||
| 215 | $stack->push($middleware); |
||
| 216 | |||
| 217 | return new HttpClient([ |
||
| 218 | 'handler' => $stack, |
||
| 219 | 'auth' => 'google_auth' |
||
| 220 | ]); |
||
| 221 | } |
||
| 222 | } |
||
| 223 |