php扩展之画图

<?php
//print_r(gd_info());//取得当前安装的 GD 库的信息
创建画布
/*$im=imagecreatetruecolor(300, 200);
//创建颜料
$gray=imagecolorallocate($im, 100,100,100);
//然后是画,,也可以是填充,
imagefill($im, 100, 100, $gray);
//保存成png图片
$a=imagepng($im,'./01.png')
//销毁
imagedestroy($a);
*/
//创建画布(要造什么格式的画布)
/*$im=imagecreatefromjpeg('./home.jpg');
//然后是造颜料,两种颜料,因为一个函数只能一种,所以两种,开始
$red=imagecolorallocate($im, 255,0,0);
$blue=imagecolorallocate($im, 0,0,255);
//然后是画线
imageline($im, 0,0,670,503,$red)
//然后是不知道
header('content-type:image/jpeg')
//header("Content-type: image/jpeg")表明请求页面的内容是jpeg格式的图像。 
//然后是输出把
imagejpeg($im);//输出的是jpeg的图片呀,只有jpeg的图片才能输出呀
//销毁
imagedestroy($im);
*/
//创建一个jpg格式的画布,名字叫做home
/*$im=imagecreatefromjpeg('./home.jpg');
//然后是颜料
$blue=imagecolorallocate($im,0,0,255);
//ttf字体库。
imagettftext($im, 50, 0, 100, 100, $blue, './msyh.ttf',"宋体");
header('Content-type:image/jpeg');
//然后是输出jpeg的画布
imagejpeg($im);
imagedestroy($im);
*/
//创建画布,400*400,宽*高
/*$im=imagecreatetruecolor(400, 400);
//然后是颜料
$gray=imagecolorallocate($im,100,100,100);
$blue=imagecolorallocate($im, 0,0,255);
//然后是填充
imagefill($im, 0,0,$gray)
//然后是
imagearc($im,300,300,300,300, 45, 135, $blue);
header('Content-type:image/jpeg');
///然后输出
imagejpeg($im);*/
//是椭圆
//创建画布400*800
/*$im=imagecreatetruecolor(400, 800);
//颜料100100100
$gray=imagecolorallocate($im, 100,100,100);
//颜料00255
$blue=imagecolorallocate($im, 0,0,255);
//然后是填充
imagefill($im,0,0,$gray)
//然后是画椭圆
imageellipse($im, 200,50,200,100, $blue);
//然后是画矩形
imagerectangle($im,100,100,300,200,$blue);
*/
//画一个圆弧
//创建画布
/*$im=imagecreatetruecolor(500,500);
//然后是做颜料
$gray=imagecolorallocate($im,100,100,100);
$blue=imagecolorallocate($im,0,0,255);
//然后是填充
imagefill($im, 0,0, $gray);
//然后是画椭圆弧
imagearc($im, 200,200,200,200,45,135,$blue);
///然后是设置gd类型
header('content-type:image/jpeg')
//输出jpeg图片
imagejpeg($im);
*/
//画矩=矩形
//查创建画布
/*$im=imagecreatetruecolor(400, 800);
//然后是颜料,一种是蓝色,一种是gray色,
$gray=imagecolorallocate($im, 100,100,100);
$blue=imagecolorallocate($im, 0,0,255);
//然后是填充
imagefill($im, 0,0, $gray)
//然后是画椭圆
imageellipse($im, 200,50,200,100,$blue);
//然后是画矩形
imagerectangle($im, 100,100,300,200,$blue);
imageellipse($im, 200,300,200,200,$blue)
//然后是输出销毁类型
header('content-type:image/jpeg');
imagejpeg($im);
imagedestroy($im);
*/
/*$im=imagecreatetruecolor(400, 400);
//然后是颜料4种
$gray=imagecolorallocate($im,100,100,100);
$blue=imagecolorallocate($im,0,0,255);
$red=imagecolorallocate($im, 255,0,0);
$orange=imagecolorallocate($im,248,224,143);
//然后是把颜色填充到画布上
imagefill($im, 100,100,$gray);
//然后是画椭圆
imageellipse($im,200,200,200,200,$blue);
//然后是填充椭圆
imagefill($im, 200,200,$red);
//然后是画一个矩形直接填充
imagefilledrectangle($im, 0,100,200,200,$blue);
imagefilledellipse($im,100,80,200,100,$orange);
header('content-type:image/jpeg');
imagejpeg($im);
imagedestroy($im);
*/
//读取图片作为画布资源,就是说要
/*$small=imagecreatefrompng('./feng.png');//这个小图
//然后是判断大小
$cx=imagesx($small);//小图的宽
$cy=imagesy($small);//小图的高
//然后是两倍大画布资源
$big=imagecreate(2*$cx, 2*$cy);
//然后是设置画布的背景为100,100,100
$gray=imagecolorallocate($big, 100,100,100);
//然后是铺上大布上
imagefill($big,0,0,$gray);
*/
//验证码

