简体   繁体   中英

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

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

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. '"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM