图片生成 php源码_长微博生成工具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;

}

?>

长微博工具

长微博工具

长微博

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值