Appearance
<div style="display: none;" hidden="true" aria-hidden="true">Are you an LLM? You can read better optimized documentation at /guides/ImageOptimizationsIn4.6.md for this page in Markdown format</div>
Image Optimizations in EcommerceN 4.6
General Info
New helpers have been added to assist in streamlining the process of srcset generation.
Please note that in order for the client to take the full advantage of the new helpers, the new ecommercen/media-stream resizer must be enabled and the legacy resizeimage CodeIgniter helper must be swapped with mediastream_proxy in /application/core/Base_C.php. For more info on ecommercen/media-stream please refer to ecommercen/media-stream's README.md
Configuration
Srcset configuration is found within /application/config/main.php (this means if the feature ought to be back-ported to a legacy 4.0 client (pre template-system era), the srcsetProfiles will have to be included within client's /application/config/<client>.php configuration file)
The srcsetProfiles Structure
srcsetProfiles is an associative array in which each key is treated as profile name and the value contains data related to the srcset profile
The data part of the srcset profile is an associative array comprised of two parts: the variations and formats.
The variations part is an indexed numeric array in which all srcset variations of the profile are stored from smallest to largest (The order has to be maintained as the generator is designed to consider the 'main' entry as the last one within the variations). Each variation is an associative array comprised of the resize_key and media. The generator will iterate through all variations and generate different <source> tags within the <picture> element using the resize_key in the resizer & passing the media attribute to each <source> element.
The formats part is an associative array with the extension suffix that is passed to the media-stream and the mime-type which will be appended in the final <source> tag upon generation. In the case of multiple formats ALL <source> combinations will be generated in the final <picture> element.
Example configuration:
PHP
$config['srcSetProfiles'] = [
'from_w1920' => [
'variants' => [
['resize_key' => 'w320', 'media' => '(max-width: 320px)'],
['resize_key' => 'w640', 'media' => '(max-width: 640px)'],
['resize_key' => 'w1920', 'media' => '(max-width: 1920px)']
],
'formats' => [
'webp' => 'image/webp',
'jpg' => 'image/jpeg'
]
],
'from_w600' => [
'variants' => [
['resize_key' => 'w320', 'media' => '(max-width: 320px)'],
['resize_key' => 'w640', 'media' => '(max-width: 640px)'],
],
'formats' => [
'webp' => 'image/webp',
'png' => 'image/png'
]
]
];Integration in front views / presentation layer
There are two ways to apply the srcset generator into a project's view. The codeigniter helper and the vue component way
The codeigniter helper way
For the implementation of this feature from php side, two helper functions were added into CodeIginiter's html_helper helper.
html source helper
html_source($srcset = '', $index_page = FALSE, $attributes = ''): stringThis helper function will return a rendered
<source>element as string. It works similarly to CodeIgniter'simg()built-in helper (which compiles and returns a rendered html<img>element as string).Params:
$srcset: array|string: Can be string or array. When$srcsetis passed as string, it will be treated as<source>'ssrcsetattribute. Otherwise when$srcsetis passed as an associative array, the keys and values of the array will be converted to attribute keys and attribute values$index_page: boolif set totruethe generator will utilizesite_urlfor the srcset link generation. Otherwisebase_urlwill be used.$attributes: array|stringUses_stringify_attributesinternally to inject additional attributes into the element.
html picture helper
html_picture($srcData, $index_page = false, $attributes = ''): stringThis helper function will return a fully rendered
<picture>element along with<source>and<img>sub-elements within as string. It utilizes internally CodeIgniter'simg()built-in helper along with thehtml_sourcehelper referenced above.Params:
$srcData: array|string: Can be string or array. When$srcDatais passed as string, it will be treated as<img>'ssrcattribute. Otherwise when$srcDatais passed as an associative array in which the keys and values of the array will be converted to attribute keys and attribute values that will be passed within the internally generated<img>tag. Additionally, there are 3 "special" keys which, if supplied, will instruct the generator to perform a lookup in thesrcsetProfilesand generate the propersrcsetsgiven the srcset profile. These special keys are:srcset_profile: string: Which profile to use (e.gfrom_w1920)srcset_image: string: The original source image without the base url (e.gfiles/slides/myslide.jpg)srcset_resizer: string (optional): Which resizer method to use (defaults toresizeImageOnTheFly)
$index_page: bool: Proxy pass-through param forimghelper.$attributes: array|stringProxy pass-through param forimghelper.
Example Usage:
<?= html_picture(['srcset_image' => 'files/slides/cityscape.jpg', 'srcset_profile' => 'from_w1920', 'alt' => 'City Landscape']); ?>
The vue component way
Additionally, a vue component has been implemented and is pre-loaded with main/vueapp.js to assist in the generation of srcsets from vue's side. (This also means that for clients using the assets/default/vueapp.js the component must be manually loaded)
The component is called AdvHtmlPicture and it is located in /assets/vue/components. It is designed to work similarly to codeigniter's html_picture helper mentioned above.
<adv-html-picture> component accepts the following props:
Typescript
width: Number = null
height: Number = null
alt: String = ''
loading: 'lazy'|'eager' = 'lazy'
srcsetProfile: String = null
srcsetImage: StringNotes:
If the srcsetProfile prop value is missing, the first srcsetProfile entry declared in the srcsetProfiles config will be used.
Example Usage:
<adv-html-picture srcset-image="files/slides/cityscape.jpg" srcset-profile="from_w1920" alt="City Landscape" />