Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
224 views
in Technique[技术] by (71.8m points)

Autogenerate Random Images with Colors & Text in PHP

I have attached a procedure that I can use to generate random images. This works so far.

But now I would like to have an additional text, 1 to 3 words each, from a separate txt file, which is then displayed somewhere in the image.

The whole thing should then automatically generate, say, 50 images and save the results as .jpg or .png in a folder at the end.

Does anyone here know how to do this?

error_reporting(E_ALL);
//seed random generator
srand((double)microtime()*1000000);
$p = rand(1,200);
define("IMAGE_WIDTH",300);
define("IMAGE_HEIGHT",300);
define("MAX_LINE_WIDTH",10);
define("COLOR_DEVIATION",18);

// create an image resource
$img = imagecreate(IMAGE_WIDTH,IMAGE_HEIGHT);

// start at middle grey...
$lr = $lg = $lb = 127;

function cmax($x) {
// make sure our color value is a sane number
if ($x > 255) { return 255; }
elseif ($x < 0) { return 0; }
else { return $x; } }

function ncolor($x) {
return rand($x - COLOR_DEVIATION, $x + COLOR_DEVIATION); }

// draw vertical fills
while($p < IMAGE_WIDTH) {
$linecolor = imagecolorallocate($img,
$cr = cmax(ncolor($lr)),
$cg = cmax(ncolor($lg)),
$cb = cmax(ncolor($lb)));
$linewidth = rand(1,MAX_LINE_WIDTH);
imagefilledrectangle($img,$p,0,$p+$linewidth,IMAGE_HEIGHT,$linecolor);
$p = $p + $linewidth;
$lr = $cr;
$lg = $cg;
$lb = $cb;
}
// return the image to the client
header("Content-type:image/png");
imagepng($img);
?> ```



与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...