<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: threehymns</title>
    <description>The latest articles on DEV Community by threehymns (@threehymns).</description>
    <link>https://dev.to/threehymns</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1167824%2F20846320-5006-496a-93b4-ac1d0e03d298.jpeg</url>
      <title>DEV Community: threehymns</title>
      <link>https://dev.to/threehymns</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/threehymns"/>
    <language>en</language>
    <item>
      <title>Optimize a Folder of Images with Astro 3.0</title>
      <dc:creator>threehymns</dc:creator>
      <pubDate>Sun, 24 Sep 2023 02:12:57 +0000</pubDate>
      <link>https://dev.to/threehymns/optimize-a-folder-of-images-with-astro-30-2l0i</link>
      <guid>https://dev.to/threehymns/optimize-a-folder-of-images-with-astro-30-2l0i</guid>
      <description>&lt;p&gt;&lt;a href="https://astro.build"&gt;Astro&lt;/a&gt; 3.0 was just released and it comes with a great image compression feature. The savings can be up to 99.1% less data based on testing I did with compressing &lt;code&gt;.jpg&lt;/code&gt; images directly from a camera. However, all the examples only show how to compress and import one image at a time. In this blog post, I'll show you how to use Astro's built-in &lt;code&gt;Image&lt;/code&gt; component to optimize all images in a folder with just a few lines of code. If you haven't done so already, &lt;a href="https://docs.astro.build/en/install/auto/"&gt;create an Astro project&lt;/a&gt; and start a new component. &lt;/p&gt;

&lt;p&gt;First, you need to import the &lt;code&gt;Image&lt;/code&gt; component from &lt;code&gt;astro:assets&lt;/code&gt;. This component will automatically compress and lazy load your images for the best quality and speed.&lt;/p&gt;

&lt;p&gt;Next, you need to use &lt;a href="https://docs.astro.build/en/reference/api-reference/#astroglob"&gt;&lt;code&gt;Astro.glob&lt;/code&gt;&lt;/a&gt; to import all your image files at once. This is a handy method that returns an array of files matching a glob pattern. You can use any valid glob pattern to filter the files by extension, name, or subfolder. For example, &lt;code&gt;"../assets/images/*.{jpg,JPG,jpeg,png,PNG,webp}"&lt;/code&gt; will get all the &lt;code&gt;.jpg&lt;/code&gt;, &lt;code&gt;.png&lt;/code&gt; and &lt;code&gt;.webp&lt;/code&gt; files from the &lt;code&gt;../assets/images/&lt;/code&gt; folder.&lt;/p&gt;

&lt;p&gt;Finally, the &lt;code&gt;images.map&lt;/code&gt; function loops over the array of files and renders each one to its own &lt;code&gt;Image&lt;/code&gt; component.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="o"&gt;---&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Image&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;astro:assets&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;images&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;Astro&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;glob&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;../assets/images/*.{jpg,JPG,jpeg,png,PNG,webp}&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;then&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;files&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;files&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;map&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;file&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;file&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;default&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Map each file to its default export (the src).&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="o"&gt;---&lt;/span&gt;

&lt;span class="p"&gt;{&lt;/span&gt;
 &lt;span class="nx"&gt;images&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;map&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;img&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Image&lt;/span&gt;
      &lt;span class="nx"&gt;src&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;img&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
      &lt;span class="nx"&gt;width&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;1200&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
      &lt;span class="nx"&gt;height&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;750&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
    &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;
  &lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's it! Now you have optimized all your images with Astro 3.0 and improved your website performance.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>astro</category>
      <category>ui</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
