Example #1
0
 /**
  * Returns a URL that serves an image.
  *
  * @param string $gs_filename The name of the Google Cloud Storage object to
  * serve. In the format gs://bucket_name/object_name
  *
  * @param mixed[] $options Array of additional options for serving the object.
  * Valid options are:
  * <ul>
  * <li>'crop': boolean Whether the image should be cropped.  If set to true, a
  *   size must also be supplied. Default value: false.
  * <li>'secure_url': boolean Whether to request an https URL. Default value:
  *   false.
  * <li>'size': integer The size of the longest dimension of the resulting
  * image. Size must be in the range 0 to 1600, with 0 specifying the size of
  * the original image. The aspect ratio is preserved unless 'crop' is
  * specified.
  * </ul>
  * @return string The image serving URL.
  *
  * @throws \InvalidArgumentException if any of the arguments are not valid.
  * @throws CloudStorageException If there was a problem contacting the
  * service.
  */
 public static function getImageServingUrl($gs_filename, $options = [])
 {
     $blob_key = self::createGsKey($gs_filename);
     if (!is_array($options)) {
         throw new \InvalidArgumentException('$options must be an array. ' . 'Actual type: ' . gettype($options));
     }
     $extra_options = array_diff(array_keys($options), array_keys(self::$get_image_serving_url_default_options));
     if (!empty($extra_options)) {
         throw new \InvalidArgumentException('Invalid options supplied: ' . htmlspecialchars(implode(',', $extra_options)));
     }
     $options = array_merge(self::$get_image_serving_url_default_options, $options);
     # Validate options.
     if (!is_bool($options['crop'])) {
         throw new \InvalidArgumentException('$options[\'crop\'] must be a boolean. ' . 'Actual type: ' . gettype($options['crop']));
     }
     if ($options['crop'] && is_null($options['size'])) {
         throw new \InvalidArgumentException('$options[\'size\'] must be set because $options[\'crop\'] is true.');
     }
     if (!is_null($options['size'])) {
         $size = $options['size'];
         if (!is_int($size)) {
             throw new \InvalidArgumentException('$options[\'size\'] must be an integer. ' . 'Actual type: ' . gettype($size));
         }
         if ($size < 0 || $size > self::MAX_IMAGE_SERVING_SIZE) {
             throw new \InvalidArgumentException('$options[\'size\'] must be >= 0 and <= ' . self::MAX_IMAGE_SERVING_SIZE . '. Actual value: ' . $size);
         }
     }
     if (!is_bool($options['secure_url'])) {
         throw new \InvalidArgumentException('$options[\'secure_url\'] must be a boolean. ' . 'Actual type: ' . gettype($options['secure_url']));
     }
     $req = new ImagesGetUrlBaseRequest();
     $resp = new ImagesGetUrlBaseResponse();
     $req->setBlobKey($blob_key);
     $req->setCreateSecureUrl($options['secure_url']);
     try {
         ApiProxy::makeSyncCall('images', 'GetUrlBase', $req, $resp);
     } catch (ApplicationError $e) {
         throw self::imagesApplicationErrorToException($e);
     }
     $url = $resp->getUrl();
     if (!is_null($options['size'])) {
         $url .= '=s' . $options['size'];
         if ($options['crop']) {
             $url .= '-c';
         }
     }
     return $url;
 }
 private function expectImageGetUrlBase($blob_key, $secure, $url)
 {
     $req = new ImagesGetUrlBaseRequest();
     $resp = new ImagesGetUrlBaseResponse();
     $req->setBlobKey($blob_key);
     $req->setCreateSecureUrl($secure);
     $resp->setUrl($url);
     $this->apiProxyMock->expectCall('images', 'GetUrlBase', $req, $resp);
 }