PHP 从数据库位置创建嵌入图像


PHP create embed image from database position

我搜索了Google和Stackoverflow,但找不到答案。可能是因为我搜索了错误的问题。没有正确的问题,就很难得到正确的答案。所以我希望有人能帮助我。

我创建了一个前 20 名列表,人们可以在其中对他们最喜欢的网站进行排名(我知道它不是那么原始,但我这样做是为了学习 php)

网站可以添加到数据库中,并根据投票进行排序。每个网站在数据库中都有自己唯一的ID,名称和位置。

我无法弄清楚的是如何执行以下操作。

在显示的列表旁边显示一个获取代码按钮。此按钮将为可以在任何网站上显示的图像文件创建代码。例如:

<img src="http://www.example.com/counter.php?id=47"/>

甚至更好

<img src="http://www.example.com/47.gif"/>

为此,我需要制作一个php代码,它可以获取id并将其转换为一个漂亮的图像文件,然后我被卡住了。我已经看到twitter,feedburner,Technorati和其他100多个网站这样做,但我找不到如何。

我发现了这个伪造饲料燃烧器的代码,但我无法弄清楚如何将其变成我需要的东西。

<?php
//Send a generated image to the browser
create_image();
exit();
function create_image(){
//Create the image resource
$image = imagecreatefromgif('image.gif');
//Create text color
$brown = ImageColorAllocate($image, 0, 0, 0);
//Check for the get parameters
if (isset($_GET['count']) && is_numeric($_GET['count']))
$rank = $_GET['count'];
else
$rank = 20;
// Some Alignment Calculations
$bbox = imagettfbbox(8.5, 1,'verdana.ttf', $rank);
$xcorr = 0 + $bbox[2];    $xcorr = 31 - $xcorr;
//Add the number in brown color to the image
imagettftext($image,8.5,0,$xcorr,16,$brown,'verdana.ttf',$rank);
//Tell the browser what kind of file is come in
header("Content-Type: image/gif");
imagegif($image);
//Free up resources
ImageDestroy($image);}?>

基于www.mygeekpal.com/how-to-fake-your-feedburner-subscribers/

使用上面的代码并将其命名为 counter .php我可以从数据库中获取位置并创建

<img src='http://www.example.com/counter.php?count=".$array ['position']."' />

这会从数据库中获取网站的位置($array已经获取),并创建一个位置为 nr 的图像。

工作正常,但是一旦位置根据用户评级发生变化,图像将不会显示正确的数据。

我希望有人能帮忙。谢谢。

总结

基本上,我试图做的是根据网站的排名显示最新数据的东西。就像显示推特关注者的数量一样,例如 http://twittercounter.com/counter/?username=labnol 或提要关注者 http://feeds.feedburner.com/~fc/labnol两者都是根据数据库中的信息显示数字的图像。但是我想根据网站在数据库中的排名创建自己的图像。

查看您的代码,每次重新加载页面时它都应该更新。清除浏览器缓存。

如果失败,我将检查它从哪里获取 Get['count'] 数据,我假设这是站点的排名号。

你能检查 Get ['计数'] 数据是否正在按预期更新吗?

我不确定在 URL 中使用数组是个好主意,为什么不使用会话?此链接可能会引起您的兴趣。

对不起,我没有提供更多帮助。

这就是我目前所拥有的(由于不同的 cookie,我无法从这台计算机上编辑这个问题)。

这是基于来自

如何从数据库中获取数据并在PHP中显示?

由于https://stackoverflow.com/users/353790/robertpitt

这似乎有效

<?php
//Connect to DB
$db = mysql_connect("localhst","user","pass") or die("Database Error");
mysql_select_db("db_name",$db);
//Get ID from request
$id = isset($_GET['id']) ? (int)$_GET['id'] : 0;
//Check id is valid
if($id > 0)
{
//Query the DB
$resource = mysql_query("SELECT * FROM domains WHERE id = " . $id);
if($resource === false)
{
    die("Database Error");
}
if(mysql_num_rows($resource) == 0)
{
    die("No User Exists");
}
$user = mysql_fetch_assoc($resource);
}
$img_number = imagecreate(110,24);
$image = imagecreatefromgif('image.gif');
$backcolor = imagecolorallocate($img_number,254,46,212);
$textcolor = imagecolorallocate($image, 0, 0, 0);
imagefill($image,0,0,$backcolor);
$number = $user['position'];
Imagestring($image,9,26,4,$number,$textcolor);
header("Content-Type: image/gif");
imagegif($image);
ImageDestroy($image);
?>