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
] )SummaryConstructor. For a more fluid syntax useImage::create()
.ParametersidThe image idsizeThe image size -
__toString
__toString( )
SummaryMake it easy to echo it at any time.Returnsstring
-
alt
alt( )
SummaryGet the alt text.Returnsnull | string
-
create
create( int
$id
[, string$size
] )SummaryFor a more fluid syntax.ParametersidThe image idsizeThe image sizeReturns$this
-
getAttribute
getAttribute( string
$attribute
)SummaryGets an attribute.ParametersattributeThe attribute to getReturnsstring | null
-
height
height( )
SummaryGet the height of the image in the current size.Returnsnull | string
-
metadata
metadata( )
SummaryGets the metadata.Returnsarray | mixed
-
removeAttribute
removeAttribute( string
$name
)SummaryRemove an attribute.ParametersnameThe attribute nameReturns$this
-
setAttribute
setAttribute( string
$name
, string$value
)SummaryAdd an attribute.ParametersnameThe attribute namevalueThe value. Null to just output the name.Returns$this
-
title
title( )
SummaryGets the title field. This is not the filename.Returnsnull | string
-
url
url( )
SummaryGets the url of the image in the current size.Returnsnull | string
-
width
width( )
SummaryGets the width of the image in the current size.Returnsnull | string
-
withCaption
withCaption( [ bool
$bool
] )SummarySets the flag to output with the caption.ParametersboolWhether to output with the caption or not.Returns$this