ホームページ php教程 php手册 源码:php文字转图片的类

源码:php文字转图片的类

Jun 06, 2016 pm 08:08 PM
php 写真 文字 昨日 ソースコード 空間

昨天在qq空间看见一篇“神奇的日志”,不同的访客看到同一张图片会显示对应访客的信息,用chrome 查看了下图片来源是个php文件; 基本原理就是 通过 php文件判断来源页面的url(url中包含qq号码),获取qq号码后通过腾讯的开放api接口,获取qq昵称和qq头像,

昨天在qq空间看见一篇“神奇的日志”,不同的访客看到同一张图片会显示对应访客的信息,用chrome 查看了下图片来源是个php文件;

基本原理就是 通过 php文件判断来源页面的url(url中包含qq号码),获取qq号码后通过腾讯的开放api接口,获取qq昵称和qq头像,然后用gb函数画图,输出

今天试着在网上找到一个文字转图片的程序,改吧改吧写了一个类似的程序,但是写完发了几篇日志才发现腾讯好像已经屏蔽掉外部图片在个人中心中显示了;

把改过的一个 php 文字转图片的类发上来给大家分享下,新手一枚代码改的不是很规整,见谅。。

文件名 Text2Img.class.php

<?php class Text2Img{
//$fontFile = 'msyh.ttc'; #字体文件名,请先拷贝一个字体到font目录下,然后修改此配置
private $LibiFontStyle = "mini.TTF";
private $LibiFontColor = "FF0000";
private $LibiBackColor = "FFFFFF";
private $haveBrLinker = "";
private $LibiWidth;
private $Message;
function __construct($options=array()){
foreach($options as $key=>$val){
if(!in_array($key,get_class_vars(get_class($this)))){
continue;
}else{
$this-&gt;$key=$val;
}
}
}
private function str_div($str, $width = 10){
$strArr = array();
$len = strlen($str);
$count = 0;
$flag = 0;
while($flag ';
if(ord($str[$flag]) &gt; 128){
$count += 1;
$flag += 3;
}
else{
$count += 0.5;
$flag += 1 ;
}
if($count &gt;= $width){
$strArr[] = substr($str, 0, $flag);
$str = substr($str, $flag);
$len -= $flag;
$count = 0;
$flag = 0;
}
}
$strArr[] = $str;
return $strArr;
}
private function str2rgb($str)
{
$color = array('red'=&gt;0, 'green'=&gt;0, 'blue'=&gt;0);
$str = str_replace('#', '', $str);
$len = strlen($str);
if($len==6){
$arr=str_split($str,2);
$color['red'] = (int)base_convert($arr[0], 16, 10);
$color['green'] = (int)base_convert($arr[1], 16, 10);
$color['blue'] = (int)base_convert($arr[2], 16, 10);
return $color;
}
if($len==3){
$arr=str_split($str,1);
$color['red'] = (int)base_convert($arr[0].$arr[0], 16, 10);
$color['green'] = (int)base_convert($arr[1].$arr[1], 16, 10);
$color['blue'] = (int)base_convert($arr[2].$arr[2], 16, 10);
return $color;
}
return $color;
}
public function text2Img($text){
if($text==''){
$this-&gt;Message = "没有文字";
return false;
}
$text = substr($text, 0, 30000); #截取前一万个字符
$paddingTop = 20;
$paddingLeft = 15;
$paddingBottom = 20;
$copyrightHeight = 36;
$canvasWidth = 440;
$canvasHeight = $paddingTop + $paddingBottom + $copyrightHeight;
$fontSize = 12;
$lineHeight = intval($fontSize * 1.8);
$textArr = array();
$tempArr = explode("\n", trim($text));
$j = 0;
foreach($tempArr as $v){
$arr = $this-&gt;str_div($v, 25);
$textArr[] = array_shift($arr);
foreach($arr as $v){
$textArr[] = $this-&gt;haveBrLinker . $v;
$j ++;
if($j &gt; 100){ break; }
}
$j ++;
if($j &gt; 100){ break; }
}
$textLen = count($textArr);
$canvasHeight = $lineHeight * $textLen + $canvasHeight;
$im = imagecreatetruecolor($canvasWidth, $canvasHeight); #定义画布
$colorArray = $this-&gt;str2rgb($this-&gt;LibiBackColor);
imagefill($im, 0, 0, imagecolorallocate($im, $colorArray['red'], $colorArray['green'], $colorArray['blue']));
$colorArray = $this-&gt;str2rgb('666666');
$colorLine = imagecolorallocate($im, $colorArray['red'], $colorArray['green'], $colorArray['blue']);
$padding = 3;
$x1 = $y1 = $x4 = $y2 = $padding;
$x2 = $x3 = $canvasWidth - $padding - 1;
$y3 = $y4 = $canvasHeight - $padding - 1;
imageline($im, $x1, $y1, $x2, $y2, $colorLine);
imageline($im, $x2, $y2, $x3, $y3, $colorLine);
imageline($im, $x3, $y3, $x4, $y4, $colorLine);
imageline($im, $x4, $y4, $x1, $y1, $colorLine);
//字体路径
if(!is_file($this-&gt;LibiFontStyle)){
$this-&gt;Message = "字体文件不存在!";
return false;
}
//写入四个随即数字
$colorArray = $this-&gt;str2rgb($this-&gt;LibiFontColor);
$fontColor = imagecolorallocate($im, $colorArray['red'], $colorArray['green'], $colorArray['blue']);
foreach($textArr as $k=&gt;$text){
$offset = $paddingTop + $lineHeight * ($k + 1) - intval(($lineHeight-$fontSize) / 2);
imagettftext($im, $fontSize, 0, $paddingLeft, $offset, $fontColor, $this-&gt;LibiFontStyle, $text);
}
$fontColor = imagecolorallocate($im, 0, 0, 0);
$offset += 18;
$text = '----END----';
imagettftext($im, 10, 0, $paddingLeft, $offset, $fontColor, $this-&gt;LibiFontStyle, $text);
$offset += 18;
$fontColor = imagecolorallocate($im, 255, 0, 0);
$text = 'By Libi';
imagettftext($im, 10, 0, $paddingLeft + 310, $offset, $fontColor, $this-&gt;LibiFontStyle, $text);
header('Content-Type: image/png');
imagepng($im, $imgfile);
imagedestroy($im);
}
}
?&gt;
ログイン後にコピー
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。

