2012-08-03 86 views
1

所以我有这个PHP脚本,其中在图像上生成随机文本。 该文本文件是一个不同的PHP文件,图像文件是一个单独的PHP文件。 image.php文件调用此text.php来选择一个随机文本。关于图像的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 
?> 

任何建议都欢迎。

+0

您是否试图制作一个验证码?另外,你对“随机图像”意味着什么? – Thorbear 2012-08-03 12:38:51

+0

不是验证码。 想要在固定图像背景上随机播放随机图像:) – Aviral 2012-08-03 16:23:52

回答

0

你的问题有点不清楚,你不需要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 
+0

感谢您提出宝贵建议,但是,是否可以在固定图像上生成随机图像? 这就是我想问:) – Aviral 2012-08-03 16:22:48

+0

所以你想在图像上的随机数字或随机文本? – VibhaJ 2012-08-04 06:16:58

+0

@ VibhaJ - 我想要一些其他的随机图像在一个固定的图像。这是可能的吗? – Aviral 2012-08-04 10:03:53

0

,如果你想创建一个随机文本的图像,这将做到这一点

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/

+0

感谢您提出宝贵建议,但是,是否可以在固定图像上生成随机图像? 这就是我想问:) – Aviral 2012-08-03 16:23:28

+0

@AviralKacholia:请检查更新的答案。 – mithunsatheesh 2012-08-04 08:21:21

+0

这更与文字颜色变化有关。 我想要一个固定图像上的随机图像。这可能吗? :) – Aviral 2012-08-04 10:05:18

0

几个在这里

  1. 选项有几个背景图像保存,并选择一个其中随机。
  2. 使用GD and Image Functions在输出之前绘制您的现有图像(例如,查看imageline()以绘制线条)。
  3. 使用GD的imagecreatetruecolor()来创建图像,甚至可以获得随机的宽度/高度。
+0

嗯..所以通过使用GD和图像功能,我可以在固定的背景上显示随机图像? 谢谢。 – Aviral 2012-08-03 16:26:37

+0

@AviralKacholia就像我在我的答案中所写的那样,使用GD和图像函数,您可以将随机的东西绘制到现有图像上,并将其直接输出到用户或将其保存到您选择的位置。 – Thorbear 2012-08-03 17:26:24