生成长微博的PHP小程序
下面是用PHP生成长微博的小程序,是从长微博工具的简化而来的一段代码,用来生成长微博基本是够用了。可以新建一个文件,如 index.php ,内容为下面的PHP和html代码,另外,将windows里的宋体字库(示例使用的字体文件名是"SIMSUN.TTC",需根据实际情况修改)复制到 index.php 的同一个目录,然后就能生成长微博了。
长微博页面html
<!DOCTYPE HTML><html><head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>长微博工具</title>
<script type="text/javascript" src="http://lib.sinaapp.com/js/jquery/1.6.2/jquery.min.js"></script>
</head>
<body>
<h1>长微博工具</h1>
<p><textarea name="text" id="text" rows="20" cols="60"></textarea></p>
<p>
<input type="button" id="format" value="格式化" />
<input type="button" id="button" value="生成长微博" />
</p>
<pre id="pre"></pre>
<p><a target="_blank" href="" id="url"></a></p>
<script type="text/javascript">
BIG_FONT_WIDTH = 44;
function f(v,w,c){
c = 0;
w = w || BIG_FONT_WIDTH;
return v.replace(/[\S\s]/g,function(a){
if(/[\r\n]/.test(a)) {
c = -2;
return "\r\n";
}
c += /[\x00-\xFF]/.test(a) ? 1 : 2;
if(c>=w){
c = 0;
return "\r\n"+a;
}
return a;
});
}
$(function(){
$("#format").click(function(){$("#text").val(f($("#text").val()));});
$("#button").click(function(){
if($("#text").val().length<1){
alert("无内容");
} else {
$.ajax({
type:"POST",
url:location.pathname,
data:"text="+$("#text").val(),
dataType:"json",
success:function(m){
if(m.message)$("#pre").innerHTML(m.message);
if(m.imgurl)$("#url").attr("href", m.imgurl).text(m.imgurl);
}
});
}
});
});
</script>
</body>
</html>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>长微博工具</title>
<script type="text/javascript" src="http://lib.sinaapp.com/js/jquery/1.6.2/jquery.min.js"></script>
</head>
<body>
<h1>长微博工具</h1>
<p><textarea name="text" id="text" rows="20" cols="60"></textarea></p>
<p>
<input type="button" id="format" value="格式化" />
<input type="button" id="button" value="生成长微博" />
</p>
<pre id="pre"></pre>
<p><a target="_blank" href="" id="url"></a></p>
<script type="text/javascript">
BIG_FONT_WIDTH = 44;
function f(v,w,c){
c = 0;
w = w || BIG_FONT_WIDTH;
return v.replace(/[\S\s]/g,function(a){
if(/[\r\n]/.test(a)) {
c = -2;
return "\r\n";
}
c += /[\x00-\xFF]/.test(a) ? 1 : 2;
if(c>=w){
c = 0;
return "\r\n"+a;
}
return a;
});
}
$(function(){
$("#format").click(function(){$("#text").val(f($("#text").val()));});
$("#button").click(function(){
if($("#text").val().length<1){
alert("无内容");
} else {
$.ajax({
type:"POST",
url:location.pathname,
data:"text="+$("#text").val(),
dataType:"json",
success:function(m){
if(m.message)$("#pre").innerHTML(m.message);
if(m.imgurl)$("#url").attr("href", m.imgurl).text(m.imgurl);
}
});
}
});
});
</script>
</body>
</html>
用php生成长微博图片奕诊智能
error_reporting(7);
define('ROOT', getcwd());
define('BASEDIR', parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH));
if ($_POST['text']) {
$ret = array ();
$text = $_POST['text'];
try {
$ret['imgurl'] = text2img($text, (array)$_REQUEST['config']);
} catch (Exception $e) {
$ret['imgurl'] = print_r($e, true);
}
echo str_replace('\\/', '/', json_encode($ret));
exit (0);
}
function text2img($text, $options = array ()) {
$text .= "\n-------------------------------\n";
$text .= "http://iscup.com";
$rows = substr_count($text, "\n") + 1;
$font_path = $options['fontfile'] ? $options['fontfile'] : ROOT . '/SIMSUN.TTC';
if (!file_exists($font_path))
throw new Exception(" 找不到字体文件:$font_path ");
$font_size = $options['fontsize'] ? $options['fontsize'] : 12;
$padding = $options['padding'] ? $options['padding'] : 30;
$row_plus_height = $options['row_plus_height'] ? $options['row_plus_height'] : 4;
$border = 1;
$im_width = 430;
$im_height = ($row_plus_height + ($font_size * 4) / 3) * $rows + ($padding + $border) * 2;
$im = @ imagecreatetruecolor($im_width, $im_height);
if (!$im)
throw new Exception("初始化图片失败,请检查 GD 配置");
imagefilledrectangle($im, $border, $border, ($im_width -2 * $border), ($im_height -2 * $border), imagecolorallocate($im, 255, 255, 255));
imagettftext($im, $font_size, 0, ($border + $padding), (($font_size * 4) / 3 + $border + $padding), imagecolorallocate($im, 0, 0, 0), $font_path, $text);
$base_path = '/files';
$base_filename = date("Y-m-d,H-i-s") . '.png';
$short_filename = $base_path . '/' . $base_filename;
$short_url = rtrim(BASEDIR, '/') . $short_filename;
@ mkdir(ROOT . $base_path, 0777, true);
$filename = ROOT . $short_filename;
if (!imagepng($im, $filename)) {
throw new Exception("创建图片时出错。");
}
@ imagedestroy($im);
return $short_url;
}
define('ROOT', getcwd());
define('BASEDIR', parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH));
if ($_POST['text']) {
$ret = array ();
$text = $_POST['text'];
try {
$ret['imgurl'] = text2img($text, (array)$_REQUEST['config']);
} catch (Exception $e) {
$ret['imgurl'] = print_r($e, true);
}
echo str_replace('\\/', '/', json_encode($ret));
exit (0);
}
function text2img($text, $options = array ()) {
$text .= "\n-------------------------------\n";
$text .= "http://iscup.com";
$rows = substr_count($text, "\n") + 1;
$font_path = $options['fontfile'] ? $options['fontfile'] : ROOT . '/SIMSUN.TTC';
if (!file_exists($font_path))
throw new Exception(" 找不到字体文件:$font_path ");
$font_size = $options['fontsize'] ? $options['fontsize'] : 12;
$padding = $options['padding'] ? $options['padding'] : 30;
$row_plus_height = $options['row_plus_height'] ? $options['row_plus_height'] : 4;
$border = 1;
$im_width = 430;
$im_height = ($row_plus_height + ($font_size * 4) / 3) * $rows + ($padding + $border) * 2;
$im = @ imagecreatetruecolor($im_width, $im_height);
if (!$im)
throw new Exception("初始化图片失败,请检查 GD 配置");
imagefilledrectangle($im, $border, $border, ($im_width -2 * $border), ($im_height -2 * $border), imagecolorallocate($im, 255, 255, 255));
imagettftext($im, $font_size, 0, ($border + $padding), (($font_size * 4) / 3 + $border + $padding), imagecolorallocate($im, 0, 0, 0), $font_path, $text);
$base_path = '/files';
$base_filename = date("Y-m-d,H-i-s") . '.png';
$short_filename = $base_path . '/' . $base_filename;
$short_url = rtrim(BASEDIR, '/') . $short_filename;
@ mkdir(ROOT . $base_path, 0777, true);
$filename = ROOT . $short_filename;
if (!imagepng($im, $filename)) {
throw new Exception("创建图片时出错。");
}
@ imagedestroy($im);
return $short_url;
}