Home > php教程 > php手册 > 基于新浪sae使用php生成图片发布图文微博

基于新浪sae使用php生成图片发布图文微博

WBOY
Release: 2016-06-06 19:59:51
Original
914 people have browsed it

1.生成图片的代码: ?phpheader (Content-type: image/png);mb_internal_encoding(UTF-8); // 设置编码session_start();//$_SESSION['weiboContent'] = 我的微博时间轴;$text = $_SESSION['weiboContent'];function autowrap($fontsize, $angle, $fontface,

1.生成图片的代码:

<?php header ("Content-type: image/png");
mb_internal_encoding("UTF-8"); // 设置编码
session_start();
//$_SESSION['weiboContent'] = "我的微博时间轴";
$text = $_SESSION['weiboContent'];

function autowrap($fontsize, $angle, $fontface, $string, $width) {
// 这几个变量分别是 字体大小, 角度, 字体名称, 字符串, 预设宽度
	$content = "";

	// 将字符串拆分成一个个单字 保存到数组 letter 中
	for ($i=0;$i<mb_strlen($string);$i++) {
		$letter[] = mb_substr($string, $i, 1);
	}

	foreach ($letter as $l) {
		$teststr = $content." ".$l;
		$testbox = imagettfbbox($fontsize, $angle, $fontface, $teststr);
		// 判断拼接后的字符串是否超过预设的宽度
		if (($testbox[2] > $width) && ($content !== "")) {
			$content .= "\n";
		}
		$content .= $l;
	}
	return $content;
}

$bg = imagecreatetruecolor(800, 350); // 创建画布
$white = imagecolorallocate($bg, 255, 255, 255); // 创建白色

// 将背景设为红色
$bgc = imagecolorallocate($bg, 45, 45, 45);
imagefill($bg, 0, 0, $bgc);

for($i=0;$iwrite("img",$filename,$imgstr);
ob_end_clean();
imagedestroy($bg);
?>
Copy after login

特别注意代码中这几行:

$s = new SaeStorage();
ob_start();
imagepng($bg);
$imgstr = ob_get_contents();
$uid = $_SESSION["uid"];
$filename = $uid.".png";
$s->write("img",$filename,$imgstr);
ob_end_clean();
imagedestroy($bg);


图片保存中只要使用imagepng相关的函数时要使用缓存,再使用sae的storage进行保存!!!

2.发布微博时获取图片url路径即可:

$token = $_SESSION['token'];
$weibo = new SaeTClientV2(WB_AKEY, WB_SKEY, $token);
$imgurl = $s->getUrl("img",$filename);
Copy after login


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template