/** * Fetch an image with wp_remote_get(), save it to $fullpath with a unique name. * Will return an empty string if something went wrong. * * @param $url string * @param $fullpath string * * @return string filename */ protected function fetchAndSaveUniqueImage($url, $fullpath) { if (isset($this->fetchedImageCache[$url])) { return $this->fetchedImageCache[$url]; } $response = wp_remote_get($url, array('timeout' => $this->timeout)); // WordPress error? if (is_wp_error($response)) { try { // protocol relative urls handed to wp_remote_get will fail // try adding a protocol $protocol_relative = wp_parse_url($url); if (!isset($protocol_relative['scheme'])) { if (true === is_ssl()) { $url = 'https:' . $url; } else { $url = 'http:' . $url; } } $response = wp_remote_get($url, array('timeout' => $this->timeout)); if (is_wp_error($response)) { throw new \Exception('Bad URL: ' . $url); } } catch (\Exception $exc) { $this->fetchedImageCache[$url] = ''; error_log('\\PressBooks\\Export\\Epub201\\fetchAndSaveUniqueImage wp_error on wp_remote_get() - ' . $response->get_error_message() . ' - ' . $exc->getMessage()); return ''; } } // Basename without query string $filename = explode('?', basename($url)); // isolate latex image service from WP, add file extension if ('s.wordpress.com' == parse_url($url, PHP_URL_HOST) && 'latex.php' == $filename[0]) { $filename = md5(array_pop($filename)); // content-type = 'image/png' $type = explode('/', $response['headers']['content-type']); $type = array_pop($type); $filename = $filename . '.' . $type; } else { $filename = array_shift($filename); $filename = sanitize_file_name(urldecode($filename)); $filename = Sanitize\force_ascii($filename); } $tmp_file = \Pressbooks\Utility\create_tmp_file(); file_put_contents($tmp_file, wp_remote_retrieve_body($response)); if (!\Pressbooks\Image\is_valid_image($tmp_file, $filename)) { $this->fetchedImageCache[$url] = ''; error_log('\\PressBooks\\Export\\Epub201\\fetchAndSaveUniqueImage is_valid_image, not a valid image '); return ''; // Not an image } if ($this->compressImages) { $format = explode('.', $filename); $format = strtolower(end($format)); // Extension \Pressbooks\Image\resize_down($format, $tmp_file); } // Check for duplicates, save accordingly if (!file_exists("{$fullpath}/{$filename}")) { copy($tmp_file, "{$fullpath}/{$filename}"); } elseif (md5(file_get_contents($tmp_file)) != md5(file_get_contents("{$fullpath}/{$filename}"))) { $filename = wp_unique_filename($fullpath, $filename); copy($tmp_file, "{$fullpath}/{$filename}"); } $this->fetchedImageCache[$url] = $filename; return $filename; }
/** * Fetch an image with wp_remote_get(), save it to $fullpath with a unique name. * Will return an empty string if something went wrong. * * @param $url string * @param $fullpath string * * @return string filename */ protected function fetchAndSaveUniqueImage($url, $fullpath) { // Cheap cache static $already_done = array(); if (isset($already_done[$url])) { return $already_done[$url]; } $response = wp_remote_get($url, array('timeout' => $this->timeout)); // WordPress error? if (is_wp_error($response)) { // TODO: handle $response->get_error_message(); $already_done[$url] = ''; return ''; } // Basename without query string $filename = explode('?', basename($url)); // isolate latex image service from WP, add file extension if ('s.wordpress.com' == parse_url($url, PHP_URL_HOST) && 'latex.php' == $filename[0]) { $filename = md5(array_pop($filename)); // content-type = 'image/png' $type = explode('/', $response['headers']['content-type']); $type = array_pop($type); $filename = $filename . '.' . $type; } else { $filename = array_shift($filename); $filename = sanitize_file_name(urldecode($filename)); $filename = \Pressbooks\Sanitize\force_ascii($filename); } $tmp_file = \Pressbooks\Utility\create_tmp_file(); file_put_contents($tmp_file, wp_remote_retrieve_body($response)); if (!\Pressbooks\Image\is_valid_image($tmp_file, $filename)) { $already_done[$url] = ''; return ''; // Not an image } if ($this->compressImages) { $format = explode('.', $filename); $format = strtolower(end($format)); // Extension \Pressbooks\Image\resize_down($format, $tmp_file); } // Check for duplicates, save accordingly if (!file_exists("{$fullpath}/{$filename}")) { copy($tmp_file, "{$fullpath}/{$filename}"); } elseif (md5(file_get_contents($tmp_file)) != md5(file_get_contents("{$fullpath}/{$filename}"))) { $filename = wp_unique_filename($fullpath, $filename); copy($tmp_file, "{$fullpath}/{$filename}"); } $already_done[$url] = $filename; return $filename; }