/** * downloads an external image to the server and stores it on the server * * @param string $file the URL to the original file * @return string the URL to the downloaded file */ public static function sideload_image($file) { $loc = self::get_sideloaded_file_loc($file); if (file_exists($loc)) { return TimberURLHelper::preslashit(TimberURLHelper::get_rel_path($loc)); } // Download file to temp location if (!function_exists('download_url')) { require_once ABSPATH . '/wp-admin/includes/file.php'; } $tmp = download_url($file); preg_match('/[^\\?]+\\.(jpe?g|jpe|gif|png)\\b/i', $file, $matches); $file_array = array(); $file_array['name'] = basename($matches[0]); $file_array['tmp_name'] = $tmp; // If error storing temporarily, unlink if (is_wp_error($tmp)) { @unlink($file_array['tmp_name']); $file_array['tmp_name'] = ''; } // do the validation and storage stuff $locinfo = pathinfo($loc); $file = wp_upload_bits($locinfo['basename'], null, file_get_contents($file_array['tmp_name'])); return $file['url']; }
function testInitFromURL() { $destination_path = self::copyTestImage(); $destination_path = TimberURLHelper::get_rel_path($destination_path); $destination_url = 'http://' . $_SERVER['HTTP_HOST'] . $destination_path; $image = new TimberImage($destination_url); $this->assertEquals($destination_url, $image->get_src()); $this->assertEquals($destination_url, (string) $image); }
function testImageWidthWithFilter() { $pid = $this->factory->post->create(); $photo = $this->copyTestImage(); $photo = TimberURLHelper::get_rel_path($photo); update_post_meta($pid, 'custom_photo', '/' . $photo); $str = '{{TimberImage(post.custom_photo).width}}'; $post = new TimberPost($pid); $rendered = Timber::compile_string($str, array('post' => $post)); $this->assertEquals(1500, $rendered); }
/** * @deprecated */ static function get_rel_path($src) { return TimberURLHelper::get_rel_path($src); }
/** * @api * @example * ```twig * <img src="{{ image.path }}" /> * ``` * ```html * <img src="/wp-content/uploads/2015/08/pic.jpg" /> * ``` * @return string the /relative/path/to/the/file */ public function path() { return TimberURLHelper::get_rel_path($this->src()); }