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;$i<100;$i++) //加入干扰象素 { $randcolor = ImageColorallocate($bg,rand(0,255),rand(0,255),rand(0,255)); imagesetpixel($bg, rand(0,800) , rand(0,150) , $randcolor); } $text = autowrap(15, 0, "simsun.ttc", $text, 900); // 自动换行处理 // 若文件编码为 GB2312 请将下行的注释去掉 //$text = iconv("GB2312", "UTF-8//IGNORE", $text); imagettftext($bg, 12, 0, 10, 30, $white, "simsun.ttc", $text); $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); ?>
$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);