Image appears "broken" after you changed the PHP version [closed]

0

I currently changed a machine application, so the bar code image no longer appears. The PHP version of this new machine is newer, I've tried changing the image call but nothing happens. How could I resolve?

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

Barcode (bar.php)

<?

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);
?>
    
asked by anonymous 02.07.2015 / 20:25

2 answers

2

There are three possible issues that may have caused the crash:

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

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

        <?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;
        }   
    
  • GD library is disabled.

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

    ;extension=php_gd2.dll
    

    Remove the ; from the beginning 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 in 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 retry the script.

  • "Debugging" script

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

    • Comment the header like this:

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

        link

        You will probably have some error, please report this error in your question or comment here.

    HTML and PHP hint

    Your php looks like this:

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

    When run it does this for example:

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

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

    • Use apostrophes for PHP and HTML pro tags
    • When using echo use , instead of points
    • Use html tags in minuscule

    It should look like this:

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

    These tips will not make a big difference, but will help make the code a little better to work with.

        
    20.09.2015 / 03:16
    -1

    Converted your method into a class, see if it helps:

    link

        
    21.10.2015 / 16:33