PHP从数据库位置创建嵌入图像 [英] PHP create embed image from database position

查看:101
本文介绍了PHP从数据库位置创建嵌入图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



我创建了一个前20名的列表,人们可以对自己喜欢的网站进行排名(我知道这不是原始的,但是我这样做学习php)



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



我不清楚的是如何执行以下操作。



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



< 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 
//将生成的图像发送到浏览器
create_image();
exit();
函数create_image(){
//创建图像资源
$ image = imagecreatefromgif('image.gif');
//创建文本颜色
$ brown = ImageColorAllocate($ image,0,0,0);
//检查get参数
if(isset($ _ GET ['count'])&& is_numeric($ _ GET ['count'])
$ rank = $ _GET [ '计数'];
else
$ rank = 20;

//某些对齐计算
$ bbox = imagettfbbox(8.5,1,'verdana.ttf',$ rank);
$ xcorr = 0 + $ bbox [2]; $ xcorr = 31 - $ xcorr;
//将数字以棕褐色的形式添加到图像
imagettftext($ image,8.5,0,$ xcorr,16,$ brown,'verdana.ttf',$ rank);
//告诉浏览器
标题(Content-Type:image / gif)中有什么文件?
imagegif($ image);
//释放资源
ImageDestroy($ image);}?>

根据
www.mygeekpal.com/how-to-fake-your-feedburner -subscribers /



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

$ c>



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



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

我希望有人可以帮忙。谢谢。



Summery



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

解决方案

这是我所拥有的到目前为止(由于不同的cookies,我无法从这台电脑编辑这个问题)。



这是基于



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



感谢
https://stackoverflow.com/users/353790/robertpitt



这似乎工作

 <?php 
//连接到DB
$ db = mysql_connect(localhst,user,pass)或die(Database Error);
mysql_select_db(db_name,$ db);

//从请求获取ID
$ id = isset($ _ GET ['id'])? (int)$ _ GET ['id']:0;

//检查ID是否有效
if($ id> 0)
{
//查询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);
?>


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.

解决方案

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

这篇关于PHP从数据库位置创建嵌入图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