/** * @deprecated */ static function preslashit($path) { return TimberURLHelper::preslashit($path); }
/** * 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']; }
/** * * * @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; }