关于图像的Php代码帮助


Php code help regarding images

所以我有这个php脚本,它在图像上生成随机文本文本文件是不同的php文件,图像文件是一个单独的php文件。image.php文件调用该text.php来选择随机文本。

这个版本运行良好,但有可能在我现有的图像文件上生成一个随机图像吗?

我已经包含了我的代码的当前版本。

这是text.php:

<?php
    $t[] = 'Sample text 1 ';
    $t[] = 'Sample text 2';
    shuffle($t);
?>

这是image.php:

<?php
    require_once 'text.php';
    $text = wordwrap($t[0], 31, "'n", true); //text
    $image = imagecreatefromjpeg('img_empty.jpg'); //background image
?>

欢迎提出任何建议。

您的问题有点不清楚。。如果你不想要text.php,那么你可以直接在image.php中使用它的代码,如下所示。

image.php

<?php
$t[] = 'Sample text 1 ';
$t[] = 'Sample text 2';
shuffle($t);
$text = wordwrap($t[0], 31, "'n", true); //text
$image = imagecreatefromjpeg('img_empty.jpg'); //background image

这里有几个选项

  1. 保存多个背景图像,并从中随机选择一个
  2. 在输出现有图像之前,请使用GD和Image函数在现有图像上绘制(例如,查看imageline()中的绘制线)
  3. 使用GD的imagecreatetruecolor()创建图像,甚至可以获得随机宽度/高度

如果你想创建一个带有随机文本的图像,这将实现

image.php

require_once 'text.php';
header("Content-type: image/png");
$string = wordwrap($t[0], 31, "'n", true); //text
$font  = 2;
$width  = imagefontwidth($font) * strlen($string);
$height = imagefontheight($font);
$image = imagecreatetruecolor ($width,$height);
$white = imagecolorallocate ($image,255,255,255);
$black = imagecolorallocate ($image,0,0,0);
imagefill($image,0,0,$white); 
imagestring ($image,$font,0,0,$string,$black);
imagepng ($image);
imagedestroy($image);

更新了回复代码:如果你想通过特定的图像获得你的随机文本

require_once 'text.php';
header("Content-type: image/png");
$string = wordwrap($t[0], 31, "'n", true); //text

$image  = ImageCreateFromJPEG("img_empty.jpg");
//Defining color. Making the color of text as red (#FFF) as 255,000,000
$color = imagecolorallocate($image , 255, 000, 000); // 
//put string over image with $color as color
imagestring($image,5,126,22,$string,$color);
imagejpeg($image,NULL,100);

上面的代码将在指定的图像上放置一个红色的随机文本。这对我来说很有效。也可以尝试更改imagecolorallocate()函数中定义的颜色。

参考:http://blog.doh.ms/2008/02/12/adding-text-to-images-in-real-time-with-php/