Jump to content

[SOLVED] Class variable becomes null?


muckv

Recommended Posts

hey, when I call the method get_copyright() the array $meta becomes null instead op returning the value of $meta['copyright']

 

Everything goes well in the constructor, but when that method is called (see above)

 

does anybody see what I did wrong

 

 

<?php



class IPTC{
var $meta;
var $image;


	function __construct($image){
	$meta=array();
	$this->meta=$meta;	
  		$this->image=$image;
  
   	$meta = array(
	'credit' => '',
	'caption' => '',
	'created_timestamp' => 0,
	'copyright' => '',
	'title' => '',
	);

	$size=getimagesize($image,$info);
        if ( isset ($info['APP13'])) {
        $iptc=iptcparse($info['APP13']);
        
        
        if ( !empty($iptc['2#110'][0]) ) 
			$meta['credit'] = utf8_encode(trim($iptc['2#110'][0]));
		elseif ( !empty($iptc['2#080'][0]) ) 
			$meta['credit'] = utf8_encode(trim($iptc['2#080'][0]));
		if ( !empty($iptc['2#055'][0]) and !empty($iptc['2#060'][0]) ) 
			$meta['created_timestamp'] = strtotime($iptc['2#055'][0] . ' ' . $iptc['2#060'][0]);
		if ( !empty($iptc['2#120'][0]) ) // caption
			$meta['caption'] = utf8_encode(trim($iptc['2#120'][0]));
		if ( !empty($iptc['2#116'][0]) ) // copyright
			$meta['copyright'] = utf8_encode(trim($iptc['2#116'][0]));
		if ( !empty($iptc['2#005'][0]) ) // title
			$meta['title'] = utf8_encode(trim($iptc['2#005'][0]));

    }
  }
  
  function get_credit(){
  	return $meta['credit'];
  	
  	}
  	
  	
  function get_timestamp(){
  	return $meta['created_timestamp'];
  	
  	}
  	
  	
  	  function get_caption(){
  	return $meta['caption'];
  	
  	}
  	
  	
  	  function get_copyright(){
  	return $meta['copyright'];
  	
  	}
  	
  	 function get_title(){
  	return $meta['title'];
  	
  	}
  	
}	


$image_info = new IPTC('suzy1.jpg') or die ('kan geen iptc gegevens extracten uit image');
        
echo $image_info->get_copyright()or die('geen copyright beschikbaar');	

 

 

Link to comment
https://forums.phpfreaks.com/topic/132113-solved-class-variable-becomes-null/
Share on other sites

Try this

 

<?php



<?php



class IPTC{
   var $meta;
   var $image;
   

      function __construct($image){
      
      
        $this->image=$image;
  
         $this->meta = array(
      'credit' => '',
      'caption' => '',
      'created_timestamp' => 0,
      'copyright' => '',
      'title' => '',
      );
         
      $size=getimagesize($image,$info); 
       if ( isset ($info['APP13'])) {
        $iptc=iptcparse($info['APP13']);
               
        if ( !empty($iptc['2#110'][0]) ) 
           $this->meta['credit'] = utf8_encode(trim($iptc['2#110'][0]));
         elseif ( !empty($iptc['2#080'][0]) ) 
            $this->meta['credit'] = utf8_encode(trim($iptc['2#080'][0]));
         if ( !empty($iptc['2#055'][0]) and !empty($iptc['2#060'][0]) ) 
            $this->meta['created_timestamp'] = strtotime($iptc['2#055'][0] . ' ' . $iptc['2#060'][0]);
         if ( !empty($iptc['2#120'][0]) ) // caption
            $this->meta['caption'] = utf8_encode(trim($iptc['2#120'][0]));
         if ( !empty($iptc['2#116'][0]) ) // copyright
            $this->meta['copyright'] = utf8_encode(trim($iptc['2#116'][0]));
         if ( !empty($iptc['2#005'][0]) ) // title
            $this->meta['title'] = utf8_encode(trim($iptc['2#005'][0]));

    }
  }
  
  function get_credit(){
     return $this->meta['credit'];
     
     }
     
     
  function get_timestamp(){
     return $this->meta['created_timestamp'];
     
     }
     
     
       function get_caption(){
     return $this->meta['caption'];
     
     }
     
     
       function get_copyright(){
	  return $this->meta['copyright'];
     
     }
     
