Does thumb() create a new image every time?

I know there are a lot of questions on here about creating and using thumbnails. I looked through a bunch of them and I don’t think this one was asked.

On the thumb documentation page, it says “Creates a thumbnail for a given image object” But then below it says it can also return a $thumb or URL string. Does that mean that if the thumbnail with given parameters doesn’t exist, it’ll create it, else it’ll find and return the thumb image?

I ask because I noticed sometimes it creates new thumbnails and I wasn’t totally sure if I was using it right so i don’t want my code to make tons of random new thumbnails when I don’t need them.

A thumb of a given size, quality etc. for an image is only created once. If it already exists, it is not re-created, unless you delete the thumbs folder or rename the image.

What that means is the thumbs method returns either a string (the URL of the thumb) or an object, depending on whether the third parameter is set to true or false:

Example 1: Return an object

<?php
if($image = $page->images()->first()) {
  dump (thumb($image,  ['width' => 200], true)); // true is the default, so you don't have to set this, this is just to make it clearer
}

Result of dump:

<pre>Asset Object
(
    [root] =&gt; /Users/sonja/htdocs/langkit/thumbs/projects/project-a/closeup-200x267.jpg
    [url] =&gt; http://localhost/langkit/thumbs/projects/project-a/closeup-200x267.jpg
    [hash] =&gt; a389fcc57e7c32f582e2d751518db80b
    [dir] =&gt; /Users/sonja/htdocs/langkit/thumbs/projects/project-a
    [filename] =&gt; closeup-200x267.jpg
    [name] =&gt; closeup-200x267
    [safeName] =&gt; closeup-200x267.jpg
    [extension] =&gt; jpg
    [size] =&gt; 16576
    [niceSize] =&gt; 16.19 kB
    [modified] =&gt; 2018-10-21T09:12:35+00:00
    [mime] =&gt; image/jpeg
    [type] =&gt; image
    [dimensions] =&gt; Dimensions Object
        (
            [width] =&gt; 200
            [height] =&gt; 267
            [ratio] =&gt; 0.74906367041199
            [orientation] =&gt; portrait
        )

    [isWritable] =&gt; 1
    [isReadable] =&gt; 1
    [isExecutable] =&gt; 
    [header] =&gt; Content-type: image/jpeg
    [exif] =&gt; Exif Object
        (
            [camera] =&gt; Exif\Camera Object
                (
                    [make] =&gt; 
                    [model] =&gt; 
                )

            [location] =&gt; Exif\Location Object
                (
                    [lat] =&gt; 
                    [lng] =&gt; 
                )

            [timestamp] =&gt; 1540113155
            [exposure] =&gt; 
            [aperture] =&gt; 
            [iso] =&gt; 
            [focalLength] =&gt; 
            [isColor] =&gt; 1
        )

)

</pre>

Example 2: third parameter set to false

<?php
if($image = $page->images()->first()) {
  dump (thumb($image,  ['width' => 200], false)); 
}

Result of dump:

<pre>http://localhost/langkit/thumbs/projects/project-a/closeup-200x267.jpg</pre>

Fantastic, that clarifies it. Thank you so much!