ホットな記事タグ

メモ帳++7.3.1

メモ帳++7.3.1

使いやすく無料のコードエディター

SublimeText3 中国語版

SublimeText3 中国語版

中国語版、とても使いやすい

ゼンドスタジオ 13.0.1

ゼンドスタジオ 13.0.1

強力な PHP 統合開発環境

ドリームウィーバー CS6

ドリームウィーバー CS6

ビジュアル Web 開発ツール

SublimeText3 Mac版

SublimeText3 Mac版

神レベルのコード編集ソフト(SublimeText3)

Ubuntu および Debian 用の PHP 8.4 インストールおよびアップグレード ガイド Ubuntu および Debian 用の PHP 8.4 インストールおよびアップグレード ガイド Dec 24, 2024 pm 04:42 PM

Ubuntu および Debian 用の PHP 8.4 インストールおよびアップグレード ガイド

CakePHP の日付と時刻 CakePHP の日付と時刻 Sep 10, 2024 pm 05:27 PM

CakePHP の日付と時刻

CakePHP ファイルのアップロード CakePHP ファイルのアップロード Sep 10, 2024 pm 05:27 PM

CakePHP ファイルのアップロード

CakePHP ルーティング CakePHP ルーティング Sep 10, 2024 pm 05:25 PM

CakePHP ルーティング

CakePHP プロジェクトの構成 CakePHP プロジェクトの構成 Sep 10, 2024 pm 05:25 PM

CakePHP プロジェクトの構成

CakePHP について話し合う CakePHP について話し合う Sep 10, 2024 pm 05:28 PM

CakePHP について話し合う

CakePHP クイックガイド CakePHP クイックガイド Sep 10, 2024 pm 05:27 PM

CakePHP クイックガイド

PHP 開発用に Visual Studio Code (VS Code) をセットアップする方法 PHP 開発用に Visual Studio Code (VS Code) をセットアップする方法 Dec 20, 2024 am 11:31 AM

PHP 開発用に Visual Studio Code (VS Code) をセットアップする方法

See all articles