Foros del Web » Programando para Internet » PHP »

[SOLUCIONADO] Invocar varias veces un objeto que devulve una imagen

Estas en el tema de Invocar varias veces un objeto que devulve una imagen en el foro de PHP en Foros del Web. Hola, tengo problemas al invocar mas de una vez un objeto de una clase php. Cuando lo invoco una vez se impreme el valor que ...
  #1 (permalink)  
Antiguo 13/05/2013, 19:05
 
Fecha de Ingreso: noviembre-2008
Ubicación: lima
Mensajes: 148
Antigüedad: 15 años, 4 meses
Puntos: 0
Invocar varias veces un objeto que devulve una imagen

Hola, tengo problemas al invocar mas de una vez un objeto de una clase php.
Cuando lo invoco una vez se impreme el valor que le envío al constructor, pero si creo otro objeto (de la misma clase) no me muestra el resultado o peor aún, después de imprimir el primer objeto ya no me muestra ni siquiera un echo.

Script que invoca a la clase: Ejemplo1.php
Código PHP:
<?php
include("Barcode39.php");
#AHORA creo el objeto $OBJETO y llamo a la funcion de impresión.
$OBJETO = new Barcode39('12345678901');
$OBJETO->draw();

echo 
'Prueba 1...';

$OBJETO1 = new Barcode39('12345678902');
$OBJETO1->draw();

echo 
'Prueba 2...';
?>
Aqui el script con la clase: Barcode39.php
Código PHP:
<?php
/**
 * Barcode39 - Code 39 Barcode Image Generator
 * 
 * @package Barcode39
 * @category Barcode39
 * @name Barcode39
 * @version 1.0
 * @author Shay Anderson 05.11
 * @link http://www.shayanderson.com/php/php-barcode-generator-class-code-39.htm
 * @license http://www.gnu.org/licenses/gpl.html GPL License
 * This is free software and is distributed WITHOUT ANY WARRANTY
 */
final class Barcode39 {
    
/**
     * Code 39 format 2 specifications
     */
    
const f2B "11";
    const 
f2W "00";
    const 
f2b "10";
    const 
f2w "01";

    
/**
     * Barcode code
     *
     * @var array $_code
     */
    
private $_code = array();

    
/**
     * Code 39 matrix
     *
     * @var array $_codes_39
     */
    
private $_codes_39 = array(
        
32 => 100011011001110110,
        
36 => 100010001000100110,
        
37 => 100110001000100010,
        
42 => 100010011101110110,
        
43 => 100010011000100010,
        
45 => 100010011001110111,
        
46 => 110010011001110110,
        
47 => 100010001001100010,
        
48 => 100110001101110110,
        
49 => 110110001001100111,
        
50 => 100111001001100111,
        
51 => 110111001001100110,
        
52 => 100110001101100111,
        
53 => 110110001101100110,
        
54 => 100111001101100110,
        
55 => 100110001001110111,
        
56 => 110110001001110110,
        
57 => 100111001001110110,
        
65 => 110110011000100111,
        
66 => 100111011000100111,
        
67 => 110111011000100110,
        
68 => 100110011100100111,
        
69 => 110110011100100110,
        
70 => 100111011100100110,
        
71 => 100110011000110111,
        
72 => 110110011000110110,
        
73 => 100111011000110110,
        
74 => 100110011100110110,
        
75 => 110110011001100011,
        
76 => 100111011001100011,
        
77 => 110111011001100010,
        
78 => 100110011101100011,
        
79 => 110110011101100010,
        
80 => 100111011101100010,
        
81 => 100110011001110011,
        
82 => 110110011001110010,
        
83 => 100111011001110010,
        
84 => 100110011101110010,
        
85 => 110010011001100111,
        
86 => 100011011001100111,
        
87 => 110011011001100110,
        
88 => 100010011101100111,
        
89 => 110010011101100110,
        
90 => 100011011101100110
    
);

    
/**
     * Width of wide bars in barcode (should be 3:1)
     *
     * @var int $barcode_bar_thick
     */
    
public $barcode_bar_thick 3;
    
/**
     * Width of thin bars in barcode (should be 3:1)
     *
     * @var int $barcode_bar_thin
     */
    
public $barcode_bar_thin 1#GROSOR DE CODIGO DE BARRAS
    /**
     * Barcode background color (RGB)
     *
     * @var array $barcode_bg_rgb
     */ 
    
public $barcode_bg_rgb = array(255255255); #MARCO DE CODIGO DE BARRAS
    /**
     * Barcode height
     *
     * @var int $barcode_height
     */
    
public $barcode_height 80;  #ALTURA  DE CODIGO DE BARRA

