Image appears "broken" after you changed the PHP version

Asked

Viewed 271 times

0

currently changed a machine application, so the barcode image no longer appears. The PHP version of this new machine is newer, I tried to change the image call but nothing happens. How could I fix?

   <?php
      echo "<IMG src='barras.php?num=" .$codigobarras." '>";             
   ?>

Barcode (.php bars)

<?

Header("Content-type: image/jpeg");

$bf = 1;
$ab = 50;
if ($bf == "")
{
    $tamxt = 1;
    $tamxl = 3;
}
else
{
    $tamxt = $bf;
    $tamxl = ($bf * 2) + 1;
}   

if ($ab == "")
    $tamy = 50;
else
    $tamy = $ab;

$aux_len = strlen($num)%2;

if ($aux_len != 0)
    $aux_len = (strlen($num) + 1) / 2;
else
    $aux_len = strlen($num) / 2;

$aux_tamx = ((6 * $tamxt + 4 * $tamxl) * $aux_len) + (6 * $tamxt) + $tamxl;


$im = ImageCreate($aux_tamx, $tamy);

$white = ImageColorAllocate($im,255,255,255);
$black = ImageColorAllocate($im,0,0,0);

$x = 0;
$y = 0;

function add_wt()
{
    global $x, $y, $tamxt, $tamy, $white, $black, $im, $tamxl;
    ImageFilledRectangle($im, $x, $y, $x + $tamxt, $y + $tamy, $white);
    $x = $x + $tamxt;
}

function add_wl()
{
    global $x, $y, $tamxt, $tamy, $white, $black, $im, $tamxl;
    ImageFilledRectangle($im, $x, $y, $x + $tamxl, $y + $tamy, $white);
    $x = $x + $tamxl;
}

function add_bt()
{
    global $x, $y, $tamxt, $tamy, $white, $black, $im, $tamxl;
    ImageFilledRectangle($im, $x, $y, $x + $tamxt, $y + $tamy, $black);
    $x = $x + $tamxt;
}

function add_bl()
{
    global $x, $y, $tamxt, $tamy, $white, $black, $im, $tamxl;
    ImageFilledRectangle($im, $x, $y, $x + $tamxl, $y + $tamy, $black);
    $x = $x + $tamxl;   
}

