1

I'm trying to add a logged in user ID (wordpress) to a GD image.

When I use a constant it displays the png correctly, when calling a variable it shows a broken image link.

The call is done through a shortcode calling:

/* ================== GET PIC USER ================== */
function get_pic_user($atts) {
$myphp_folder = get_stylesheet_directory_uri() . '/make-pic.php';
$html = '<img src="'.$myphp_folder.'" alt="Image created by a PHP script" width="1000" height="768" align="middle">';
return $html;
}
add_shortcode( 'display_get_pic_user', 'get_pic_user' );

The php behind the make-pic.php is:

<?php
$user = wp_get_current_user();
$string_user = $user->id;
$im = imagecreatefrompng("../my_user_pic.png");
header("Content-type: image/png");
$color = imagecolorallocate($im, 51, 51, 51);
imagestring($im, 21, 500, 350, $string_user, $color);
imagepng($im);
imagedestroy($im);
exit;

So this works:

<?php
$string_user = 15;
$im = imagecreatefrompng("../my_user_pic.png");

and this doesn't:

<?php
$user = wp_get_current_user();
$string_user = $user->id;
$im = imagecreatefrompng("../my_user_pic.png");

Any suggestions would be appreciated.

Thank you

Duttch
  • 21
  • 3
  • On debugging the issue further, I've notice the following: Its giving a FATAL ERROR: Uncaught Error: Call to undefined function wp_get_curent_user() in ..... – Duttch Nov 20 '18 at 20:03

1 Answers1

0

After adding several REQUIRE and other solutions issues still manifested. A working solution that I came up with was to add the IMAGEPNG just inline with code, however using a OB_START / OB_GET_CLEAN. The following works:

The image is still called from the page with a shortcode [display_get_pic_user]. The code behind it is:

function get_pic_user() {
    $user = wp_get_current_user();
    $string_user = $user->id;
    $im = imagecreatefrompng("../my_user_pic.png");
    $color = imagecolorallocate($im, 51, 51, 51);
    imagestring($im, 21, 500, 350, $string_user, $color);
    ob_start(); 
        imagepng($im);
    $rawImageBytes = ob_get_clean();
    $html = "<img src='data:image/png;base64," . base64_encode( $rawImageBytes ) . "' align=middle/>";
    imagedestroy($im);
    return $html;
}
add_shortcode( 'display_get_pic_user', 'get_pic_user' );
Duttch
  • 21
  • 3