      function get_title(){
     return $this->meta['title'];
     
     }
     
}   
   
   
$image_info = new IPTC('ARPC.JPG') or die ('kan geen iptc gegevens extracten uit image');
$res = $image_info->get_copyright()or die('geen copyright beschikbaar');       
echo $res;   

Well, Still the same problem, the if structure does get executed cause I follow it while debugging

 

 

I can see the correct value in meta['copyright'] until the method gets called

 

 

 

<?php



class IPTC{

var $meta;
var $info;

	function __construct($image){


  		$this->image=$image;
		$this->meta=array(
	'credit' => '',
	'caption' => '',
	'created_timestamp' => 0,
	'copyright' => '',
	'title' => '',
	);

	$size=getimagesize($image,$info);
	$this->info=$info;
        if ( isset ($info['APP13'])) {
        $iptc=iptcparse($info['APP13']);
        
        
        if ( !empty($iptc['2#110'][0]) ) 
			$meta['credit'] = utf8_encode(trim($iptc['2#110'][0]));
		elseif ( !empty($iptc['2#080'][0]) ) 
			$meta['credit'] = utf8_encode(trim($iptc['2#080'][0]));
		if ( !empty($iptc['2#055'][0]) and !empty($iptc['2#060'][0]) ) 
			$meta['created_timestamp'] = strtotime($iptc['2#055'][0] . ' ' . $iptc['2#060'][0]);
		if ( !empty($iptc['2#120'][0]) ) // caption
			$meta['caption'] = utf8_encode(trim($iptc['2#120'][0]));
		if ( !empty($iptc['2#116'][0]) ) // copyright
			$meta['copyright'] = utf8_encode(trim($iptc['2#116'][0]));
		if ( !empty($iptc['2#005'][0]) ) // title
			$meta['title'] = utf8_encode(trim($iptc['2#005'][0]));

    }
  }
  
  function get_credit(){
  	return $meta['credit'];
  	
  	}
  	
  	
  function get_timestamp(){
  	return $nfo['created_timestamp'];
  	
  	}
  	
  	
  	  function get_caption(){
  	return $meta['caption'];
  	
  	}
  	
  	
  	  function get_copyright(){
  	return $meta['copyright'];
  	
  	}
  	
  	 function get_title(){
  	return $meta['title'];
  	
  	}
  	
}	


$image_info = new IPTC('suzy1.jpg') or die ('kan geen iptc gegevens extracten uit image');
        
echo $image_info->get_copyright();










?>

well, i just ran the code you pasted (had to fix it to use $this), and i added this line:

echo "in if";

on line 26, right after this:

$iptc=iptcparse($info['APP13']);

and the text "in if" does not display. as i said, $info['APP13'] does not get set/declared/initialized anywhere in the class, and so the if(isset($info['APP13'])) does not execute.

I just ran this code  and In if IS echoed, i'm really in the dark here

 

 

 

 

<?php



class IPTC{

var $meta;
var $info;

	function __construct($image){


  		$this->image=$image;
		$this->meta=array(
	'credit' => '',
	'caption' => '',
	'created_timestamp' => 0,
	'copyright' => '',
	'title' => '',
	);

	$size=getimagesize($image,$info);
	$this->info=$info;
        if ( isset ($info['APP13'])) {
        	echo "in if";
        $iptc=iptcparse($info['APP13']);
        
        
        if ( !empty($iptc['2#110'][0]) ) 
			$meta['credit'] = utf8_encode(trim($iptc['2#110'][0]));
		elseif ( !empty($iptc['2#080'][0]) ) 
			$meta['credit'] = utf8_encode(trim($iptc['2#080'][0]));
		if ( !empty($iptc['2#055'][0]) and !empty($iptc['2#060'][0]) ) 
			$meta['created_timestamp'] = strtotime($iptc['2#055'][0] . ' ' . $iptc['2#060'][0]);
		if ( !empty($iptc['2#120'][0]) ) // caption
			$meta['caption'] = utf8_encode(trim($iptc['2#120'][0]));
		if ( !empty($iptc['2#116'][0]) ) // copyright
			$meta['copyright'] = utf8_encode(trim($iptc['2#116'][0]));
		if ( !empty($iptc['2#005'][0]) ) // title
			$meta['title'] = utf8_encode(trim($iptc['2#005'][0]));

    }
  }
  
