/** * @param string $src * @param int $w * @param int $h * @param string $crop * @return string */ static function get_retina_file_path($src, $mult = 2) { $new_name = self::get_retina_file_name_relative_to_content($src, $mult); $new_server_path = WP_CONTENT_DIR . $new_name; $new_server_path = TimberURLHelper::remove_double_slashes($new_server_path); return $new_server_path; }
/** * * * @param string $url * @param bool $force * @return string */ public static function get_rel_url($url, $force = false) { $url_info = parse_url($url); if (isset($url_info['host']) && $url_info['host'] != self::get_host() && !$force) { return $url; } $link = ''; if (isset($url_info['path'])) { $link = $url_info['path']; } if (isset($url_info['query']) && strlen($url_info['query'])) { $link .= '?' . $url_info['query']; } if (isset($url_info['fragment']) && strlen($url_info['fragment'])) { $link .= '#' . $url_info['fragment']; } $link = TimberURLHelper::remove_double_slashes($link); return $link; }
/** * @deprecated */ static function remove_double_slashes($url) { return TimberURLHelper::remove_double_slashes($url); }
function testResizeGif() { $filename = self::copyTestImage('loading.gif'); $gif_url = str_replace(ABSPATH, 'http://' . $_SERVER['HTTP_HOST'] . '/', $filename); $str = '<img src="{{' . "'{$gif_url}'" . '|resize(200)}}" />'; $result = Timber::compile_string($str); $resized_url = str_replace('loading.gif', 'loading-200x0-c-default.gif', $gif_url); $resized_path = str_replace('http://example.org', ABSPATH, $resized_url); $resized_path = TimberURLHelper::remove_double_slashes($resized_path); $this->assertFileExists($resized_path); }
/** * * * @param string $src * @param int $w * @param int $h * @param string $crop * @param bool $force_resize * @return string */ public static function resize($src, $w, $h = 0, $crop = 'default', $force_resize = false) { if (empty($src)) { return ''; } if (strstr($src, 'http') && !strstr($src, home_url())) { $src = self::sideload_image($src); } $abs = false; if (strstr($src, 'http')) { $abs = true; } // Sanitize crop position $allowed_crop_positions = array('default', 'center', 'top', 'bottom', 'left', 'right'); if ($crop !== false && !in_array($crop, $allowed_crop_positions)) { $crop = $allowed_crop_positions[0]; } //oh good, it's a relative image in the uploads folder! $new_path = self::get_resize_file_rel($src, $w, $h, $crop); $new_server_path = self::get_resize_file_path($src, $w, $h, $crop); $old_server_path = self::get_server_location($src); $old_server_path = TimberURLHelper::remove_double_slashes($old_server_path); $new_server_path = TimberURLHelper::remove_double_slashes($new_server_path); if (file_exists($new_server_path)) { if ($force_resize) { // Force resize - warning: will regenerate the image on every pageload, use for testing purposes only! unlink($new_server_path); } else { if (!$abs) { return TimberURLHelper::preslashit($new_path); } return untrailingslashit(home_url()) . $new_path; } } $image = wp_get_image_editor($old_server_path); if (!is_wp_error($image)) { $current_size = $image->get_size(); $src_w = $current_size['width']; $src_h = $current_size['height']; $src_ratio = $src_w / $src_h; if (!$h) { $h = round($w / $src_ratio); } // Get ratios $dest_ratio = $w / $h; $src_wt = $src_h * $dest_ratio; $src_ht = $src_w / $dest_ratio; if (!$crop) { // Always crop, to allow resizing upwards $image->crop(0, 0, $src_w, $src_h, $w, $h); } else { //start with defaults: $src_x = $src_w / 2 - $src_wt / 2; $src_y = ($src_h - $src_ht) / 6; //now specific overrides based on options: if ($crop == 'center') { // Get source x and y $src_x = round(($src_w - $src_wt) / 2); $src_y = round(($src_h - $src_ht) / 2); } else { if ($crop == 'top') { $src_y = 0; } else { if ($crop == 'bottom') { $src_y = $src_h - $src_ht; } else { if ($crop == 'left') { $src_x = 0; } else { if ($crop == 'right') { $src_x = $src_w - $src_wt; } } } } } // Crop the image if ($dest_ratio > $src_ratio) { $image->crop(0, $src_y, $src_w, $src_ht, $w, $h); } else { $image->crop($src_x, 0, $src_wt, $src_h, $w, $h); } } $result = $image->save($new_server_path); if (is_wp_error($result)) { error_log('Error resizing image'); error_log(print_r($result, true)); } if ($abs) { return untrailingslashit(home_url()) . $new_path; } return $new_path; } else { if (isset($image->error_data['error_loading_image'])) { TimberHelper::error_log('Error loading ' . $image->error_data['error_loading_image']); } else { TimberHelper::error_log($image); } } return $src; }