/** * 从附件中获取上传的最后一张图 * * 语法: Thumbnail_Plugin::show($this); * * @access public * @param int $obj $this * @return string $img 缩略图的地址 */ public static function show($obj) { $options = Typecho_Widget::widget('Widget_Options'); $url_plugin = $options->pluginUrl . '/Thumbnail/'; //插件地址 -> http://domain.com/usr/plugins/Thumbnail/ $url_resource = $options->resourceUrl; //资源地址 -> http://domain.com/usr/resources/ $dir_resource = "/" . str_replace($options->siteUrl, "", $url_resource); //资源路径 -> /usr/resources/ /********* 以下可以自定义 ************/ $dir_cate = $dir_resource . "cate/"; // 分类图片相对位置 -> /usr/resources/cate/ $dir_rand = $dir_resource . "rand/"; // 随机图片相对位置 -> /usr/resources/rand/ $default_img = $url_plugin . "default.png"; // 默认图片 -> http://domain.com/usr/plugins/Thumbnail/default.png $default_ext = '.png'; //默认分类图片类型 -> /usr/resources/cate/xxx.png $arr_ext = array('jpg', 'gif', 'png', 'bmp', 'jpeg'); //随机图片的后缀名 /********* 以上可以自定义 ************/ $cid = $obj->cid; $cate = $obj->category; $content = $obj->text; $img = $default_img; if (strpos($content, "<!--NoThumbnail-->") === false) { $db = Typecho_Db::get(); $attach = $db->fetchAll($db->select()->from('table.contents')->where('type = ? AND parent = ? ', 'attachment', $cid)); if (empty($attach)) { //没有附件的时候 从文章中匹配 /**↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓**/ preg_match_all("/\\<img.*?src\\=(\\'|\")(.*?)(\\'|\")[^>]*>/i", $content, $matches); $imgCount = count($matches[0]); if ($imgCount >= 1) { $img = $matches[2][0]; } else { if (file_exists($_SERVER['DOCUMENT_ROOT'] . $dir_cate . $cate . $default_ext)) { $img = $dir_cate . $cate . $default_ext; } else { $img = Thumbnail_Plugin::randPic($dir_rand, $default_img, $arr_ext); //随机图片 } } /**↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑**/ /***** 1、只从附件中读取,请将以上↓↑之间代码注释即可。 ******/ /***** 2、不存在附件时读分类图片,请将以上↓↑之间代码注释,并打开以下代码 if( file_exists( $_SERVER['DOCUMENT_ROOT'] . $dir_cate . $cate . $default_ext ) ){ $img = $dir_cate . $cate . $default_ext ; } *****/ } else { //直接从附件中找出第一个上传的图片 foreach ($attach as $attach) { $attach_text = unserialize($attach['text']); if (Thumbnail_Plugin::isImage($attach_text['type'], $arr_ext)) { $img = $attach_text['path']; break; } } } $img = $img; } else { $img = ""; } echo $img; }