|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace ImageOptim; |
| 4 | + |
| 5 | +class Request { |
| 6 | + const BASE_URL = 'https://im2.io'; |
| 7 | + |
| 8 | + private $username, $url; |
| 9 | + private $width, $height, $dpr, $fit, $quality, $timeout; |
| 10 | + |
| 11 | + function __construct($username, $url) { |
| 12 | + if (!$username) throw new InvalidArgumentException(); |
| 13 | + if (!$url) { |
| 14 | + throw new InvalidArgumentException("Image URL is required"); |
| 15 | + } |
| 16 | + if (!preg_match('/^https?:\/\//', $url)) { |
| 17 | + throw new InvalidArgumentException("The API requires absolute image URL (starting with http:// or https://). Got: $url"); |
| 18 | + } |
| 19 | + $this->username = $username; |
| 20 | + $this->url = $url; |
| 21 | + } |
| 22 | + |
| 23 | + public function resize($width, $height_or_fit = null, $fit = null) { |
| 24 | + if (!is_numeric($width)) { |
| 25 | + throw new InvalidArgumentException("Width is not a number: $width"); |
| 26 | + } |
| 27 | + |
| 28 | + $width = intval($width); |
| 29 | + if (null === $height_or_fit) { |
| 30 | + $height = null; |
| 31 | + } else if (is_numeric($height_or_fit)) { |
| 32 | + $height = intval($height_or_fit); |
| 33 | + } else if ($fit) { |
| 34 | + throw new InvalidArgumentException("Height is not a number: $height_or_fit"); |
| 35 | + } else { |
| 36 | + $fit = $height_or_fit; |
| 37 | + $height = null; |
| 38 | + } |
| 39 | + |
| 40 | + if ($width < 1 || $width > 10000) { |
| 41 | + throw new InvalidArgumentException("Width is out of allowed range: $width"); |
| 42 | + } |
| 43 | + if ($height !== null && ($height < 1 || $height > 10000)) { |
| 44 | + throw new InvalidArgumentException("Height is out of allowed range: $height"); |
| 45 | + } |
| 46 | + |
| 47 | + $allowedFitOptions = ['fit', 'crop', 'scale-down']; |
| 48 | + if (null !== $fit && !in_array($fit, $allowedFitOptions)) { |
| 49 | + throw new InvalidArgumentException("Fit is not one of ".implode(', ',$allowedFitOptions).". Got: $fit"); |
| 50 | + } |
| 51 | + |
| 52 | + $this->width = $width; |
| 53 | + $this->height = $height; |
| 54 | + $this->fit = $fit; |
| 55 | + |
| 56 | + return $this; |
| 57 | + } |
| 58 | + |
| 59 | + public function timeout($timeout) { |
| 60 | + if (!is_numeric($timeout) || $timeout <= 0) { |
| 61 | + throw new InvalidArgumentException("Timeout not a positive number: $timeout"); |
| 62 | + } |
| 63 | + $this->timeout = $timeout; |
| 64 | + |
| 65 | + return $this; |
| 66 | + } |
| 67 | + |
| 68 | + public function dpr($dpr) { |
| 69 | + if (!preg_match('/^\d[.\d]*(x)?$/', $dpr, $m)) { |
| 70 | + throw new InvalidArgumentException("DPR should be 1x, 2x or 3x. Got: $dpr"); |
| 71 | + } |
| 72 | + $this->dpr = $dpr . (empty($m[1]) ? 'x' : ''); |
| 73 | + |
| 74 | + return $this; |
| 75 | + } |
| 76 | + |
| 77 | + public function quality($quality) { |
| 78 | + $allowedQualityOptions = ['low', 'medium', 'high', 'lossless']; |
| 79 | + if (!in_array($quality, $allowedQualityOptions)) { |
| 80 | + throw new InvalidArgumentException("Quality is not one of ".implode(', ',$allowedQualityOptions).". Got: $quality"); |
| 81 | + } |
| 82 | + $this->quality = $quality; |
| 83 | + |
| 84 | + return $this; |
| 85 | + } |
| 86 | + |
| 87 | + function optimize() { |
| 88 | + // always. This is here to make order of calls flexible |
| 89 | + return $this; |
| 90 | + } |
| 91 | + |
| 92 | + function apiURL() { |
| 93 | + $options = []; |
| 94 | + if ($this->width) { |
| 95 | + $size = $this->width; |
| 96 | + if ($this->height) { |
| 97 | + $size .= 'x' . $this->height; |
| 98 | + } |
| 99 | + $options[] = $size; |
| 100 | + if ($this->fit) $options[] = $this->fit; |
| 101 | + } else { |
| 102 | + $options[] = 'full'; |
| 103 | + } |
| 104 | + if ($this->dpr) $options[] = $this->dpr; |
| 105 | + if ($this->quality) $options[] = 'quality=' . $this->quality; |
| 106 | + if ($this->timeout) $options[] = 'timeout=' . $this->timeout; |
| 107 | + |
| 108 | + $imageURL = $this->url; |
| 109 | + if (preg_match('/[\s%+]/', $imageURL)) { |
| 110 | + $imageURL = rawurlencode($imageURL); |
| 111 | + } |
| 112 | + |
| 113 | + return self::BASE_URL . '/' . rawurlencode($this->username) . '/' . implode(',', $options) . '/' . $imageURL; |
| 114 | + } |
| 115 | + |
| 116 | + function getBytes() { |
| 117 | + $url = $this->apiURL(); |
| 118 | + $stream = @fopen($url, 'r', false, stream_context_create([ |
| 119 | + 'http' => [ |
| 120 | + 'ignore_errors' => true, |
| 121 | + 'method' => 'POST', |
| 122 | + 'header' => "User-Agent: ImageOptim-php/1.0 PHP/" . phpversion(), |
| 123 | + 'timeout' => max(30, $this->timeout), |
| 124 | + ], |
| 125 | + ])); |
| 126 | + |
| 127 | + if (!$stream) { |
| 128 | + $err = error_get_last(); |
| 129 | + throw new NetworkException("Can't send HTTPS request to: $url\n" . ($err ? $err['message'] : '')); |
| 130 | + } |
| 131 | + |
| 132 | + $res = @stream_get_contents($stream); |
| 133 | + if (!$res) { |
| 134 | + $err = error_get_last(); |
| 135 | + fclose($stream); |
| 136 | + throw new NetworkException("Error reading HTTPS response from: $url\n" . ($err ? $err['message'] : '')); |
| 137 | + } |
| 138 | + |
| 139 | + $meta = @stream_get_meta_data($stream); |
| 140 | + if (!$meta) { |
| 141 | + $err = error_get_last(); |
| 142 | + fclose($stream); |
| 143 | + throw new NetworkException("Error reading HTTPS response from: $url\n" . ($err ? $err['message'] : '')); |
| 144 | + } |
| 145 | + fclose($stream); |
| 146 | + |
| 147 | + if (!$meta || !isset($meta['wrapper_data'], $meta['wrapper_data'][0])) { |
| 148 | + throw new NetworkException("Unable to read headers from HTTP request to: $url"); |
| 149 | + } |
| 150 | + if (!empty($meta['timed_out'])) { |
| 151 | + throw new NetworkException("Request timed out: $url", 504); |
| 152 | + } |
| 153 | + |
| 154 | + if (!preg_match('/HTTP\/[\d.]+ (\d+) (.*)/', $meta['wrapper_data'][0], $status)) { |
| 155 | + throw new NetworkException("Unexpected response: ". $meta['wrapper_data'][0]); |
| 156 | + } |
| 157 | + |
| 158 | + $code = intval($status[1]); |
| 159 | + if ($code >= 500) { |
| 160 | + throw new APIException($status[2], $code); |
| 161 | + } |
| 162 | + if ($code == 404) { |
| 163 | + throw new NotFoundException("Could not find the image: {$this->url}", $code); |
| 164 | + } |
| 165 | + if ($code == 403) { |
| 166 | + throw new AccessDeniedException("API username was not accepted: {$this->username}", $code); |
| 167 | + } |
| 168 | + if ($code >= 400) { |
| 169 | + throw new InvalidArgumentException($status[2], $code); |
| 170 | + } |
| 171 | + |
| 172 | + return $res; |
| 173 | + } |
| 174 | +} |
0 commit comments