strange problem with session variable when creating images

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
twrofpwr
Forum Newbie
Posts: 17
Joined: Wed Jun 15, 2005 8:46 pm

strange problem with session variable when creating images

Post by twrofpwr »

Hey guys, I have a loop that goes through a bunch of images and then sets a session variable of the image url, and calls a image class through an image src tag.

This image class tha is called from the src tag, retreives the session variable set in my other file, but it seems to get the very LAST image_url from the session variable, for each iteration of the loop, I am really wondering what is going on.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

show your code.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Yeah show your code but it sounds like your loop simply overwrites the old variable for each iteration (common mistake) and hence you finish up with only the last one :)
twrofpwr
Forum Newbie
Posts: 17
Joined: Wed Jun 15, 2005 8:46 pm

Post by twrofpwr »

Code: Select all

The below code is in a loop, and yes the $row['img_url'] changes each iteration through

loopingcode.php :

if($thumbnail)
	                                                    {
	                                                    	
	                                                    	//TODO 	                                                    $img_source="http://65.77.130.67:82/auto_listings/porsche/356/images/image.php";
	                                                    	
	                                                    	
print "<br>IMAGE SOURCE in functions.php = " .$row['img_url'];
$_SESSION['img_source'] = $row['img_url'];
$_SESSION['width'] = $width;
$row['img_url'] ."<img src='".$img_source ."'></img></td>";





class.image.php

<?PHP
//Image class to perform some manipulations to images such as creating thumbnails
Class image {
	var $image;
	var $height;
	var $width;
	var $thumbnail; //This is the "php" bitmap image
	var $thumbnail_t; //this is where the final thumbnail image will be stored type (jpg,gif,....)
	
	function image($image_src)
	{
		
		//print("<br> image_src = " . $image_src);
		
		@ $image_src or
    		die ("image_src is not set '$php_errormsg'");
		
		
		//Find out the type of image and call the appropriate function
		$image_type = exif_imagetype($image_src);

		
		
		
		//print "<br>creating new image from image = " .$image_src ." of image " .$image_type;
		
		
		switch($image_type){
		case IMAGETYPE_GIF :
			$this->image = imagecreatefromgif($image_src);
			break;
		case IMAGETYPE_JPEG :
			
			print("<br> img_src = " . $image_src);
		
			$this->image = imagecreatefromjpeg($image_src);
			
			break;
		case IMAGETYPE_JPEG2000 :

			$this->image = imagecreatefromjpeg($image_src);
		
		default:
			
			return 0;
		}

		
		
		 $this->width = imagesx($this->image);
    	 $this->height = imagesy($this->image);
    	 return 1;
	}

	function create_thumbnail($thumb_width)
	{		
    	$diff = $this->width/$thumb_width;
    	$new_height = $thumb_width;
    	
    	//print("resource =  " .$this->image);
    	//imagejpeg($this->image);
    	$this->thumbnail = imagecreate($thumb_width,$new_height);
    	imagecopyresized($this->thumbnail,$this->image,0,0,0,0,$thumb_width,$new_height,imagesx($this->image),imagesy($this->image)); 	
    	//imagejpeg($this->thumbnail);
    }
	
    function create_jpg_thumbnail($thumb_width)
    {
    	$this->create_thumbnail($thumb_width);
    	imagejpeg($this->thumbnail, "$thumb_path/$image_name");
    	$this->thumbnail_t = imagecreate($new_width,$new_height);
    }
	
    function display_jpeg()
    {
    	//header("Content-type: image/jpeg");
    	imagejpeg($this->thumbnail);
    }
    
    function display_gif()
    {
    	//header("Content-type: image/gif");
    	imagegif($this->thumbnail);
    }
	
    function destroy()
    {
    	imagedestroy($this->image);
    	imagedestroy($this->thumbnail);
    }
	
    function display()
    {
    	
    	imagejpeg($this->thumbnail);
    	$this->destroy();
    	/*
    	$image_type = exif_imagetype($this->thumbnail);
		
		
		switch($image_type){
		case IMAGETYPE_GIF :
			$this->display_gif();
		break;
		case IMAGETYPE_JPEG||IMAGETYPE_JPEG2000 :
			 $this->display_jpeg();
		break; 
    	}
    	*/
    }
}// Class Image



//image.php

<?php
session_start();
//PRE: must pass in through a query string the image location (url or file) and the image width
/*
$img_source = $_SESSION[img_source];
$width = $_SESSION[width];

 $img_source = $_SESSION[img_source];
//print "<br> session var = " .$img_source;
//print "<br> image_url in image.php = " .$image_url;
//print "<br> width image.php= " .$width;


//print "<br> session src IN image.php =" .$img_source ."<br>width=" .$width;
*/
print "<br> image_src session var inside image.php = " .$_SESSION['img_source'] ."<br>";

include("/var/www/new/classes/class.image.php");  //have include instead of include_once because I want references to differnet objects
$img = new Image($_SESSION['img_source']);
$img->create_thumbnail($_SESSION['width']);
$img->display();
//$thumb_img = new image($image);
//$thumb_img->create_jpg_thumbnail($width);
?>


Does anyone have any clue why the session var inside the image.php is the last img url each time?? I am thinking the php file image.php is only created once and preprossed at the end... It shoudln't work like this shoudl it?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Moved to PHP-Code.
Post Reply