/*function randName($n = 6) {
        if($n <= 0) {
            return '';
        }
        $str = 'abcdefghijkmnpqrstuvwxyzABCDEFGHIJKMNPQRSTUVWXYZ0123456789';
        $str = substr(str_shuffle($str),0,$n);

        return $str;
    }

// 做验证码

$im = imagecreatetruecolor(80,30);

$gray = imagecolorallocate($im,30,30,30);
$red = imagecolorallocate($im,255,0,0);

imagefill($im,0,0,$gray);

imagestring($im,5,5,5,randName(4),$red);

// 输出
header('content-type: image/jpeg');
imagejpeg($im);
*/
//缩略图:意思是在一张大画布上创建一个小画布粘贴上去大画布上
//先创建获取一个png格式的图片,,,图片名叫做feng.png
/*$feng=imagecreatefrompng('./feng.png');
//然后是获取这张图片的宽高
$fx=imagesx($feng);//获取宽
$fy=imagesy($feng);//获取高
//然后是做一个1/2的小画布,,并创建出来宽额和高
$sx=$fx/2;//$sx小画布
$sy=$fy/2;//$xy小画布
//然后是先创建出小画布
$small=imagecreatetruecolor($sx, $sy);
//然后是imagecopyresampled
/*imagecopyresampled是 返回更改过的图像
也就是第一个是新创建的图片的,第二个是什么格式的图片的image
*/
/*imagecopyresampled($small, $feng, 0,0,0,0, $fx,$fy,$sx,$sy);
//作用是两张图片
//然后是判断如果
if(imagepng($small,'./xiaofeng.png'))//意思是small这张画布在不在路径当前路径下
{
	echo "成功";
}
else
{
	echo "失败";
}*/
//然后销毁*/
//造大图画布
/*$big=imagecreatetruecolor(2*$sx, 2*sy);
//然后是颜料
$gray=imagecolorallocate($big,100,100,100);
//然后是把1颜料进大画布里面
imagefill($big, 0,0, $gray);
//复制小图到大图上
imagecopy($big, $small, 0,0,0,0,$sx,$sy);
//然后设置字符集
header('content-type:image/jpeg');
//然后是输出了
imagejpeg($big);
//然后销毁
imagedestroy($small)
*/
//然后是水印效果
/*$big=imagecreatefromjpeg('./home.jpg');//意思是创建格式为jpg名字为home在当前路径下的画布
//然后是获取这张画布的xy宽高
$bx=imagesx($big);
$by=imagesy($big);
//然后是在创建
$small=imagecreatefrompng('./feng.png');
//然后是算出它的大小
$sx=imagesx($small);
$sy=imagesy($small)
//然后是加文字
$blue=imagecolorallocate($big,0,0,255);
//然后是加文字
imagettftext($big, 16,0,100,100,$blue,'./msyh.ttf','陈业贵爱李文');
//然后是imagecopymerge函数用于拷贝并合并图像的一部分,成功返回 TRUE ,否则返回 FALSE 。
imagecopymerge($big, 16,0,100,100,$blue,'./msth.ttf','陈业贵爱李文');
//然后是字符集
header('content-type:image/jpeg');
imagejpeg($big);
imagedestroy($big);
*/
?>
<div class="post-text" itemprop="text"> <p>i got this script to generate an image out of a random jpg in a directory, adding a random slogan from a database:</p> <pre><code><?php header('Content-type: text/html; charset=utf-8'); include '../connect.php'; require_once 'random.php'; $timestamp = time(); $date = date("d.m.Y_G", $timestamp); $slogan = $mysqli->query("SELECT `text` FROM `slogans` ORDER BY RAND() LIMIT 1"); $slogan_txt = $slogan->fetch_assoc(); $bg = get_rand_img('../../images/'); $imgPath = '../../images/' .$bg; $text = $slogan_txt[text]; $image = $imgPath; $font = "font.TTF"; $font_size = "25"; $image_2 = imagecreatefromjpeg($image); $black = imagecolorallocate($image_2,0,0,0); $image_width = imagesx($image_2); $image_height = imagesy($image_2); $text_box = imagettfbbox($font_size,$angle,$font,$text); $text_width = $text_box[2]-$text_box[0]; // lower right corner - lower left corner $text_height = $text_box[3]-$text_box[1]; $x = ($image_width/2) - ($text_width/2); $y = ($image_height/2) - ($text_height/2); imagettftext($image_2,$font_size,0,$x,$y,$black,$font,$text ); header ("Content-type: image/png"); imagejpeg($image_2); ?> </code></pre> <p>works fine so far.</p> <p>Now there are some slogans with to much words for one row. i need them to be wrapped and also be centered!</p> <p>can't use wordwrap in imagettftext(), so i need to explode it somehow.</p> <p>i found some functions on the web, but they dont work as expected. maybe i just dont know how to combine them with my existing code!</p> <p>maybe someone got a workign example from own projects?</p> <p>thanks so far!</p> </div>
©️2020 CSDN 皮肤主题: 数字20 设计师:CSDN官方博客 返回首页