From 30eecd1fcf32cc02d39e4dc4a67591db9bcdd632 Mon Sep 17 00:00:00 2001 From: Beshoy Girgis Date: Tue, 29 Dec 2015 02:10:43 -0600 Subject: [PATCH] Make sure thumbnails work when images are on S3 Copied from q-m/wp-json-api@ff9ffe1 -- I would have preferred to create a PR with his commit but he didn't for this repo so I had to re-create the change. This commit is necessary for us to implement an api while using an S3 plugin that pushes all images to S3. The actual files don't exist locally so without this change, we get an empty array and the only available `url` is one that's local and is not valid. --- models/attachment.php | 25 +++++++++++-------------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/models/attachment.php b/models/attachment.php index 1dd0c28..b61e450 100644 --- a/models/attachment.php +++ b/models/attachment.php @@ -42,23 +42,20 @@ function query_images() { } $this->images = array(); $home = get_bloginfo('url'); + $upload_dir = wp_upload_dir(); + $basedir = $upload_dir['basedir']; + $file = get_post_meta($this->id, '_wp_attached_file', true); + foreach ($sizes as $size) { list($url, $width, $height) = wp_get_attachment_image_src($this->id, $size); - $filename = ABSPATH . substr($url, strlen($home) + 1); - if (file_exists($filename)) { - list($measured_width, $measured_height) = getimagesize($filename); - if ($measured_width == $width && - $measured_height == $height) { - $this->images[$size] = (object) array( - 'url' => $url, - 'width' => $width, - 'height' => $height - ); - } - } + $filename = $basedir . '/' . str_replace(basename($file), basename(parse_url($url, PHP_URL_PATH)), $file); + list($measured_width, $measured_height) = getimagesize($filename); + $this->images[$size] = (object) array( + 'url' => $url, + 'width' => $width, + 'height' => $height + ); } } } - -?>