Skip to content
This repository has been archived by the owner on Aug 12, 2023. It is now read-only.
Junaid Bhura edited this page Jun 27, 2020 · 6 revisions

The magical functions 🎩

fly_get_attachment_image_src( $attachment_id, $size, $crop )

Parameters

  • attachment_id (integer)(required) : The ID of the image attachment
  • size (string/array)(required) : Either the name of the pre-defined size defined using fly_add_image_size, or an array with the width and height. Ex: array( 500, 500 )
  • crop (boolean/array)(optional) : Whether the image should be cropped, or the crop position

Return Value

Returns an array:

array(
	'src' => string,
	'width' => integer,
	'height' => integer
)

fly_get_attachment_image( $attachment_id, $size, $crop, $attr )

Parameters

  • attachment_id (integer)(required) : The ID of the image attachment
  • size (string/array)(required) : Either the name of the pre-defined size defined using fly_add_image_size, or an array with the width and height. Ex: array( 500, 500 )
  • crop (boolean/array)(optional) : Whether the image should be cropped, or the crop position
  • attr (array)(optional) : An array of attributes. Ex: array( 'alt' => 'Alt text', 'title' => 'Title text', 'class' => 'my-class', 'id' => 'my-id' )

Return Value

Returns a HTML IMG element string:

<img src="http://yoursite.com/wp-content/uploads/fly-images/10/your-image-500x500-c.jpg" width="500" height="500" alt="Alt text" />

Examples

Example 1: Pre-defined Image Sizes

In this method, you define as many image sizes as you want in your functions.php file.

if ( function_exists( 'fly_add_image_size' ) ) {
	fly_add_image_size( 'home_page_square', 500, 500, true );
	fly_add_image_size( 'home_page_square_2x', 1000, 1000, true );
	fly_add_image_size( 'cropped_top_left', 1000, 1000, array( 'left', 'top' ) );
}

Now, lets get the post thumbnail using the image sizes we just defined:

<?php echo fly_get_attachment_image( get_post_thumbnail_id(), 'home_page_square' ); ?>

Here's another way you can do this:

<?php $image = fly_get_attachment_image_src( get_post_thumbnail_id(), 'home_page_square' ); echo '<img src="' . $image['src'] . '" width="' . $image['width'] . '" height="' . $image['height'] . '" />'; ?>

Let's get the image from our example above which has a crop position defined:

<?php echo fly_get_attachment_image( get_post_thumbnail_id(), 'cropped_top_left' ); ?>

Example 2: Dynamic Image Sizes

Lets get the post thumbnail using some dynamic image sizes:

<?php echo fly_get_attachment_image( get_post_thumbnail_id(), array( 500, 500 ), true ); ?>

Here's another way you can do this:

<?php $image = fly_get_attachment_image_src( get_post_thumbnail_id(), array( 500, 500 ), true ); echo '<img src="' . $image['src'] . '" width="' . $image['width'] . '" height="' . $image['height'] . '" />'; ?>

Lets get the post thumbnail cropped from the bottom right:

<?php echo fly_get_attachment_image( get_post_thumbnail_id(), array( 500, 500 ), array( 'right', 'bottom' ) ); ?>

A Note on Crop Positions

Crop positions are set using an array. The first parameter of the array needs to be the x-axis crop and the second parameter needs to be the y-axis crop. This feature will not work the other way around.

For example:

fly_get_attachment_image( get_post_thumbnail_id(), array( 500, 500 ), array( 'right', 'bottom' ) ) Will work! :)

fly_get_attachment_image( get_post_thumbnail_id(), array( 500, 500 ), array( 'bottom', 'right' ) ) Will not work! :(