Information graphical visualization for web-page: Is there any better way instead of small PNG files?

后端 未结 3 812
被撕碎了的回忆
被撕碎了的回忆 2021-01-25 05:20

Let\'s describe the task first:

I\'d like to create a web-page with several rows of a text and a small (let\'s say 100 by 20 pixels) graphic for each one. Each graphic g

相关标签:
3条回答
  • 2021-01-25 05:58

    Well if you want to avoid "unnecessary disk access" and since the image size is small you can use Base64 encoding. You can store the encoded strings in the database as well.

    This way <img src='row1graph.png'> becomes...

    <img src="data:image/jpg;base64,/9j/4AAQSkZJRgABAQEAYABgAAD//gA8Q1JFQVRPUjogZ2QtanBlZyB2MS4wICh1c2luZyBJSkcgSlBFRyB2ODApLCBxdWFsaXR5ID0gMTAwCv/bAEMAAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAf/bAEMBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAf/AABEIABQAZAMBIgACEQEDEQH/xAAfAAABBQEBAQEBAQAAAAAAAAAAAQIDBAUGBwgJCgv/xAC1EAACAQMDAgQDBQUEBAAAAX0BAgMABBEFEiExQQYTUWEHInEUMoGRoQgjQrHBFVLR8CQzYnKCCQoWFxgZGiUmJygpKjQ1Njc4OTpDREVGR0hJSlNUVVZXWFlaY2RlZmdoaWpzdHV2d3h5eoOEhYaHiImKkpOUlZaXmJmaoqOkpaanqKmqsrO0tba3uLm6wsPExcbHyMnK0tPU1dbX2Nna4eLj5OXm5+jp6vHy8/T19vf4+fr/xAAfAQADAQEBAQEBAQEBAAAAAAAAAQIDBAUGBwgJCgv/xAC1EQACAQIEBAMEBwUEBAABAncAAQIDEQQFITEGEkFRB2FxEyIygQgUQpGhscEJIzNS8BVictEKFiQ04SXxFxgZGiYnKCkqNTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqCg4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k="/>

    0 讨论(0)
  • 2021-01-25 06:01

    There's two ways to optimize small graphics in a web page. You can use the tile-set concept (all the graphics in a single image, with offsets based on e.g. background-position), or you can embed the PNG directly into the page as base-64:

    <img alt="Embedded Image" src="data:image/png;base64,yourbase64image" />
    

    The options have different merits based on file size and browser support. And of course, both can be combined - you can have an embedded tile-set.

    0 讨论(0)
  • 2021-01-25 06:05

    You can serve an image from PHP rather than from a file - I mean you can have PHP dynamically create an image and serve it rather than having to have a file in your webserver's filesystem and having to refer to it by name in the src field of an tag in your HTML.

    So, instead of

    <image src="xyz.png" alt="..." size="...">
    

    you can use

    <img src="/php/thumb.php?param1=128&param2=45"/>
    

    which causes the PHP script at /php/thumb.php to be called when the page is rendered. In that script, you can dynamically create the image (using extra parameters if you wish) like this:

    <?php    
    header("Content-type: image/png");
    $p1 = $_GET['param1'];
    $p2 = $_GET['param2'];
    
    // Create a 200x200 image
    $im    = imagecreatetruecolor(200,200);
    $white = imagecolorallocate($im, 255, 255, 255);
    $red   = imagecolorallocate($im, 255, 0, 0);
    
    // Here you can draw in the image, write on it, set any pixels, calculate any colours you wish
    
    // Draw a white rectangle, then a red one
    imagefilledrectangle($im, 50, 50, 100, 100, $white);
    imagefilledrectangle($im, 75, 75, 150, 150, $red);
    
    imagepng($im);
    imagedestroy($im);
    ?>
    

    I have omitted some code after the first 3 lines so you just see the technique rather than all the gory details of my code. The actual lines you are interested in are:

    header(...image/png);
    

    which tells the browser what type of stuff is coming - i.e. an image, and

    imagepng();
    

    which actually sends the stream of PNG data to the browser.

    The code above produces this in the browser:

    enter image description here

    0 讨论(0)
提交回复
热议问题