Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public function fetchImage($image_url, $folder='') {
- $error = '';
- $info = '';
- $filename = '';
- $image_url = trim($image_url);
- if (strstr($image_url, 'catalog/') == 0 && file_exists(DIR_IMAGE . $folder . $image_url)) {
- $error = sprintf("[%s] - %s", $this->language->get('log_level_info'), $image_url . ' is not a URL, using local file instead.');
- $filename = $image_url;
- }
- if (strpos($image_url, 'http') !== 0) {
- $error = sprintf("[%s] - %s", $this->language->get('log_level_info'), $image_url . ' is not a valid URL, skipping image.');
- }
- if (empty($error)) {
- if($folder != '') {
- $new_folder = DIR_IMAGE . 'catalog/' . $folder;
- if (!file_exists($new_folder)) {
- mkdir($new_folder, 0777, true);
- }
- }
- if (strstr($image_url, '?')) {
- if($folder != '' && $folder != '/') {
- $filename = 'catalog/' . $folder . '/' . md5($image_url) . '.jpg';
- } else {
- $filename = 'catalog/' . md5($image_url) . '.jpg';
- }
- } else {
- $url_parts = explode('/', $image_url);
- // Decode html space for image filename
- $end = str_replace('%20', ' ', end($url_parts));
- if($folder != '' && $folder != '/') {
- $filename = 'catalog/' . $folder . '/' . $end;
- } else {
- $filename = 'catalog/' . $end;
- }
- }
- if (pathinfo($filename, PATHINFO_EXTENSION) == "")
- $filename = $filename.'.jpg';
- if (!file_exists(DIR_IMAGE . $filename)) {
- $fp = fopen(DIR_IMAGE . $filename, 'w');
- $ch = curl_init();
- $ports = array();
- // Encode spaces in url
- $image_url = str_replace(' ', '%20', $image_url);
- if (preg_match('/:(\d+)/', $image_url, $ports)) {
- $image_url = preg_replace('/:\d+/', '', $image_url);
- curl_setopt($ch, CURLOPT_PORT, (int)$ports[1]);
- }
- curl_setopt($ch, CURLOPT_URL, $image_url);
- curl_setopt($ch, CURLOPT_FILE, $fp);
- if (ini_get('open_basedir') == '') {
- curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
- curl_setopt($ch, CURLOPT_MAXREDIRS, 2);
- }
- curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
- curl_exec($ch);
- $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
- $info = curl_getinfo($ch);
- curl_close($ch);
- fclose($fp);
- $file_info = "";
- if (file_exists(DIR_IMAGE . $filename) && filesize(DIR_IMAGE . $filename) > 0)
- $file_info = getimagesize(DIR_IMAGE . $filename);
- if($httpCode == 404 || empty($file_info) || (isset($file_info['mime']) && strpos($file_info['mime'], 'image/') !== 0)) {
- unlink(DIR_IMAGE . $filename);
- $filename = '';
- if (isset($file_info['mime']) && strpos($file_info['mime'], 'image/') !== 0) {
- $error = sprintf("[%s] - %s", $this->language->get('log_level_warning'), $image_url . " was not an image.");
- }
- if($httpCode == 404) {
- $error = sprintf("[%s] - %s", $this->language->get('log_level_warning'), $image_url . " not found. (404)");
- }
- if(empty($file_info)) {
- $error = sprintf("[%s] - %s", $this->language->get('log_level_warning'), $image_url . " was empty.");
- }
- }
- }
- }
- return array('filename' => $filename, 'error' => $error, 'info' => $info);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement