Skip to content
This repository has been archived by the owner on Aug 12, 2023. It is now read-only.

Feature/can handle attachment #43

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions inc/class-core.php
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,28 @@ public function get_all_image_sizes() {
return $this->_image_sizes;
}

/**
* Check if we can resize and save the file.
*
* @param string $file Full path to the file.
* @return bool
*/
public function can_resize_and_save($file = '') {
if ( ! $file ) {
return false;
}

$supports = array(
'mime_type' => wp_check_filetype_and_ext( $file, basename( $file ) )['type'],
'methods' => array(
'resize',
'save',
),
);

return wp_image_editor_supports( $supports );
}

/**
* Gets a dynamically generated image URL from the Fly_Images class.
*
Expand All @@ -221,6 +243,21 @@ public function get_attachment_image_src( $attachment_id = 0, $size = '', $crop
return array();
}

// Check if given attachment is an existing image
$file = wp_get_attachment_image_url( $attachment_id );
if ( ! $file ) {
return array();
}

// Check if we can resize and save the image
if ( ! $this->can_resize_and_save( $file ) ) {
return array(
'src' => esc_url( home_url( $file ) ),
'width' => 'auto',
'height' => 'auto',
);
}

// If size is 'full', we don't need a fly image
if ( 'full' === $size ) {
return wp_get_attachment_image_src( $attachment_id, 'full' );
Expand Down Expand Up @@ -321,6 +358,17 @@ public function get_attachment_image( $attachment_id = 0, $size = '', $crop = nu
return '';
}

// Check if given attachment is an existing image
$file = wp_get_attachment_image_url( $attachment_id );
if ( ! $file ) {
return '';
}

// Check if we can resize and save the image
if ( ! $this->can_resize_and_save( $file ) ) {
return wp_get_attachment_image( $attachment_id, 'full', $attr );
}

// If size is 'full', we don't need a fly image
if ( 'full' === $size ) {
return wp_get_attachment_image( $attachment_id, $size, $attr );
Expand Down