    /**
     * Barcode padding
     *
     * @var int $barcode_padding
     */
    
public $barcode_padding 5#PADDING

    /**
     * Use barcode text flag
     *
     * @var bool $barcode_text
     */
    
public $barcode_text true#BOOLEANO SI SE MUESTRA EL TEXTO DEL CODIGO O NO

    /**
     * Barcode text size
     *
     * @var int $barcode_text_size
     */
    
public $barcode_text_size 3#TAMAÑO DE ROTULO DE TEXTO

    /**
     * Use dynamic barcode width (will auto set width)
     *
     * @var bool $barcode_use_dynamic_width
     */
    
public $barcode_use_dynamic_width true;  //AJUSTA EL CODIGO CON EL ROTULO DE TEXTO A UN MISMO ANCHO

    /**
     * Barcode width (if not using dynamic width)
     *
     * @var int $barcode_width
     */
    
public $barcode_width 1000;

    
/**
     * Set and format params
     *
     * @param string $code
     */
    
public function  __construct($code null) {
        
// format and code
        
$code = (string)strtoupper($code);

        
// convert code to code array
        
$i 0;
        while(isset(
$code[$i])) {
            
$this->_code[] = $code[$i++];
        }

        
// add start and stop symbols
        
array_unshift($this->_code,'');
        
array_push($this->_code,'');
    }

    
/**
     * Draw barcode (and save as file if filename set)
     *
     * @param string $filename (optional)
     * @return bool
     */
    
public function draw($filename null) {
        
// check if GB library functions installed
        
if(!function_exists("imagegif"))
        {
            return 
false;
        }

        
// check for valid code
        
if(!is_array($this->_code) || !count($this->_code)) {
            return 
false;
        }

        
// bars coordinates and params
        
$bars = array();

        
// position pointer
        
$pos $this->barcode_padding;

        
// barcode text
        
$barcode_string null;

        
// set code 39 codes
        
$i 0;
        foreach(
$this->_code as $k => $v) {
            
// check for valid code
            
if(isset($this->_codes_39[ord($v)])) {
                
// valid code add code 39, also add separator between characters if not first character
                
$code = ( $i self::f2w null ) . $this->_codes_39[ord($v)];

                
// check for valid code 39 code
                
if($code) {
                    
// add to barcode text
                    
$barcode_string .= " {$v}";

                    
// init params
                    
$w 0;
                    
$f2 $fill null;

                    
// add each bar coordinates and params
                    
for($j 0$j strlen($code); $j++) {
                        
// format 2 code
                        
$f2 .= (string)$code[$j];

                        
// valid format 2 code
                        
if(strlen($f2) == 2) {
                            
// set bar fill
                            
$fill $f2 == self::f2B || $f2 == self::f2b "_000" "_fff";

                            
// set bar width
                            
$w $f2 == self::f2B || $f2 == self::f2W $this->barcode_bar_thick $this->barcode_bar_thin;

                            
// check for valid bar params
                            
if($w && $fill) {
                                
// add bar coordinates and params
                                
$bars[] = array($pos$this->barcode_padding$pos $w,
                                    
$this->barcode_height $this->barcode_padding 1$fill);

                                
// move position pointer
                                
$pos += $w;
                            }

                            
// reset params
                            
$f2 $fill null;
                            
$w 0;
                        }
                    }
                }
                
$i++;
            
// invalid code, remove character from code
            
} else {
                unset(
$this->_code[$k]);
            }
        }

        
// check for valid bar coordinates and params
        
if(!count($bars)) {
            
// no valid bar coordinates and params
            
return false;
        }

        
// set barcode width
        
$bc_w $this->barcode_use_dynamic_width $pos $this->barcode_padding $this->barcode_width;

        
// if not dynamic width check if barcode wider than barcode image width
        
if(!$this->barcode_use_dynamic_width && $pos $this->barcode_width) {
            return 
false;
        }

        
// initialize image
        
$img imagecreate($bc_w$this->barcode_height);
        
$_000 imagecolorallocate($img000);
        
$_fff imagecolorallocate($img255255255);
        
$_bg imagecolorallocate($img$this->barcode_bg_rgb[0], $this->barcode_bg_rgb[1], $this->barcode_bg_rgb[2]);

        
// fill background
        
imagefilledrectangle($img00$bc_w$this->barcode_height$_bg);

        
// add bars to barcode
        
for($i 0$i count($bars); $i++) {
            
imagefilledrectangle($img$bars[$i][0], $bars[$i][1], $bars[$i][2], $bars[$i][3], $$bars[$i][4]);
        }

        
// check if using barcode text
        
if($this->barcode_text) {
            
// set barcode text box
            
$barcode_text_h 10 $this->barcode_padding;
            
imagefilledrectangle($img$this->barcode_padding$this->barcode_height $this->barcode_padding $barcode_text_h,
                
$bc_w $this->barcode_padding$this->barcode_height $this->barcode_padding$_fff);

            
// set barcode text font params
            
$font_size $this->barcode_text_size;
            
$font_w imagefontwidth($font_size);
            
$font_h imagefontheight($font_size);

            
// set text position
            
$txt_w $font_w strlen($barcode_string);
            
$pos_center ceil((($bc_w $this->barcode_padding) - $txt_w) / 2);

            
// set text color
            
$txt_color imagecolorallocate($img0255255);

            
// draw barcod text
            
imagestring($img$font_size$pos_center$this->barcode_height $barcode_text_h 2,
                
$barcode_string$_000);
        }

        
// check if writing image
        
if($filename)
        {
            
imagegif($img$filename);
        
// display image
        
} else {
            
//header("Content-type: image/gif");
            
imagegif($img);
        }
        
        
imagedestroy($img);

        
// valid barcode
        
return true;
    }
}
?>
¿Qué es lo que impide que muestre un valor después de la primera invocación?, y ¿cómo puedo solucionarlo?.

