function download($url, $store_dir) { $filename = get_image_filename($url, $store_dir); if (file_exists($filename)) { return; } //存在时不下载 $curl = new Curl\Curl(); $curl->setHeader('X-Requested-With', 'XMLHttpRequest'); $curl->setUserAgent('Mozilla/5.0 (Macintosh; Intel Mac OS X 10.11; rv:40.0) Gecko/20100101 Firefox/40.0'); $curl->setHeader('Accept-Language', 'zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3'); $curl->setHeader('Accept', 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'); @mkdir($store_dir, 0755, true); $curl->get($url); $data = $curl->response; file_put_contents($filename, $data); try { $image = new \Eventviva\ImageResize($filename); $image->resizeToWidth(JS_IMG_WIDTH); $image->save($filename); } catch (Exception $e) { } return $filename; }
function output_icon($icon) { $time = filemtime(get_image_filename($icon)); if (isset($_SERVER['HTTP_IF_NONE_MATCH']) && $time == $_SERVER['HTTP_IF_NONE_MATCH'] || $time <= if_modified_since()) { header('HTTP/1.1 304 Not Modified'); header('Etag: ' . $time); header('Cache-Control: public, max-age=' . FAVICON_CACHE_TTL); header('Expires: ' . gmdate("D, d M Y H:i:s", UNIX_TIME + FAVICON_CACHE_TTL) . ' GMT'); exit; } output_image($icon, $time); }
function get_image_by_template($template = '', $before = '', $after = '') { if ($template == '' || $template == 'default') { $template = '<img width="%IMAGE_WIDTH%" height="%IMAGE_HEIGHT%" id="%IMAGE_ID%" src="%IMAGE_URL%" alt="%IMAGE_FILENAME%" />'; } $template = $before . $template . $after; $patterns = array('IMAGE_ID' => get_image_id(), 'IMAGE_REAL_ID' => get_image_real_id(), 'IMAGE_FILENAME' => get_image_filename(), 'IMAGE_URL' => get_image_url(), 'IMAGE_VIEWER' => get_image_viewer(), 'IMAGE_SHORTURL' => get_image_shorturl(), 'IMAGE_WIDTH' => get_image_width(), 'IMAGE_HEIGHT' => get_image_height(), 'IMAGE_SIZE' => get_image_size(), 'IMAGE_SIZE_BYTES' => get_image_size_bytes(), 'IMAGE_THUMB_WIDTH' => get_image_thumb_width(), 'IMAGE_THUMB_HEIGHT' => get_image_thumb_height(), 'IMAGE_THUMB_URL' => get_image_thumb_url()); return preg_replace('/%([a-z_]+)%/ie', '$patterns["$1"]', $template); }
function fix_images($filedirectory) { global $vbulletin; $contenttypeid = vB_Types::instance()->getContentTypeId("vBCms_Article"); $set = $vbulletin->db->query_read(" SELECT cms_node.nodeid, cms_article.* FROM " . TABLE_PREFIX . "cms_article AS cms_article JOIN " . TABLE_PREFIX . "cms_node AS cms_node ON (cms_article.contentid = cms_node.contentid AND cms_node.contenttypeid = $contenttypeid) "); while ($row = $vbulletin->db->fetch_array($set)) { $attachment_map = array(); $pagetext = $row['pagetext']; //get attachments and replace with new ids $matches = array(); if (preg_match_all("#\\[ATTACH=CONFIG\\](\\d+)\\[/ATTACH\\]#i", $row['pagetext'], $matches)) { foreach($matches[1] AS $attachmentid) { if (!array_key_exists($attachmentid, $attachment_map)) { $file_name = get_image_filename($filedirectory, $attachmentid); $attachment_map[$attachmentid] = attach_image($file_name, $filedirectory, $row['nodeid']); } } if (count($attachment_map)) { $orig = array(); $replacement = array(); foreach($attachment_map AS $oldid => $newid) { $orig[] = "[ATTACH=CONFIG]" . $oldid . "[/ATTACH]"; $replacement[] = "[ATTACH=CONFIG]" . $newid . "[/ATTACH]"; } if (count($orig)) { $pagetext = str_replace($orig, $replacement, $pagetext); } $vbulletin->db->query_write(" UPDATE " . TABLE_PREFIX . "cms_article SET pagetext = '" . $vbulletin->db->escape_string($pagetext) . "' WHERE contentid = $row[contentid] "); } } //find and replace attachments added as IMG tags. Otherwise they'll look for the live site, which is bad. $matches = array(); if (preg_match_all("#\\[IMG\\][^]]*attachmentid=(\\d+)[^]]*\\[/IMG\\]#i", $row['pagetext'], $matches)) { $orig = array(); $replacement = array(); foreach($matches[1] AS $key => $attachmentid) { if (!array_key_exists($attachmentid, $attachment_map)) { $file_name = get_image_filename($filedirectory, $attachmentid); $attachment_map[$attachmentid] = attach_image($file_name, $filedirectory, $row['nodeid']); } if ($attachment_map[$attachmentid]) { $orig[] = $matches[0][$key]; $replacement[] = "[ATTACH]" . $attachment_map[$attachmentid] . "[/ATTACH]"; } } if (count($orig)) { $pagetext = str_replace($orig, $replacement, $pagetext); $vbulletin->db->query_write(" UPDATE " . TABLE_PREFIX . "cms_article SET pagetext = '" . $vbulletin->db->escape_string($pagetext) . "' WHERE contentid = $row[contentid] "); } } //handle preview images $matches = array(); if (preg_match("#attachmentid=(\\d+)&#", $row['previewimage'], $matches)) { $attachmentid = $matches[1]; if ($attachmentid) { if (!array_key_exists($attachmentid, $attachment_map)) { $file_name = get_image_filename($filedirectory, $attachmentid); $attachment_map[$attachmentid] = attach_image($file_name, $filedirectory, $row['nodeid']); } $newid = $attachment_map[$attachmentid]; if ($newid) { $record = $vbulletin->db->query_first($q = " SELECT thumbnail_width, thumbnail_height FROM " . TABLE_PREFIX . "attachment AS attach INNER JOIN " . TABLE_PREFIX . "filedata AS data ON data.filedataid = attach.filedataid WHERE attachmentid = $newid" ); $vbulletin->db->query_write($q = " UPDATE " . TABLE_PREFIX . "cms_article SET previewimage = 'attachment.php?attachmentid=$newid&cid=$contenttypeid', imagewidth = $record[thumbnail_width], imageheight = $record[thumbnail_height] WHERE contentid = $row[contentid] "); } else { echo "<p>Could not find attachmentid $attachmentid</p>"; } } else { var_dump($row['contentid'], $row['previewimage']); } } } }