【问题标题】:PHP create embed image from database positionPHP从数据库位置创建嵌入图像
【发布时间】:2016-12-20 20:39:17
【问题描述】:

我搜索了 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 多个其他网站这样做,但我不知道如何。

我发现了这个伪造 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 我可以从数据库中获取位置并创建

&lt;img src='http://www.example.com/counter.php?count=".$array ['position']."' /&gt;

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

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

我希望有人可以提供帮助。谢谢。

夏天

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

【问题讨论】:

    标签: php image dynamic


    【解决方案1】:

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

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

    您能否检查 Get['Count'] 数据是否按应有的方式更新?

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

    抱歉,我没有提供更多帮助。

    【讨论】:

    • 在这种情况下应该添加而不是从目录中随机获取图像,您将从数据库中选择它(如示例代码所示)Gif、Jpeg、Jpg、Png 但您将始终使用此方法输出 PNG 作为标题(“Content-type: image/png”);设置为 PNG。
    • 感谢您的帮助。我不明白显示随机图像与问题有什么关系。我添加了其他信息以清除我的问题。
    • 再次感谢您,但我仍然认为我没有很好地解释它。基本上我要做的是根据网站的排名显示最新数据。就像显示 Twitter 关注者的数量一样,例如 twittercounter.com/counter/?username=labnol 或 feeds.feedburner.com/~fc/labnol 两者都是根据数据库中的信息显示数字的图像。但我想根据网站在数据库中的排名创建自己的图像。
    【解决方案2】:

    这是我目前所拥有的(由于 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);
    ?>
    

    【讨论】:

      猜你喜欢
      • 2014-02-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-03
      • 1970-01-01
      • 1970-01-01
      • 2016-11-21
      • 1970-01-01
      相关资源
      最近更新 更多