|
| 1 | +--- |
| 2 | +description: Configure Fastly Image Optimizer. |
| 3 | +--- |
| 4 | + |
| 5 | +# Fastly Image Optimizer (Fastly IO) |
| 6 | + |
| 7 | +The Fastly Image Optimizer (Fastly IO) is an external service that provides real-time image optimisation for multiple input and output formats. |
| 8 | +It serves and caches image requests from your origin server, making your website faster and more efficient. |
| 9 | + |
| 10 | +To be able to configure this feature, you need [Fastly IO subscription](https://docs.fastly.com/en/guides/about-fastly-image-optimizer). |
| 11 | + |
| 12 | +## Enable shielding |
| 13 | + |
| 14 | +To use Fastly Image Optimizer, you need shielding. |
| 15 | +To enable it, follow steps in Fastly documentation, [Enabling and disabling shielding](https://developer.fastly.com/learning/concepts/shielding/). |
| 16 | + |
| 17 | +Before proceeding, make sure that you have the [`snippet_re_enable_shielding.vcl`](https://github.com/ibexa/fastly/blob/main/fastly/snippet_re_enable_shielding.vcl) configuration file added to your project. |
| 18 | + |
| 19 | +You can add it using Fastly CLI: |
| 20 | + |
| 21 | +```bash |
| 22 | +- fastly vcl snippet create --name="Re-Enable shielding on restart" --version=active --autoclone --priority 100 --type recv --content=vendor/ibexa/fastly/fastly/snippet_re_enable_shielding.vcl |
| 23 | +- fastly service-version activate --version=latest |
| 24 | +``` |
| 25 | + |
| 26 | +Next, you need to choose the [Shield location](https://developer.fastly.com/learning/concepts/shielding/#choosing-a-shield-location) from the Shielding menu in Fastly web interface as specified in [Fastly IO documentation](https://docs.fastly.com/en/guides/shielding#enabling-shielding). |
| 27 | + |
| 28 | +## VCL configuration |
| 29 | + |
| 30 | +To manipulate your Fastly VCL configuration directly from the command line, |
| 31 | +you need to: |
| 32 | + |
| 33 | +- [install Fastly CLI](https://developer.fastly.com/learning/tools/cli#installing), |
| 34 | +- define `FASTLY_SERVICE_ID` and `FASTLY_KEY` environmental variables, |
| 35 | +- set restrictions on the optimiser by using [`ibexa_image_optimizer.vcl`](https://github.com/ibexa/fastly/blob/main/fastly/ibexa_image_optimizer.vcl). |
| 36 | + |
| 37 | +This is an example VCL snippet uploaded by using the `vcl_recv` hook: |
| 38 | + |
| 39 | +```bash |
| 40 | +fastly vcl custom create --name="Ibexa VCL" --main --version=latest --autoclone --content=vendor/ibexa/fastly/fastly/ez_main.vcl |
| 41 | +fastly vcl snippet create --name="Shielding" --version=active --autoclone --type recv --content=vendor/ibexa/fastly/fastly/snippet_re_enable_shielding.vcl |
| 42 | +``` |
| 43 | + |
| 44 | +Fastly passes requests through the image optimizer by adding the `x-fastly-imageopto-api` header in `vcl_recv`. |
| 45 | +You need to restrict the optimizer by file path and extension to only apply to image requests: |
| 46 | + |
| 47 | +```vcl |
| 48 | +if (req.url.ext ~ "(?i)^(gif|png|jpe?g|webp)$") { |
| 49 | + if (req.url.path ~ "^/var/([a-zA-Z0-9_-]+)/storage/images") { |
| 50 | + set req.http.x-fastly-imageopto-api = "fastly"; |
| 51 | + } |
| 52 | +} |
| 53 | +``` |
| 54 | + |
| 55 | +You can define what image formats will be included, for example: `gif|png|jpe?g|webp` |
| 56 | +and which paths should be used as a source of images, for example: `^/var/([a-zA-Z0-9_-]+)/storage/images`. |
| 57 | +For more configuration options, see [Enabling image optimization](https://developer.fastly.com/reference/io/#enabling-image-optimization). |
| 58 | + |
| 59 | +After you save this snippet or create your own, you can upload it from the command line using: |
| 60 | + |
| 61 | +```bash |
| 62 | +fastly vcl snippet create --name="Shielding" --version=active --autoclone --type recv --content=vendor/ibexa/fastly/fastly/snippet_re_enable_shielding.vcl |
| 63 | +``` |
| 64 | + |
| 65 | +## Define SiteAccess for Fastly IO |
| 66 | + |
| 67 | +Fastly IO configuration is SiteAccess aware. |
| 68 | +You can define what handler should be used for a specific SiteAccess under `variation_handler_identifier`. |
| 69 | +You need to set it up as `fastly`, so Fastly IO can generate all image links. |
| 70 | +By default, it is set as `alias`, and it points to a built-in image optimizer. |
| 71 | +You can also set up a custom handler if your setup requires it. |
| 72 | + |
| 73 | +```yaml |
| 74 | +ibexa: |
| 75 | + system: |
| 76 | + my_siteaccess: |
| 77 | + variation_handler_identifier: 'fastly' |
| 78 | +``` |
| 79 | +
|
| 80 | +You can also use environmental variables to configure a specific handler for a SiteAccess. |
| 81 | +See the example below to configure it with the `.env` file: |
| 82 | + |
| 83 | +``` |
| 84 | +IBEXA_VARIATION_HANDLER_IDENTIFIER="fastly" |
| 85 | +``` |
| 86 | +
|
| 87 | +## Image configuration |
| 88 | +
|
| 89 | +When you define image variation keys for Fastly IO, keep in mind |
| 90 | +that they should reflect variations in your original setup. |
| 91 | +The built-in image optimizer serves as backup to Fastly IO in case of misconfiguration, |
| 92 | +so it needs to be able to serve the same image variations. |
| 93 | +
|
| 94 | +Fastly IO image filters are not compatible with our built-in filters, |
| 95 | +so you will not be able to reflect your original filters accurately with Fastly. |
| 96 | +The script below will help you find replacement filters within Fastly configuration for the basic filters. |
| 97 | +For more optimization options on Fastly side, see [Fastly IO reference](https://developer.fastly.com/reference/io/). |
| 98 | +
|
| 99 | +To generate your original image configuration run: |
| 100 | +
|
| 101 | +```bash |
| 102 | +php bin/console ibexa:fastly:migrate-configuration |
| 103 | +``` |
| 104 | + |
| 105 | +Paste the configuration to `config/packages/ibexa.yaml` to define the same variations for Fastly IO: |
| 106 | + |
| 107 | +```yaml |
| 108 | +ibexa: |
| 109 | + system: |
| 110 | + default: |
| 111 | + fastly_variations: |
| 112 | + reference: |
| 113 | + reference: original |
| 114 | + configuration: |
| 115 | + width: 600 |
| 116 | + height: 600 |
| 117 | + fit: bounds |
| 118 | + small: |
| 119 | + reference: reference |
| 120 | + configuration: |
| 121 | + width: 100 |
| 122 | + height: 100 |
| 123 | + fit: bounds |
| 124 | + tiny: |
| 125 | + reference: reference |
| 126 | + configuration: |
| 127 | + width: 30 |
| 128 | + height: 30 |
| 129 | + fit: bounds |
| 130 | + medium: |
| 131 | + reference: reference |
| 132 | + configuration: |
| 133 | + width: 200 |
| 134 | + height: 200 |
| 135 | + fit: bounds |
| 136 | + large: |
| 137 | + reference: reference |
| 138 | + configuration: |
| 139 | + width: 300 |
| 140 | + height: 300 |
| 141 | + fit: bounds |
| 142 | + gallery: |
| 143 | + reference: original |
| 144 | + configuration: { } |
| 145 | + ezplatform_admin_ui_profile_picture_user_menu: |
| 146 | + reference: reference |
| 147 | + configuration: |
| 148 | + width: 30 |
| 149 | + height: 30 |
| 150 | + fit: bounds |
| 151 | + crop: '30,30,x0,y0' |
| 152 | +``` |
| 153 | +
|
| 154 | +You can select defined image variations during Content item creation in the image options. |
| 155 | +Variations can include different sizing options and other filters that are applied to the image. |
| 156 | +
|
| 157 | + |
0 commit comments