PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, October 10, 2022

[FIXED] How to add WP variables to GD imagepng

 October 10, 2022     gd, php, php-gd, wordpress     No comments   

Issue

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


Solution

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' );


Answered By - Duttch
Answer Checked By - Mary Flores (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing