带有 MySQL 信息的文本到图片


text to picture with mysql information

im 正在建立一个网站 indianskincare.co.in 它正在达到我想要的。 问题是我想将每个产品的所有文本转换为图片,因为我的客户希望文本不那么容易复制。 我看到如何使用PHP通过调用图像源中的PHP和PHP中的相关代码来完成此操作。

它可以很好地处理普通文本,但是当我包含MySQL行或任何复杂的代码时,它会显示一个损坏的图像。

这作品显示时间和文字

Header( "Content-type: image/png");

$image = imagecreate(320,130);
$date = date ("F dS Y h:i a");
$ip = "ip";
$fullip = "Your IP address is $ip.";
$black = ImageColorAllocate($image,0,0,0);
$red = ImageColorAllocate($image,204,0,0);
$white = ImageColorAllocate($image,255,255,255);
ImageFilledRectangle($image,0,0,320,130,$white);
ImageString($image, 14, 0, 0, "$fullip", $red);
ImageString($image, 12, 0, 24, "Time is $date", $black);
ImagePNG($image);
ImageDestroy($image);

我尝试在其中一个变量之间添加我的 mysql 代码(我认为在哪个变量之间无关紧要),否则在另一个页面上可以完美运行,但似乎无法将文本更改为图片。

MySQL 代码

if(isset($_GET['id']))
{
$id=$_GET['id'];
$qry=mysql_query("SELECT * FROM Product WHERE id='$id'", $con);
if(!$qry)
{
die("Query Failed: ". mysql_error());
}
                /* Fetching data from the field "title" */
while($row=mysql_fetch_array($qry))
{
echo "<div style='overflow-x:hidden;width:20%;margin:0.5em;text-align:center;float:left;'>";
echo "<h3>".$row['name']."</h3>";
echo "<img  style='width:100%;margin:0;' src='".$row['image']."' /><br>";
echo "<h1><img style='height:4%;' src='images/rs.png' />".$row['price']."</h1><br><a href='product.php?cat=".$row['short']."'>Back</a><br>";
echo "</div><div style='width:50%;float:left;'>";
echo "<h5 style='font-weight:normal;'>".$row['description']."</h5><br>";

所以我的问题是如何将 MySQL 数组中的文本修改为图像。

PS:我知道MySQL被弃用不是首选,但是我已经使用它完成了网站,我将无法修改它以使用其他方法

前段时间我使用这个类 - 它运行良好:

http://image.intervention.io/

干预图像需要以下组件才能正常工作。

PHP >= 5.3
Fileinfo Extension

以及以下图像库之一。

GD Library (>=2.0) … or …
Imagick PHP extension (>=6.5.7)

一个好的开始是安装作曲家:https://packagist.org/

使用作曲家,您可以轻松地为几乎所有问题安装PHP包/模块。页面中的示例(如何将文本转换为图像)

// create Image from file
$img = Image::make('public/foo.jpg');
// write text
$img->text('The quick brown fox jumps over the lazy dog.');
// write text at position
$img->text('The quick brown fox jumps over the lazy dog.', 120, 100);
// use callback to define details
$img->text('foo', 0, 0, function($font) {
    $font->file('foo/bar.ttf');
    $font->size(24);
    $font->color('#fdf6e3');
    $font->align('center');
    $font->valign('top');
    $font->angle(45);
});
// draw transparent text
$img->text('foo', 0, 0, function($font) {
    $font->color(array(255, 255, 255, 0.5))
});

因此,您的代码的示例可能是(如果干预图像类正常工作):

$result = array();
while($row=mysql_fetch_array($qry))
{
    $result[] = array(
        'name' => convertToImage($row['name'], $row['id'].'_name'),
        'price' => convertToImage($row['price'], $row['id'].'_price')
    )
}
function convertToImage($text, $name) {
    $img = Image::make('images/transparent.png');
    $img->text($text);
    $img->save('images/'.$name.'.png');
    return '<img src="images/'.$name.'.png">';
}
?>