function add_to_bar ($code)
{
    switch ($code)
    {
    case '00':
        add_bt();
        add_wt();
        add_bt();
        add_wt();
        add_bl();
        add_wl();
        add_bl();
        add_wl();
        add_bt();
        add_wt();
    break;

    case '01':
        add_bt();
        add_wl();
        add_bt();
        add_wt();
        add_bl();
        add_wt();
        add_bl();
        add_wt();
        add_bt();
        add_wl();
    break;

    case '02':
        add_bt();
        add_wt();
        add_bt();
        add_wl();
        add_bl();
        add_wt();
        add_bl();
        add_wt();
        add_bt();
        add_wl();
    break;

    case '03':
        add_bt();
        add_wl();
        add_bt();
        add_wl();
        add_bl();
        add_wt();
        add_bl();
        add_wt();
        add_bt();
        add_wt();
    break;

    case '04':
        add_bt();
        add_wt();
        add_bt();
        add_wt();
        add_bl();
        add_wl();
        add_bl();
        add_wt();
        add_bt();
        add_wl();
    break;

    case '05':
        add_bt();
        add_wl();
        add_bt();
        add_wt();
        add_bl();
        add_wl();
        add_bl();
        add_wt();
        add_bt();
        add_wt();
    break;

    case '06':
        add_bt();
        add_wt();
        add_bt();
        add_wl();
        add_bl();
        add_wl();
        add_bl();
        add_wt();
        add_bt();
        add_wt();
    break;

    case '07':
        add_bt();
        add_wt();
        add_bt();
        add_wt();
        add_bl();
        add_wt();
        add_bl();
        add_wl();
        add_bt();
        add_wl();
    break;

    case '08':
        add_bt();
        add_wl();
        add_bt();
        add_wt();
        add_bl();
        add_wt();
        add_bl();
        add_wl();
        add_bt();
        add_wt();
    break;

    case '09':
        add_bt();
        add_wt();
        add_bt();
        add_wl();
        add_bl();
        add_wt();
        add_bl();
        add_wl();
        add_bt();
        add_wt();
    break;

    case '10':
        add_bl();
        add_wt();
        add_bt();
        add_wt();
        add_bt();
        add_wl();
        add_bt();
        add_wl();
        add_bl();
        add_wt();
    break;

    case '11':
        add_bl();
        add_wl();
        add_bt();
        add_wt();
        add_bt();
        add_wt();
        add_bt();
        add_wt();
        add_bl();
        add_wl();
    break;

    case '12':
        add_bl();
        add_wt();
        add_bt();
        add_wl();
        add_bt();
        add_wt();
        add_bt();
        add_wt();
        add_bl();
        add_wl();
    break;

    case '13':
        add_bl();
        add_wl();
        add_bt();
        add_wl();
        add_bt();
        add_wt();
        add_bt();
        add_wt();
        add_bl();
        add_wt();
    break;

    case '14':
        add_bl();
        add_wt();
        add_bt();
        add_wt();
        add_bt();
        add_wl();
        add_bt();
        add_wt();
        add_bl();
        add_wl();
    break;

    case '15':
        add_bl();
        add_wl();
        add_bt();
        add_wt();
        add_bt();
        add_wl();
        add_bt();
        add_wt();
        add_bl();
        add_wt();
    break;

    case '16':
        add_bl();
        add_wt();
        add_bt();
        add_wl();
        add_bt();
        add_wl();
        add_bt();
        add_wt();
        add_bl();
        add_wt();
    break;

    case '17':
        add_bl();
        add_wt();
        add_bt();
        add_wt();
        add_bt();
        add_wt();
        add_bt();
        add_wl();
        add_bl();
        add_wl();
    break;

    case '18':
        add_bl();
        add_wl();
        add_bt();
        add_wt();
        add_bt();
        add_wt();
        add_bt();
        add_wl();
        add_bl();
        add_wt();
    break;

    case '19':
        add_bl();
        add_wt();
        add_bt();
        add_wl();
        add_bt();
        add_wt();
        add_bt();
        add_wl();
        add_bl();
        add_wt();
    break;

    case '20':
        add_bt();
        add_wt();
        add_bl();
        add_wt();
        add_bt();
        add_wl();
        add_bt();
        add_wl();
        add_bl();
        add_wt();
    break;

    case '21':
        add_bt();
        add_wl();
        add_bl();
        add_wt();
        add_bt();
        add_wt();
        add_bt();
        add_wt();
        add_bl();
        add_wl();
    break;

    case '22':
        add_bt();
        add_wt();
        add_bl();
        add_wl();
        add_bt();
        add_wt();
        add_bt();
        add_wt();
        add_bl();
        add_wl();
    break;

    case '23':
        add_bt();
        add_wl();
        add_bl();
        add_wl();
        add_bt();
        add_wt();
        add_bt();
        add_wt();
        add_bl();
        add_wt();
    break;

    case '24':
        add_bt();
        add_wt();
        add_bl();
        add_wt();
        add_bt();
        add_wl();
        add_bt();
        add_wt();
        add_bl();
        add_wl();
    break;

    case '25':
        add_bt();
        add_wl();
        add_bl();
        add_wt();
        add_bt();
        add_wl();
        add_bt();
        add_wt();
        add_bl();
        add_wt();
    break;

    case '26':
        add_bt();
        add_wt();
        add_bl();
        add_wl();
        add_bt();
        add_wl();
        add_bt();
        add_wt();
        add_bl();
        add_wt();
    break;

    case '27':
        add_bt();
        add_wt();
        add_bl();
        add_wt();
        add_bt();
        add_wt();
        add_bt();
        add_wl();
        add_bl();
        add_wl();
    break;

    case '28':
        add_bt();
        add_wl();
        add_bl();
        add_wt();
        add_bt();
        add_wt();
        add_bt();
        add_wl();
        add_bl();
        add_wt();
    break;

    case '29':
        add_bt();
        add_wt();
        add_bl();
        add_wl();
        add_bt();
        add_wt();
        add_bt();
        add_wl();
        add_bl();
        add_wt();
    break;

    case '30':
        add_bl();
        add_wt();
        add_bl();
        add_wt();
        add_bt();
        add_wl();
        add_bt();
        add_wl();
        add_bt();
        add_wt();
    break;

    case '31':
        add_bl();
        add_wl();
        add_bl();
        add_wt();
        add_bt();
        add_wt();
        add_bt();
        add_wt();
        add_bt();
        add_wl();
    break;

    case '32':
        add_bl();
        add_wt();
        add_bl();
        add_wl();
        add_bt();
        add_wt();
        add_bt();
        add_wt();
        add_bt();
        add_wl();
    break;

    case '33':
        add_bl();
        add_wl();
        add_bl();
        add_wl();
        add_bt();
        add_wt();
        add_bt();
        add_wt();
        add_bt();
        add_wt();
    break;

    case '34':
        add_bl();
        add_wt();
        add_bl();
        add_wt();
        add_bt();
        add_wl();
        add_bt();
        add_wt();
        add_bt();
        add_wl();
    break;

    case '35':
        add_bl();
        add_wl();
        add_bl();
        add_wt();
        add_bt();
        add_wl();
        add_bt();
        add_wt();
        add_bt();
        add_wt();
    break;

    case '36':
        add_bl();
        add_wt();
        add_bl();
        add_wl();
        add_bt();
        add_wl();
        add_bt();
        add_wt();
        add_bt();
        add_wt();
    break;

    case '37':
        add_bl();
        add_wt();
        add_bl();
        add_wt();
        add_bt();
        add_wt();
        add_bt();
        add_wl();
        add_bt();
        add_wl();
    break;

    case '38':
        add_bl();
        add_wl();
        add_bl();
        add_wt();
        add_bt();
        add_wt();
        add_bt();
        add_wl();
        add_bt();
        add_wt();
    break;

    case '39':
        add_bl();
        add_wt();
        add_bl();
        add_wl();
        add_bt();
        add_wt();
        add_bt();
        add_wl();
        add_bt();
        add_wt();
    break;

    case '40':
        add_bt();
        add_wt();
        add_bt();
        add_wt();
        add_bl();
        add_wl();
        add_bt();
        add_wl();
        add_bl();
        add_wt();
    break;

    case '41':
        add_bt();
        add_wl();
        add_bt();
        add_wt();
        add_bl();
        add_wt();
        add_bt();
        add_wt();
        add_bl();
        add_wl();
    break;

    case '42':
        add_bt();
        add_wt();
        add_bt();
        add_wl();
        add_bl();
        add_wt();
        add_bt();
        add_wt();
        add_bl();
        add_wl();
    break;

    case '43':
        add_bt();
        add_wl();
        add_bt();
        add_wl();
        add_bl();
        add_wt();
        add_bt();
        add_wt();
        add_bl();
        add_wt();
    break;

    case '44':
        add_bt();
        add_wt();
        add_bt();
        add_wt();
        add_bl();
        add_wl();
        add_bt();
        add_wt();
        add_bl();
        add_wl();
    break;

    case '45':
        add_bt();
        add_wl();
        add_bt();
        add_wt();
        add_bl();
        add_wl();
        add_bt();
        add_wt();
        add_bl();
        add_wt();
    break;

    case '46':
        add_bt();
        add_wt();
        add_bt();
        add_wl();
        add_bl();
        add_wl();
        add_bt();
        add_wt();
        add_bl();
        add_wt();
    break;

    case '47':
        add_bt();
        add_wt();
        add_bt();
        add_wt();
        add_bl();
        add_wt();
        add_bt();
        add_wl();
        add_bl();
        add_wl();
    break;

    case '48':
        add_bt();
        add_wl();
        add_bt();
        add_wt();
        add_bl();
        add_wt();
        add_bt();
        add_wl();
        add_bl();
        add_wt();
    break;

    case '49':
        add_bt();
        add_wt();
        add_bt();
        add_wl();
        add_bl();
        add_wt();
        add_bt();
        add_wl();
        add_bl();
        add_wt();
    break;

    case '50':
        add_bl();
        add_wt();
        add_bt();
        add_wt();
        add_bl();
        add_wl();
        add_bt();
        add_wl();
        add_bt();
        add_wt();
    break;

    case '51':
        add_bl();
        add_wl();
        add_bt();
        add_wt();
        add_bl();
        add_wt();
        add_bt();
        add_wt();
        add_bt();
        add_wl();
    break;

    case '52':
        add_bl();
        add_wt();
        add_bt();
        add_wl();
        add_bl();
        add_wt();
        add_bt();
        add_wt();
        add_bt();
        add_wl();
    break;

    case '53':
        add_bl();
        add_wl();
        add_bt();
        add_wl();
        add_bl();
        add_wt();
        add_bt();
        add_wt();
        add_bt();
        add_wt();
    break;

    case '54':
        add_bl();
        add_wt();
        add_bt();
        add_wt();
        add_bl();
        add_wl();
        add_bt();
        add_wt();
        add_bt();
        add_wl();
    break;

    case '55':
        add_bl();
        add_wl();
        add_bt();
        add_wt();
        add_bl();
        add_wl();
        add_bt();
        add_wt();
        add_bt();
        add_wt();
    break;

    case '56':
        add_bl();
        add_wt();
        add_bt();
        add_wl();
        add_bl();
        add_wl();
        add_bt();
        add_wt();
        add_bt();
        add_wt();
    break;

    case '57':
        add_bl();
        add_wt();
        add_bt();
        add_wt();
        add_bl();
        add_wt();
        add_bt();
        add_wl();
        add_bt();
        add_wl();
    break;

    case '58':
        add_bl();
        add_wl();
        add_bt();
        add_wt();
        add_bl();
        add_wt();
        add_bt();
        add_wl();
        add_bt();
        add_wt();
    break;

    case '59':
        add_bl();
        add_wt();
        add_bt();
        add_wl();
        add_bl();
        add_wt();
        add_bt();
        add_wl();
        add_bt();
        add_wt();
    break;

    case '60':
        add_bt();
        add_wt();
        add_bl();
        add_wt();
        add_bl();
        add_wl();
        add_bt();
        add_wl();
        add_bt();
        add_wt();
    break;

    case '61':
        add_bt();
        add_wl();
        add_bl();
        add_wt();
        add_bl();
        add_wt();
        add_bt();
        add_wt();
        add_bt();
        add_wl();
    break;

    case '62':
        add_bt();
        add_wt();
        add_bl();
        add_wl();
        add_bl();
        add_wt();
        add_bt();
        add_wt();
        add_bt();
        add_wl();
    break;

    case '63':
        add_bt();
        add_wl();
        add_bl();
        add_wl();
        add_bl();
        add_wt();
        add_bt();
        add_wt();
        add_bt();
        add_wt();
    break;

    case '64':
        add_bt();
        add_wt();
        add_bl();
        add_wt();
        add_bl();
        add_wl();
        add_bt();
        add_wt();
        add_bt();
        add_wl();
    break;

    case '65':
        add_bt();
        add_wl();
        add_bl();
        add_wt();
        add_bl();
        add_wl();
        add_bt();
        add_wt();
        add_bt();
        add_wt();
    break;

    case '66':
        add_bt();
        add_wt();
        add_bl();
        add_wl();
        add_bl();
        add_wl();
        add_bt();
        add_wt();
        add_bt();
        add_wt();
    break;

    case '67':
        add_bt();
        add_wt();
        add_bl();
        add_wt();
        add_bl();
        add_wt();
        add_bt();
        add_wl();
        add_bt();
        add_wl();
    break;

    case '68':
        add_bt();
        add_wl();
        add_bl();
        add_wt();
        add_bl();
        add_wt();
        add_bt();
        add_wl();
        add_bt();
        add_wt();
    break;

    case '69':
        add_bt();
        add_wt();
        add_bl();
        add_wl();
        add_bl();
        add_wt();
        add_bt();
        add_wl();
        add_bt();
        add_wt();
    break;

    case '70':
        add_bt();
        add_wt();
        add_bt();
        add_wt();
        add_bt();
        add_wl();
        add_bl();
        add_wl();
        add_bl();
        add_wt();
    break;

    case '71':
        add_bt();
        add_wl();
        add_bt();
        add_wt();
        add_bt();
        add_wt();
        add_bl();
        add_wt();
        add_bl();
        add_wl();
    break;

    case '72':
        add_bt();
        add_wt();
        add_bt();
        add_wl();
        add_bt();
        add_wt();
        add_bl();
        add_wt();
        add_bl();
        add_wl();
    break;

    case '73':
        add_bt();
        add_wl();
        add_bt();
        add_wl();
        add_bt();
        add_wt();
        add_bl();
        add_wt();
        add_bl();
        add_wt();
    break;

    case '74':
        add_bt();
        add_wt();
        add_bt();
        add_wt();
        add_bt();
        add_wl();
        add_bl();
        add_wt();
        add_bl();
        add_wl();
    break;

    case '75':
        add_bt();
        add_wl();
        add_bt();
        add_wt();
        add_bt();
        add_wl();
        add_bl();
        add_wt();
        add_bl();
        add_wt();
    break;

    case '76':
        add_bt();
        add_wt();
        add_bt();
        add_wl();
        add_bt();
        add_wl();
        add_bl();
        add_wt();
        add_bl();
        add_wt();
    break;

    case '77':
        add_bt();
        add_wt();
        add_bt();
        add_wt();
        add_bt();
        add_wt();
        add_bl();
        add_wl();
        add_bl();
        add_wl();
    break;

    case '78':
        add_bt();
        add_wl();
        add_bt();
        add_wt();
        add_bt();
        add_wt();
        add_bl();
        add_wl();
        add_bl();
        add_wt();
    break;

    case '79':
        add_bt();
        add_wt();
        add_bt();
        add_wl();
        add_bt();
        add_wt();
        add_bl();
        add_wl();
        add_bl();
        add_wt();
    break;

    case '80':
        add_bl();
        add_wt();
        add_bt();
        add_wt();
        add_bt();
        add_wl();
        add_bl();
        add_wl();
        add_bt();
        add_wt();
    break;

    case '81':
        add_bl();
        add_wl();
        add_bt();
        add_wt();
        add_bt();
        add_wt();
        add_bl();
        add_wt();
        add_bt();
        add_wl();
    break;

    case '82':
        add_bl();
        add_wt();
        add_bt();
        add_wl();
        add_bt();
        add_wt();
        add_bl();
        add_wt();
        add_bt();
        add_wl();
    break;

    case '83':
        add_bl();
        add_wl();
        add_bt();
        add_wl();
        add_bt();
        add_wt();
        add_bl();
        add_wt();
        add_bt();
        add_wt();
    break;

    case '84':
        add_bl();
        add_wt();
        add_bt();
        add_wt();
        add_bt();
        add_wl();
        add_bl();
        add_wt();
        add_bt();
        add_wl();
    break;

    case '85':
        add_bl();
        add_wl();
        add_bt();
        add_wt();
        add_bt();
        add_wl();
        add_bl();
        add_wt();
        add_bt();
        add_wt();
    break;

    case '86':
        add_bl();
        add_wt();
        add_bt();
        add_wl();
        add_bt();
        add_wl();
        add_bl();
        add_wt();
        add_bt();
        add_wt();
    break;

    case '87':
        add_bl();
        add_wt();
        add_bt();
        add_wt();
        add_bt();
        add_wt();
        add_bl();
        add_wl();
        add_bt();
        add_wl();
    break;

    case '88':
        add_bl();
        add_wl();
        add_bt();
        add_wt();
        add_bt();
        add_wt();
        add_bl();
        add_wl();
        add_bt();
        add_wt();
    break;

    case '89':
        add_bl();
        add_wt();
        add_bt();
        add_wl();
        add_bt();
        add_wt();
        add_bl();
        add_wl();
        add_bt();
        add_wt();
    break;

    case '90':
        add_bt();
        add_wt();
        add_bl();
        add_wt();
        add_bt();
        add_wl();
        add_bl();
        add_wl();
        add_bt();
        add_wt();
    break;

    case '91':
        add_bt();
        add_wl();
        add_bl();
        add_wt();
        add_bt();
        add_wt();
        add_bl();
        add_wt();
        add_bt();
        add_wl();
    break;

    case '92':
        add_bt();
        add_wt();
        add_bl();
        add_wl();
        add_bt();
        add_wt();
        add_bl();
        add_wt();
        add_bt();
        add_wl();
    break;

    case '93':
        add_bt();
        add_wl();
        add_bl();
        add_wl();
        add_bt();
        add_wt();
        add_bl();
        add_wt();
        add_bt();
        add_wt();
    break;

    case '94':
        add_bt();
        add_wt();
        add_bl();
        add_wt();
        add_bt();
        add_wl();
        add_bl();
        add_wt();
        add_bt();
        add_wl();
    break;

    case '95':
        add_bt();
        add_wl();
        add_bl();
        add_wt();
        add_bt();
        add_wl();
        add_bl();
        add_wt();
        add_bt();
        add_wt();
    break;

    case '96':
        add_bt();
        add_wt();
        add_bl();
        add_wl();
        add_bt();
        add_wl();
        add_bl();
        add_wt();
        add_bt();
        add_wt();
    break;

    case '97':
        add_bt();
        add_wt();
        add_bl();
        add_wt();
        add_bt();
        add_wt();
        add_bl();
        add_wl();
        add_bt();
        add_wl();
    break;

    case '98':
        add_bt();
        add_wl();
        add_bl();
        add_wt();
        add_bt();
        add_wt();
        add_bl();
        add_wl();
        add_bt();
        add_wt();
    break;

    case '99':
        add_bt();
        add_wt();
        add_bl();
        add_wl();
        add_bt();
        add_wt();
        add_bl();
        add_wl();
        add_bt();
        add_wt();
    break;

    case '100':     /* start */
        add_bt();
        add_wt();
        add_bt();
        add_wt();
    break;

    case '101':     /* stop */
        add_bl();
        add_wt();
        add_bt();
        add_wl();
    break;
    }
}

