Columns.render calling a PHP function?

Columns.render calling a PHP function?

somma_mattsomma_matt Posts: 20Questions: 5Answers: 0
edited October 2016 in Free community support

What's the best way to go about this. I've searched the forums and web and have come up short.

I have raw data being parsed as a CSV file and it comes in just fine. I have images in a seperate folder. I want to use columns.render to write the <img> tag based on the row data.

I want to include a php function in the img src to convert the image to base64 formatting.

Here is my code

    $('#datasource').DataTable( {
    data: results.data,
    columns: [
        { title: "Style", defaultContent: ""  },
        { title: "Color", defaultContent: "" },
        { title: "Style Desc", defaultContent: "" },
        { title: "Color Desc", defaultContent: "" },
        { title: "XS", type: "num", defaultContent: "" },
        { title: "S", type: "num", defaultContent: "" },
        { title: "M", type: "num", efaultContent: "" },
        { title: "L", type: "num", defaultContent: "" },
        { title: "X", type: "num", defaultContent: "" },
        { title: "XX", type: "num", defaultContent: "" },
        { title: "XXXL", type: "num", defaultContent: "" },
        { title: "4XL", type: "num", defaultContent: "" },  
        { title: "5XL", type: "num", defaultContent: "" },  
        { title: "Grand Total", type: "num", defaultContent: "" },  
        { title: "ImageSrc", orderable: false, data: null, render: 
            function ( data, type, row) { 
            return '<img src="<?php echo base64_encode_image ('images/' + row[0] + '_NEU_BLANK_' + row[1] +  '_MF.low.res.jpeg','jpeg'); ?>"/>'

}

Here is the PHP

<?php
function base64_encode_image ($filename=string,$filetype=string) {
if ($filename) {
$imgbinary = fread(fopen($filename, "r"), filesize($filename));
return 'data:image/' . $filetype . ';base64,' . base64_encode($imgbinary);
}
}

<?php > ?>

Answers

  • allanallan Posts: 63,818Questions: 1Answers: 10,517 Site admin

    You can't do that I'm afraid. There are three options I can think of:

    1. Do the base64 encode in Javascript (there are several libraries that will do that)
    2. Alter the data that has been loaded to contain the base64 string already
    3. Make an <img> tag that will load the image from a PHP script (i.e. one request for each image).

    Allan

This discussion has been closed.