与PHP的Farbtastic

时间:2014-05-28 05:32:37

标签: php

嘿家伙正在开发一个图像创建应用程序,其中允许用户从Farbtastic颜色选择器中选择颜色范围,并使用文本生成图像。文本将以用户选择的颜色显示来自Farbtastic。

html代码(颜色选择器)

<html>
<head>
 <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>
 <script type="text/javascript" src="jquery.js"></script>
 <script type="text/javascript" src="farbtastic.js"></script>
 <link rel="stylesheet" href="farbtastic.css" type="text/css" />
<script type="text/javascript" charset="utf-8">
  $(document).ready(function() {


 $('#demo').hide();


   $('#picker').farbtastic('#color');
  });

 </script>

</head>
<body>


<div id="demo" style="color: red; font-size: 1.4em"></div>

<form action="" style="width: 400px;">
  <div class="form-item"><label for="color">Color:</label><input type="text" id="color" name="color" value="#123456" /></div><div id="picker"></div>

</form>

<br>

<form action="image.php">
<input type="submit" value="Submit">
</form>
</body>
</html>

我的PHP代码(图像生成)

<?php
header ('Content-Type: image/png');
$im = @imagecreatetruecolor(120, 20)
  or die('Cannot Initialize new GD image stream');

$text_color = imagecolorallocate($im, 255, 255, 255);

imagestring($im, 1, 5, 5,  'A Simple Text String', $text_color);

imagepng($im);

imagedestroy($im);

?>

我真的卡住了我不知道如何将我的html整合到php中。我需要的是在图像上的文本上生成颜色。颜色必须来自用户选择的Farbtastic颜色选择器。关于如何做这个或任何代码示例的任何想法将不胜感激..谢谢

1 个答案:

答案 0 :(得分:0)

在你的html脚本中,你应该指明php文件的端点,在第一个表单标记中,第二个表单标记是不必要的:

<form action="image.php" method="POST" style="width: 400px;">
<div class="form-item"><label for="color">Color:</label><input type="text" id="color" name="color" value="#123456" /></div><div id="picker"></div>
<input type="submit" value="Submit">
</form>

在php脚本中,您应该从POST数组中读取颜色,然后将其转换为rgb。示例如下:

<?php
//read hex color and remove leading #
$hex_color = str_replace('#', '', $_POST['color']);
//convert hex color to rgb
$red = hexdec(substr($hex_color,0,2));
$green = hexdec(substr($hex_color,2,2));
$blue = hexdec(substr($hex_color,4,2));

header ('Content-Type: image/png');
$im = @imagecreatetruecolor(120, 20)
  or die('Cannot Initialize new GD image stream');

//use rgb color
$text_color = imagecolorallocate($im, $red, $green, $blue);

imagestring($im, 1, 5, 5,  'A Simple Text String', $text_color);

imagepng($im);

imagedestroy($im);

?>