add_to_bar("100");      /* Start */

for ($i = 0; $i < strlen ($num); $i = $i + 2)
{
    $cod = substr ($num, $i, 2);
    add_to_bar ($cod);
}

add_to_bar("101");      /* Stop */

//ImagePNG($im); 
ImageJpeg($im); 
imagedestroy($im);
?>

2 answers

2

There are three possible problems that may have caused the failure:

  1. You used <? instead of <?php, recommend that you always use <?php.
  2. You used $num instead of $_GET['num']

    To solve this problem, add at the beginning of the script this:

        <?php
        $num = $_GET['num'];
    
        Header("Content-type: image/jpeg");
    
        $bf = 1;
        $ab = 50;
        if ($bf == "")
        {
           $tamxt = 1;
           $tamxl = 3;
        }
        else
        {
           $tamxt = $bf;
           $tamxl = ($bf * 2) + 1;
        }   
    
  3. GD library is disabled.

    The GD library is responsible for the imagecreate, imagecolorallocate, etc. To activate it you will need to configure the php.ini, look in php.ini for a line similar to is:

    ;extension=php_gd2.dll
    

    Remove the ; start and save the file, if this line does not exist then add this to the file:

    extension=php_gd2.dll
    

    If you are using a Debian or Ubuntu distribution (linux servers) then you should also run this line on the terminal:

    $ sudo apt-get install php5-gd
    

    (However I believe that GD is already installed, since it previously worked).

    After this restart the server and re-test the script.

"Debugging" the script

If none of this works, then you can try to test your file because it may be a problem on your server that only you can detect, to "debug" your script do the following:

  • Comment the header thus:

        <?php
        $num = $_GET['num'];
    
        //Header("Content-type: image/jpeg");
    
    • Open your file directly in the web-browser/browser without using the <img>, something like:

      http://localhost/project/bar.php? num=1

      You will probably get an error, enter this error in your question or comment here.

Hint about HTML and PHP

Your php is like this:

echo "<IMG src='barras.php?num=" .$codigobarras." '>";

When executed it processes this for example:

<IMG src='barras.php?num=5 '>

Note that there was a gap at the end, although it does not affect your script, this does not seem right. I also recommend:

  • Use apostrophes for PHP and quotation marks for HTML
  • When to use echo use , instead of points
  • Use html tags in minusculo

Should stay like this:

echo '<img src="barras.php?num=', $codigobarras, '">';

These tips won’t make much difference, but help make the code a little better to work with.

  • Your answer is perfect, I would give +1 if I could. But actually there are many more possible mistakes besides these.

  • 1

    @Ivanferrer thanks, I believe that yes there are more problems, but they seem to me more focused on how the script was written, my answer is not exact, but tries to give an idea of how to activate everything "necessary" and still debug the code, however if you noticed anything else regarding html configuration or syntax error please provide us with a more complete answer (you can be sure you already have +1 on my part) :) See more

-1

Browser other questions tagged

You are not signed in. Login or sign up in order to post.