NetAdmin1
NetAdmin1

Reputation: 23

Set width and height of image in php

I migrated a static site to WordPress, I created a custom PHP template and on the old site I was able to set the image width and height in PHP, now it doesn't work no matter what I change.

Can someone help me fix my code?

My goal is to create a bar chart using the variable as the width of the image.

for($n=$lYear; $n >= $fYear; $n--){
    $pCent = $avg[$n]/$hAvg;
    print("<tr>");
    print("<td>$n</td>");
    print("<td>&nbsp;</td>");
    $avgString = sprintf("%01.2f", $avg[$n]);
    print("<td align=right>$avgString</td>");
    print("<td align=center>&nbsp;</td>");
    $pixW = ($pCent*516)-16;

    echo('<td><img src="../dev2016/wp-content/uploads/2017/01/redbar.gif" 
                                               width="$pixW" height"16" />');

Upvotes: 0

Views: 1513

Answers (2)

user4759415
user4759415

Reputation:

Your PHP code doesn't realize that there is a variable being called in your html. You need to escape the quotes so that the variable gets treated like a variable and not another piece of text.

echo('<td><img src="../dev2016/wp-content/uploads/2017/01/redbar.gif" width="' .$pixW. '" height"16" />');

No offence intended but here is a link to W3 that illustrates the point

http://www.w3schools.com/php/showphp.asp?filename=demo_echo2

Edit to reflect comment

You need to have a space between the single quotes and your variable. With your height it's in there as height="'.$pixH. '" but it needs to be height="' .$pixH. '"

Upvotes: 0

Raj Vimal Chopra
Raj Vimal Chopra

Reputation: 56

Issue is with enclosing echo content in single quotes. If you want to use a variable, always enclose in double quotes.

Try this
echo("<td><img src='../dev2016/wp-content/uploads/2017/01/redbar.gif' width='$pixW' height='16' />");

Upvotes: 1

Related Questions