Image

Images made easy.

Usage

    
            use WPDev\Models\Image;

            echo Image::create(80, 'large');
            /* Outputs something like...
            <img src="https://site.com/wp-content/uploads/2018/02/sample-image-260x300.jpg"
                 width="260"
                 height="300"
                 alt="This is the alt text"
                 srcset="https://site.com/wp-content/uploads/2018/02/sample-image-260x300.jpg 260w, https://site.com/wp-content/uploads/2018/02/sample-image-768x887.jpg 768w, https://site.com/wp-content/uploads/2018/02/sample-image-887x1024.jpg 887w, https://site.com/wp-content/uploads/2018/02/sample-image.jpg 1143w"
                 sizes="(max-width: 260px) 100vw, 260px"
                 title="This is the title"
            >
            */

            echo Image::create(80, 'large')
            ->setAttribute('class', 'img-responsive someclass')
            ->setAttribute('data-cool', 'pretty much')
            ->setAttribute('data-no-val')
            ->withCaption();
            /* Only if there actually is a caption it outputs something like...
            <figure>
            <img src="https://site.com/wp-content/uploads/2018/02/sample-image-260x300.jpg"
                     width="260"
                     height="300"
                     alt="This is the alt text"
                     srcset="https://site.com/wp-content/uploads/2018/02/sample-image-260x300.jpg 260w, https://site.com/wp-content/uploads/2018/02/sample-image-768x887.jpg 768w, https://site.com/wp-content/uploads/2018/02/sample-image-887x1024.jpg 887w, https://site.com/wp-content/uploads/2018/02/sample-image.jpg 1143w"
                     sizes="(max-width: 260px) 100vw, 260px"
                     title="This is the title"
                     class="img-responsive someclass"
                     data-cool="pretty much"
                     data-no-val
                >
            <figcaption>This is the caption</figcaption>
            </figure>
            */

            // You don't have to use it to output an image tag.
            $image = Image::create(80, 'large');
            $image_url = $image->url();
            $image_caption = $image->caption();
            $image_metadata = $image->metadata();
    

API Reference

Public Methods
Method Summary
__construct Constructor. For a more fluid syntax use Image::create().
__toString Make it easy to echo it at any time.
alt Get the alt text.
caption Get the caption.
create
static
For a more fluid syntax.
getAttribute Gets an attribute.
height Get the height of the image in the current size.
metadata Gets the metadata.
removeAttribute Remove an attribute.
setAttribute Add an attribute.
title Gets the title field. This is not the filename.
url Gets the url of the image in the current size.
width Gets the width of the image in the current size.
withCaption Sets the flag to output with the caption.
  • __construct

    __construct( $id [, string $size ] )

    Summary
    Constructor. For a more fluid syntax use Image::create().
    Parameters
    id
    The image id
    size
    The image size
  • __toString

    __toString( )

    Summary
    Make it easy to echo it at any time.
    Returns

    string

  • alt

    alt( )

    Summary
    Get the alt text.
    Returns

    null | string

  • caption

    caption( )

    Summary
    Get the caption.
    Returns

    bool | string

  • create

    create( int $id [, string $size ] )

    Summary
    For a more fluid syntax.
    Parameters
    id
    The image id
    size
    The image size
    Returns

    $this

  • getAttribute

    getAttribute( string $attribute )

    Summary
    Gets an attribute.
    Parameters
    attribute
    The attribute to get
    Returns

    string | null

  • height

    height( )

    Summary
    Get the height of the image in the current size.
    Returns

    null | string

  • metadata

    metadata( )

    Summary
    Gets the metadata.
    Returns

    array | mixed

  • removeAttribute

    removeAttribute( string $name )

    Summary
    Remove an attribute.
    Parameters
    name
    The attribute name
    Returns

    $this

  • setAttribute

    setAttribute( string $name, string $value )

    Summary
    Add an attribute.
    Parameters
    name
    The attribute name
    value
    The value. Null to just output the name.
    Returns

    $this

  • title

    title( )

    Summary
    Gets the title field. This is not the filename.
    Returns

    null | string

  • url

    url( )

    Summary
    Gets the url of the image in the current size.
    Returns

    null | string

  • width

    width( )

    Summary
    Gets the width of the image in the current size.
    Returns

    null | string

  • withCaption

    withCaption( [ bool $bool ] )

    Summary
    Sets the flag to output with the caption.
    Parameters
    bool
    Whether to output with the caption or not.
    Returns

    $this