【问题标题】:PHP Swatch download, only generating 2 colours?PHP Swatch 下载,只生成 2 种颜色?
【发布时间】:2014-04-03 16:26:38
【问题描述】:

我一直致力于使用 PHP 将样本保存为图像,我在这方面取得了先机,到目前为止它保存了第一种颜色、最后一种颜色和黑色 - 但将最后一种颜色迭代三个块。

<?php

$colours = $_GET['c'];
$swatches = explode("|", $colours);


// Create image
$im = imagecreatetruecolor(120, 30);


foreach ($swatches as $key => $rgb_set) 
{
if ($rgb_set=="") break;
list($r, $g, $b) = explode(",", $rgb_set);

$x_pos = (24 * $key);

$swatch = imagecolorallocate($im, $r, $g, $b);
imagefilledrectangle($im, $x_pos, 0, 24, 30, $swatch);
}


// Set the content type header
header('Content-Type: image/png');

// Save the image
imagepng($im);
imagedestroy($im);

有谁知道我做错了什么或如何解决这个问题,以便它从调色板下载所有 5 个色板?

【问题讨论】:

    标签: php html css gd gdlib


    【解决方案1】:

    由于您的 imagefilledrectangle 函数调用中硬编码的 24 值,该问题似乎发生了(至少部分地)

    而不是这个(注意24

    imagefilledrectangle($im, $x_pos, 0, 24, 30, $swatch);

    这个:

    imagefilledrectangle($im, $x_pos, 0, $x_pos+24, 30, $swatch);

    这样imagefilledrectangle$x2 值会随着您在原始代码中的$x1 值继续移动。

    完整代码示例

    <?php
    $colours = $_GET['c'];
    $swatches = explode("|", $colours);
    
    // Create image
    $im = imagecreatetruecolor(120, 30);
    
    foreach ($swatches as $key => $rgb_set)
    {
        if ($rgb_set == "") break;
        list($r, $g, $b) = explode(",", $rgb_set);
    
        $x_pos = (24 * $key);
    
        $swatch = imagecolorallocate($im, $r, $g, $b);
        imagefilledrectangle($im, $x_pos, 0, $x_pos+24, 30, $swatch);
    }
    
    // Set the content type header
    header('Content-Type: image/png');
    
    // Save the image
    imagepng($im);
    imagedestroy($im);
    ?>
    

    注意: 这已使用以下 URL 查询字符串进行了测试: ?c=255,255,0|255,155,25|13,103,100|54,123,53|255,0,0

    【讨论】:

    • @thesarahjay 很高兴听到这个消息;希望您项目的其余部分一切顺利!
    猜你喜欢
    • 2011-02-26
    • 2013-07-06
    • 1970-01-01
    • 2021-07-20
    • 1970-01-01
    • 1970-01-01
    • 2013-06-21
    • 2018-05-24
    • 2013-04-02
    相关资源
    最近更新 更多