创建动态PNG图像

apn*_*rve 26 php png image

我想在PHP中创建一个小函数,它接受颜色,形状,透明度等参数并输出PNG图像.我听说过PHP GD库,但我想知道怎样才能创建一个尽快创新的东西.media.mit.edu

Sam*_*152 22

这是一个很好的例子,您几乎可以使用这些功能完成所有操作.虽然可能,创建一个像你描述的那样的图像会非常困难,但我已经用渐变,循环和颜色做了一些奇怪的事情.

如果你想根据一些参数动态制作这样的图像,你可以在photoshop中预先创建图像,然后根据用户选择的内容叠加它们.

你可以有很多乐趣.

编辑:哦顺便说一句,如果你感兴趣的给出一个无效的参数显示一些负责创建图像并导致错误的python代码.这是一个了解代码的好地方.

第二次编辑:这只是我用这种技术所做的事情.请记住它已经有一段时间了.它接受基于查询字符串的名称,并且基本上做了一些带有大量随机数的循环.

这是源代码,我为任何愚蠢的代码/引号道歉.这是在很久以前写的,当我大约14岁时,我相信(可能有很多瑕疵).

<?php
header("Content-type:image/jpeg");
$array=array("I am a monument to all your sins", "Currently making pizza","Best before 12/7/09", "Farming Onions");
        function imagettftext_cr(&$im, $size, $angle, $x, $y, $color, $fontfile, $text)
        {
            // retrieve boundingbox
            $bbox = imagettfbbox($size, $angle, $fontfile, $text);
            // calculate deviation
            $dx = ($bbox[2]-$bbox[0])/2.0 - ($bbox[2]-$bbox[4])/2.0;         // deviation left-right
            $dy = ($bbox[3]-$bbox[1])/2.0 + ($bbox[7]-$bbox[1])/2.0;        // deviation top-bottom
            // new pivotpoint
            $px = $x-$dx;
            $py = $y-$dy;
            return imagettftext($im, $size, $angle, $px, $y, $color, $fontfile, $text);
        }
$image = imagecreate(500,90);
$black = imagecolorallocate($image,0,0,0);
$grey_shade = imagecolorallocate($image,40,40,40);
$white = imagecolorallocate($image,255,255,255);


$text = $array[rand(0,sizeof($array)-1)];

// Local font files, relative to script
$otherFont = 'army1.ttf';
$font = 'army.ttf';

if($_GET['name'] == ""){ $name = "Sam152";}else{$name= $_GET['name'];}
$name = substr($name, 0, 25);    


//BG text for Name
while($i<10){
imagettftext_cr($image,rand(2,40),rand(0,50),rand(10,500),rand(0,200),$grey_shade,$font,$name);
$i++;
}
//BG text for saying
while($i<10){
imagettftext_cr($image,rand(0,40),rand(90,180),rand(100,500),rand(200,500),$grey_shade,$otherFont,$text);
$i++;
}

// Main Text
imagettftext_cr($image,35,0,250,46,$white,$font,$name);
imagettftext_cr($image,10,0,250,76,$white,$otherFont,$text);
imagejpeg($image);

?>
Run Code Online (Sandbox Code Playgroud)

  • 嘿!真的很酷.我从来不知道使用这些功能可以实现这一点. (2认同)

nia*_*her 8

这是我之前用来生成具有两个名称的图像的代码,这些名称可以从查询字符串参数中接受.我使用准备好的背景图像并将名称放在它上面.

<?php
// Print two names on the picture, which accepted by query string parameters.

$n1 = $_GET['n1'];
$n2 = $_GET['n2'];

Header ("Content-type: image/jpeg");
$image = imageCreateFromJPEG("images/someimage.jpg");
$color = ImageColorAllocate($image, 255, 255, 255);

// Calculate horizontal alignment for the names.
$BoundingBox1 = imagettfbbox(13, 0, 'ITCKRIST.TTF', $n1);
$boyX = ceil((125 - $BoundingBox1[2]) / 2); // lower left X coordinate for text
$BoundingBox2 = imagettfbbox(13, 0, 'ITCKRIST.TTF', $n2);
$girlX = ceil((107 - $BoundingBox2[2]) / 2); // lower left X coordinate for text

// Write names.
imagettftext($image, 13, 0, $boyX+25, 92, $color, 'ITCKRIST.TTF', $n1);
imagettftext($image, 13, 0, $girlX+310, 92, $color, 'ITCKRIST.TTF', $n2);

// Return output.
ImageJPEG($image, NULL, 93);
ImageDestroy($image);
?>
Run Code Online (Sandbox Code Playgroud)

要在页面上显示生成的图像,请执行以下操作:

<img src="myDynamicImage.php?n1=bebe&n2=jake" />
Run Code Online (Sandbox Code Playgroud)


T.T*_*dua 5

这是一个简单的例子:

<?php
    $your_text = "Helloooo Worldddd";

    $IMG = imagecreate( 250, 80 );
    $background = imagecolorallocate($IMG, 0,0,255);
    $text_color = imagecolorallocate($IMG, 255,255,0); 
    $line_color = imagecolorallocate($IMG, 128,255,0);
    imagestring( $IMG, 10, 1, 25, $your_text,  $text_color );
    imagesetthickness ( $IMG, 5 );
    imageline( $IMG, 30, 45, 165, 45, $line_color );
    header( "Content-type: image/png" );
    imagepng($IMG);
    imagecolordeallocate($IMG, $line_color );
    imagecolordeallocate($IMG, $text_color );
    imagecolordeallocate($IMG, $background );
    imagedestroy($IMG); 
    exit;   
?>
Run Code Online (Sandbox Code Playgroud)