emlog图片调用方法汇总
[ 2015/03/17, Emlog , 2870阅, 0评 ]

1.获取附件中的第一张图片

<?php
//获取附件的第一张图片
function getpic_attfirst($blogid) {
    $db = MySql::getInstance();
    $sql = "SELECT * FROM ".DB_PREFIX."attachment WHERE blogid=".$blogid." AND (`filepath` LIKE '%jpg' OR `filepath` LIKE '%gif' OR `filepath` LIKE '%png') ORDER BY `aid` ASC LIMIT 0,1";
	$imgs = $db->query($sql);
    while($row = $db->fetch_array($imgs)){
		$img.= ''.BLOG_URL.substr($row['filepath'],3,strlen($row['filepath'])).'';
	}
    echo $img;
}
?>

2.获取文章内容中的第一张图片

a.匹配文章内容中的所有图片链接,然后取第一个

<?php
//获取文章内容中的第一张图片
function getpic_contentfirst($content){
	$content = html_entity_decode($content);
    //preg_match_all("|<img[^>]+src=\"([^>\"]+)\"?[^>]*>|is", $content, $match);
	//preg_match_all("/\<img.*?src\=\"(.*?)\"[^>]*>/i", $content, $match);
	preg_match_all("/<img.*src=[\"](.*?)[\"].*?>/i", $content, $match);
	if(!empty($match[1])){
		echo $match[1][0];
	}else{
		echo TEMPLATE_URL.'images/rand/'.rand(1,10).'.jpg';
	}
}
?>

b.匹配到第一个图片链接后不再匹配

<?php
//获取文章内容中的第一张图片
function getpic_contentfirst($content){
	$content = html_entity_decode($content);
	$imgpp = preg_match('/<img.*src=[\"](.*?)[\"].*?>/i', $content, $match);
	if($imgpp === 1){
		echo $match[1];
	}else{
		echo TEMPLATE_URL.'images/rand/'.rand(1,10).'.jpg';
	}
}
?>

3.获取文章内容中的多张图片

<?php 
if(preg_match_all("/<img.*src=[\"'](.*)[\"']/Ui", $value['content'], $imgs) && !empty($imgs[1])){
	$imgNum = count($imgs[1]);
	if($imgNum < 4){
		$n = 1;
	}elseif($imgNum < 8){
		$n = 4;
	}else{
		$n = 8;
	}
	for($i=0;$i < $n;$i++){
		$img = $imgs[1][$i];
		echo "<img src=\"{$img}\">";
	}
}else{
	echo "<img src='图片地址'>";
}
?>

当正文中少于4张图片时候只调用1张图片;当少于8张图片时,调出4张;当大于8张图片时,调出8张图片。

4.先附件的第一张图片,再文章内容中的第一张图片,最后再是随机图片(推荐)

<?php
//获取文章缩略图
function get_thum($logid){
	$db = MySql::getInstance();
	//先查询附件中的第一张图片
	$sql = "SELECT * FROM ".DB_PREFIX."attachment WHERE blogid=".$logid." AND (`filepath` LIKE '%jpg' OR `filepath` LIKE '%gif' OR `filepath` LIKE '%png') ORDER BY `aid` ASC LIMIT 0,1";
	$img = $db->query($sql);
	while($row = $db->fetch_array($img)){
		//组合缩略图路径
		$thum_url_a = substr($row['filepath'],3,26);//将类似../content/uploadfile/201507/853b1437274481.jpg截取为content/uploadfile/201507/
		$thum_url_b = substr($row['filepath'],29,strlen($row['filepath']));//将类似../content/uploadfile/201507/853b1437274481.jpg截取为853b1437274481.jpg
		$thum_url_b = str_replace(''.$thum_url_b.'', 'thum-'.$thum_url_b.'', $thum_url_b);//将上一步的853b1437274481.jpg变为thum-853b1437274481.jpg
		$thum_url_c = BLOG_URL.$thum_url_a.$thum_url_b;//最后再将之组合为缩略图的实际地址
		//判断缩略图是否存在,否则取原图
		if(@fopen($thum_url_c, 'r')){
			$thum_url = $thum_url_c;
		}else{
			$thum_url = BLOG_URL.substr($row['filepath'],3,strlen($row['filepath']));
		}
	}
	//再查询文章内容的第一张图片
	if(empty($thum_url)){
		$sql2 = "SELECT content FROM ".DB_PREFIX."blog WHERE gid=".$logid."";
		$result = $db->query($sql2);
		while($row = $db->fetch_array($result)){
			$content = $row['content'];
		}
		$content = html_entity_decode($content);
		$imgpp = preg_match('/<img.*src=[\"](.*?)[\"].*?>/i', $content, $match);
		if($imgpp === 1){
			$thum_url = $match[1];
		}else{
			$aa = true;
		}
	}
	//最后使用随机图片
	if($aa){
		srand((double)microtime()*1000000);
		$randval = rand(1,10);
		$thum_url = TEMPLATE_URL.'images/rand/'.$randval.'.jpg';
	}
	echo $thum_url;
}
?>


有朋自远方来...评论一下呗O(∩_∩)O