PHP create embed image from database position

别等时光非礼了梦想. 提交于 2019-12-11 06:46:14

问题


I searched Google and Stackoverflow but could not find the answer. Probably it is because I am searching for the wrong question. Without the right question, it’s hard to get the right answers. So I hope someone can help me.

I have created a top 20 list where people can rank their favourite website (I know it is not that original, but I do it to learn php)

Websites can be added to a database and are sorted based on votes. Each website has its own unique ID in the database, name and position.

What I cannot figure out is how to do the following.

Next to the list displayed show a get code button. This button will create a code for an image file that can be display on any website. For example:

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

or even better

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

To do this I need to make a php code, that can take the id and turn it into a nice image file and there I am stuck. I have seen twitter, feedburner, Technorati and over 100 other websites do this, but I cannot find out how.

I found this code that fakes feedburner, but I cannot figure out how to turn it into what I need.

<?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);}?>

Based on www.mygeekpal.com/how-to-fake-your-feedburner-subscribers/

Using the above code and naming it counter.php I can fetch the position from the database and create

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

This takes the positon of a website from the database ($array has already been made to fetch) and creates an image with the position nr.

Works fine, but once the postion changes based on user ratings, the image will not show the correct data.

I hope someone can help. Thank you.

Summery

Basically what I am try to make is something that will show the most recent data, based on the ranking of the website. Just like showing the number of twitter followers, for example http://twittercounter.com/counter/?username=labnol or feedburner followers on http://feeds.feedburner.com/~fc/labnol Both are images that show a number based on information in a database. But I want to create my own image, based on the rank of the website in the database.


回答1:


Looking at your code it should update everytime the page is reloaded. Clear your browser cache.

If this fails i would check from where it is getting the Get['count'] data which im assuming is the Rank number of the site.

Can you check that the Get['Count'] data is updating as it should ?

Im not sure using an ARRAY in the URL is a good idea, why not use Sessions ? This link might be of interest.

Sorry i have not been of more help.




回答2:


This is what I have so far (I cannot edit this question from this computer, due to different cookies).

This is based on the help from

How to fetch data from database and display it in PHP?

thanks to https://stackoverflow.com/users/353790/robertpitt

This seems to work

<?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);
?>


来源:https://stackoverflow.com/questions/6091010/php-create-embed-image-from-database-position

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!