Skip to content

Commit

Permalink
New function added for returning a single image URL
Browse files Browse the repository at this point in the history
  • Loading branch information
A5hleyRich committed Apr 19, 2017
1 parent e05815b commit bfbc9d4
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 22 deletions.
80 changes: 58 additions & 22 deletions includes/class-image-processing-queue.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,39 @@ public function filter_update_post_metadata( $check, $object_id, $meta_key, $met
return $check;
}

/**
* Check if the image sizes exist and push them to the queue if not.
*
* @param int $post_id
* @param array $sizes
*/
protected function process_image( $post_id, $sizes ) {
$new_item = false;

foreach ( $sizes as $size ) {
if ( self::does_size_already_exist_for_image( $post_id, $size ) ) {
continue;
}

if ( self::is_size_larger_than_original( $post_id, $size ) ) {
continue;
}

$item = array(
'post_id' => $post_id,
'width' => $size[0],
'height' => $size[1],
'crop' => $size[2],
);
$this->process->push_to_queue( $item );
$new_item = true;
}

if ( $new_item ) {
$this->process->save()->dispatch();
}
}

/**
* Get image HTML for a specific context in a theme, specifying the exact sizes
* for the image. The first image size is always used as the `src` and the other
Expand All @@ -132,32 +165,34 @@ public function filter_update_post_metadata( $check, $object_id, $meta_key, $met
* @return string HTML img element or empty string on failure.
*/
public function get_image( $post_id, $sizes, $attr = '' ) {
// If we haven't created the image yet, add it to the queue.
$new_item = false;
foreach ( $sizes as $size ) {
if ( self::does_size_already_exist_for_image( $post_id, $size ) ) {
continue;
}
$this->process_image( $post_id, $sizes );

if ( self::is_size_larger_than_original( $post_id, $size ) ) {
continue;
}
return wp_get_attachment_image( $post_id, array( $sizes[0][0], $sizes[0][1] ), false, $attr );
}

$item = array(
'post_id' => $post_id,
'width' => $size[0],
'height' => $size[1],
'crop' => $size[2],
);
$this->process->push_to_queue( $item );
$new_item = true;
}
/**
* Get image URL for a specific context in a theme, specifying the exact size
* for the image. If the image size does not currently exist, it is queued for
* creation by a background process. Example:
*
* echo ipq_get_theme_image_url( 1353, array( 600, 400, false ) );
*
* @param int $post_id
* @param array $size
*
* @return string
*/
public function get_image_url( $post_id, $size ) {
$this->process_image( $post_id, array( $size ) );

if ( $new_item ) {
$this->process->save()->dispatch();
$size = self::get_size_name( $size );
$src = wp_get_attachment_image_src( $post_id, $size );

if ( isset( $src[0] ) ) {
return $src[0];
}

return wp_get_attachment_image( $post_id, array( $sizes[0][0], $sizes[0][1] ), false, $attr );
return '';
}

/**
Expand Down Expand Up @@ -211,7 +246,8 @@ public static function update_image_meta( $post_id, $data ) {
*/
public static function does_size_already_exist_for_image( $post_id, $size ) {
$image_meta = self::get_image_meta( $post_id );
$size_name = self::get_size_name( $size );
$size_name = self::get_size_name( $size );

return isset( $image_meta['sizes'][ $size_name ] );
}

Expand Down
17 changes: 17 additions & 0 deletions includes/ipq-template-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,25 @@
* @param int $post_id Image attachment ID.
* @param array $sizes Array of arrays of sizes in the format array(width,height,crop).
* @param string $attr Optional. Attributes for the image markup. Default empty.
*
* @return string HTML img element or empty string on failure.
*/
function ipq_get_theme_image( $post_id, $sizes, $attr = '' ) {
return Image_Processing_Queue::instance()->get_image( $post_id, $sizes, $attr );
}

/**
* Get image URL for a specific context in a theme, specifying the exact size
* for the image. If the image size does not currently exist, it is queued for
* creation by a background process. Example:
*
* echo ipq_get_theme_image_url( 1353, array( 600, 400, false ) );
*
* @param int $post_id
* @param array $size
*
* @return string Img URL
*/
function ipq_get_theme_image_url( $post_id, $size ) {
return Image_Processing_Queue::instance()->get_image_url( $post_id, $size );
}

0 comments on commit bfbc9d4

Please sign in to comment.