2011-05-22 55 views
1

我搜索了Google和Stackoverflow,但找不到答案。可能是因为我在寻找错误的问题。没有正确的问题,很难得到正确的答案。所以我希望有人能帮助我。PHP从数据库位置创建嵌入图像

我创建了一个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多个网站这样做,但我无法找到如何。

我发现这个伪代码feedburner的代码,但我不知道如何把它变成我需要的东西。

<?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图像。

工作正常,但一旦位置根据用户评分改变,图像将不会显示正确的数据。

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

综述

基本上什么,我尽量让的东西,会显示最新的数据,根据该网站的排名。就像显示Twitter追随者的数量一样,例如http://twittercounter.com/counter/?username=labnol或者feedburner追随者http://feeds.feedburner.com/~fc/labnol 两者都是基于数据库中的信息显示数字的图像。但是我想根据数据库中网站的排名创建自己的图片。

回答

0

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

如果这个失败,我会检查它从哪里得到Get ['count']数据,我假设是网站的排名数字。

你可以检查Get ['Count']数据是否应该更新?

我不确定在URL中使用ARRAY是一个好主意,为什么不使用Sessions? This link might be of interest.

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

+0

如果在这种情况下添加,而不是从得到的图像随机一个目录,你会从你的数据库中选择它(如示例代码所示)Gif,Jpeg,Jpg,Png b您将始终使用此方法输出一个PNG作为您的标题(“Content-type:image/png”); 设置为PNG。 – DevilCode 2011-05-22 22:41:07

+0

谢谢你尝试帮助。我不明白什么显示随机图像与这个问题有关。我添加了其他信息来清除我的问题。 – Lisa 2011-05-22 22:46:11

+0

再次感谢你,但我仍然认为我没有很好地解释它。基本上我试图做的是根据网站的排名显示最新的数据。就像显示Twitter追随者的数量一样,例如http://twittercounter.com/counter/?username=labnol或http://feeds.feedburner.com/~fc/labnol都是基于信息显示数字的图像在数据库中。但是我想根据数据库中网站的排名创建自己的图片。 – Lisa 2011-05-23 05:17:49

0

这是我到目前为止(由于不同的cookie,我无法从这台计算机编辑此问题)。

这是基于帮助从

How to fetch data from database and display it in 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); 
?>