Gracias por la ayuda.
  #2 (permalink)  
Antiguo 13/05/2013, 19:12
Avatar de pateketrueke
Modernizr
 
Fecha de Ingreso: abril-2008
Ubicación: Mexihco-Tenochtitlan
Mensajes: 26.399
Antigüedad: 16 años
Puntos: 2534
Respuesta: Invocar varias veces un objeto que devulve una imagen

El método draw() sirve de dos formas:

1. Guarda la imagen como un archivo
2. Envía la imagen directamente al navegador

En el ejemplo que estás ejecutando usas la segunda forma, de modo que es imposible mostrar dos imágenes al mismo tiempo, sólo eso.
__________________
Y U NO RTFM? щ(ºдºщ)

No atiendo por MP nada que no sea personal.
  #3 (permalink)  
Antiguo 13/05/2013, 19:19
 
Fecha de Ingreso: noviembre-2008
Ubicación: lima
Mensajes: 148
Antigüedad: 15 años, 4 meses
Puntos: 0
Respuesta: Invocar varias veces un objeto que devulve una imagen

Hola pateketrueke, y como puedo hacer para usar sólo la primera forma y que me permita mostrar mas de un archivo generado?.

Gracias por la respuesta.
  #4 (permalink)  
Antiguo 13/05/2013, 19:22
Avatar de pateketrueke
Modernizr
 
Fecha de Ingreso: abril-2008
Ubicación: Mexihco-Tenochtitlan
Mensajes: 26.399
Antigüedad: 16 años
Puntos: 2534
Respuesta: Invocar varias veces un objeto que devulve una imagen

Mira, tu eres la que debe aprender a usar las librerías o software que descargas de algún lado, nosotros no podemos estar leyendo cada librería y explicar su uso únicamente por "buenas gentes". Esa es responsabilidad de quien hace uso de software de terceros.

No se con exactitud como debes hacerlo porque no conozco la librería que estás usando, pero a simple vista se puede leer en el código esto:
Código PHP:
Ver original
  1. /**
  2.   * Draw barcode (and save as file if filename set)
  3.   *
  4.   * @param string $filename (optional)
  5.   * @return bool
  6.   */
  7. public function draw($filename = null)

Vamos, que necesitas pasar por argumento el nombre del archivo y ya con eso deberías ser capaz de guardar distintos archivos, ¿no te parece?

Por eso es recomendable usar únicamente software documentado y con ejemplos.
__________________
Y U NO RTFM? щ(ºдºщ)

No atiendo por MP nada que no sea personal.
  #5 (permalink)  
Antiguo 14/05/2013, 13:55
 
Fecha de Ingreso: noviembre-2008
Ubicación: lima
Mensajes: 148
Antigüedad: 15 años, 4 meses
Puntos: 0
Respuesta: Invocar varias veces un objeto que devulve una imagen

Solucionado.
Gracias por la respuesta.

Etiquetas: html, invocar, objeto, veces
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 03:35.