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

1.生成图片的代码:

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

$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);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值