  function get_credit(){
  	return $meta['credit'];
  	
  	}
  	
  	
  function get_timestamp(){
  	return $meta['created_timestamp'];
  	
  	}
  	
  	
  	  function get_caption(){
  	return $meta['caption'];
  	
  	}
  	
  	
  	  function get_copyright(){
  	return $meta['copyright'];
  	
  	}
  	
  	 function get_title(){
  	return $meta['title'];
  	
  	}
  	
}	


$image_info = new IPTC('suzy1.jpg') or die ('kan geen iptc gegevens extracten uit image');
        
echo $image_info->get_copyright();

 

 

contentrt3.png

w789.png

 

this is the content of image_info so you see the array $meta is empty but why?

 

<?php



class IPTC{

var $meta;
var $info;

	function __construct($image){


  		$this->image=$image;
		$this->meta=array(
	'credit' => '',
	'caption' => '',
	'created_timestamp' => 0,
	'copyright' => '',
	'title' => '',
	);

	$size=getimagesize($image,$info);
	$this->info=$info;
        if ( isset ($info['APP13'])) {
        $iptc=iptcparse($info['APP13']);
        
        
        if ( !empty($iptc['2#110'][0]) ) 
			$meta['credit'] = utf8_encode(trim($iptc['2#110'][0]));
		elseif ( !empty($iptc['2#080'][0]) ) 
			$meta['credit'] = utf8_encode(trim($iptc['2#080'][0]));
		if ( !empty($iptc['2#055'][0]) and !empty($iptc['2#060'][0]) ) 
			$meta['created_timestamp'] = strtotime($iptc['2#055'][0] . ' ' . $iptc['2#060'][0]);
		if ( !empty($iptc['2#120'][0]) ) // caption
			$meta['caption'] = utf8_encode(trim($iptc['2#120'][0]));
		if ( !empty($iptc['2#116'][0]) ) // copyright
			$meta['copyright'] = utf8_encode(trim($iptc['2#116'][0]));
		if ( !empty($iptc['2#005'][0]) ) // title
			$meta['title'] = utf8_encode(trim($iptc['2#005'][0]));

    }
  }
  
  function get_credit(){
  	return $this->meta['credit'];
  	
  	}
  	
  	
  function get_timestamp(){
  	return $this->meta['created_timestamp'];
  	
  	}
  	
  	
  	  function get_caption(){
  	return $this->meta['caption'];
  	
  	}
  	
  	
  	  function get_copyright(){
  	return $this->meta['copyright'];
  	
  	}
  	
  	 function get_title(){
  	return $this->meta['title'];
  	
  	}
  	
}	


$image_info = new IPTC('suzy1.jpg') or die ('kan geen iptc gegevens extracten uit image');

echo $image_info->get_copyright();

You are setting the $this->meta variable in correctly. Here

if ( !empty($iptc['2#110'][0]) ) 
            $meta['credit'] = utf8_encode(trim($iptc['2#110'][0]));
         elseif ( !empty($iptc['2#080'][0]) ) 
            $meta['credit'] = utf8_encode(trim($iptc['2#080'][0]));
         if ( !empty($iptc['2#055'][0]) and !empty($iptc['2#060'][0]) ) 
            $meta['created_timestamp'] = strtotime($iptc['2#055'][0] . ' ' . $iptc['2#060'][0]);
         if ( !empty($iptc['2#120'][0]) ) // caption
            $meta['caption'] = utf8_encode(trim($iptc['2#120'][0]));
         if ( !empty($iptc['2#116'][0]) ) // copyright
            $meta['copyright'] = utf8_encode(trim($iptc['2#116'][0]));
         if ( !empty($iptc['2#005'][0]) ) // title
            $meta['title'] = utf8_encode(trim($iptc['2#005'][0]));

    }

You're populating your $meta array. $this->meta and $meta are not the same. You'll need to change all instances of $meta to $this->meta or add the following line after the above block of code

$this->meta = $meta;

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.