Page 1 of 1

image gallery issue.

Posted: Sat May 26, 2012 8:12 pm
by matthewparker
Hi,

Im in the last part of the image gallery tutorial.

The thumbs folder has been created, the images are being resized and saved to it and im redirected to the thumbs/img.jpg page - however when i try and display the image its saying "page not found"(even though when i check the thumbs folder i can see its there and has been resized :?

Here is the code:

<?php

if (isset($_GET['img']))
//make thumbnail


{
	if (file_exists($_GET['img']))
	
	{
		ignore_user_abort(true);
		set_time_limit(120);
		ini_set('memory_limit', '512M');
		
		$src_size = getimagesize($_GET['img']);
		
		if ($src_size === false)
		{
			die('That does not look like an image');
			
		}
		
		$thumb_width  = 250;
		$thumb_height = 200;
		
	if ($src_size['mime'] === 'image/jpeg'){
		$src = imagecreatefromjpeg($_GET['img']);
	}else if ($src_size['mime'] === 'image/png'){
		$src = imagecreatefrompng($_GET['img']);
	}else if ($src_size['mime'] === 'image/gif'){
		$src = imagecreatefromgif($_GET['img']);
		}
		
		$src_aspect = round(($src_size[0] / $src_size[1]), 1);
		$thumb_aspect = round(($thumb_width / $thumb_height), 1);
		
		if ($src_aspect < $thumb_aspect){
			//higher
			$new_size = array($thumb_width,($thumb_width / $src_size[0]) * $src_size[1]);
		    $src_pos = array(0, ($new_size[1] - $thumb_height) / 2);
			
		}else if($src_aspect > $thumb_aspect){
			//wider
			$new_size = array(($thumb_width / $src_size[1]) * $src_size[0], $thumb_height);
			$src_pos = array(($new_size[0] - $thumb_width) / 2, 0);
		}else{
			//same shape
			$new_size = array($thumb_width, $thumb_height);
			$src_pos = array(0, 0);
		}
	
		if($new_size[0] < 1) $new_size[0] = 1;
		if($new_size[0] < 1) $new_size[1] = 1;
		
		$thumb = imagecreatetruecolor($thumb_width, $thumb_height);
		imagecopyresampled($thumb, $src, 0, 0, $src_pos[0], $src_pos[1], $new_size[0], $new_size[1], $src_size[0], $src_size[1]);
		
		if ($src_size['mime'] === 'image/jpeg'){
		imagejpeg($thumb, "thumbs/{$_GET['img']}");
	}else if ($src_size['mime'] === 'image/png'){
		imagepng($thumb, "thumbs/{$_GET['img']}");
	}else if ($src_size['mime'] === 'image/gif'){
		imagegif($thumb, "thumbs/{$_GET['img']}");
		}
		
		header("Location: thumbs/{$_GET['img']}");
	}
	
	
	die();
	
}

if(is_dir('./thumbs') === false){
	mkdir('./thumbs', 0744);
	
}

$images = glob('*.{jpg,jpeg,png,gif}' , GLOB_BRACE);


?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type="text/css">
		a, img { float : left; }
</style>

<title>Untitled Document</title>
</head>

<body>
<div>
	<?php
	
	foreach($images as $image){
	if (file_exists("./thumbs/{$image}")){	
		echo "<a href=\"\"><img src=\"thumbs/{$image}\" alt=\"{$image}\" /></a>";
	}else{
		echo "<a href=\"\"><img src=\"?img={$image}\" alt=\"{$image}\" /></a>";
		
	}
	
	}
	
	



	?>
</div>
</body>
</html>











i can see for example that "a.jpg" is being saved in thumbs, and resized when i open it however when i access it on the server its like it doesn't even exist.

Re: image gallery issue.

Posted: Sat May 26, 2012 11:26 pm
by jacek
Erm :? Your code looks okay :?

What is the URL that you are trying to go to, and what is the URL of the php script ? it could be that you need to modify the img src="" to account for an extra directory level.

Re: image gallery issue.

Posted: Sun May 27, 2012 5:41 pm
by matthewparker
ok i tried doing this again, and gave my folders different name. the image is in the folder and its being resized but just coming up as "page not found"


the url i am trying to access is:

http://www.thepram.co.uk/user_system/im ... ls/100.png


the link in the script is:

header("Location: thumbnails/{$_GET['img']}");

the gallery.php page is located within the imagegallery folder.

And the updated script:

<?php

if (isset($_GET['img'])){
	//make thumbnail
	if (file_exists($_GET['img'])){
		ignore_user_abort(true);
		set_time_limit(120);
		ini_set('memory_limit', '512M');
		
		$src_size = getimagesize($_GET['img']);
		
		if ($src_size === false){
			die('That does not look like an Image.');
		}
		
		$thumb_width 		= 250;
		$thumb_height 		= 200;
		
		if ($src_size['mime'] === 'image/jpeg'){
			$src = imagecreatefromjpeg($_GET['img']);
		}else if($src_size['mime'] === 'image/png'){
			$src = imagecreatefrompng($_GET['img']);
		}else if($src_size['mime'] === 'image/gif'){
			$src = imagecreatefromgif($_GET['img']);
		}
		
		$src_aspect = round(($src_size[0] / $src_size[1]), 1);
		$thumb_aspect = round(($sthumb_width / $thumb_height), 1);
		
		if ($src_aspect < $thumb_aspect){
			//higher
			$new_size = array($thumb_width,($thumb_width/ $src_size[0]) * $src_size[1]);
			$src_pos = array(0,($new_size[1] - $thumb_height) / 2);
		}else if ($src_aspect > $thumb_aspect){
			//wider
			$new_size = array(($thumb_width / $src_size[1]) * $src_size[0], $thumb_height);
			$src_pos = array(($new_size[0] - $thumb_width) / 2, 0);
		}else{
			//same shape
			$new_size = array($thumb_width, $thumb_height);
			$src_pos = array(0, 0);
		
		}
		
		if ($new_size[0] < 1) $new_size[0] = 1;
		if ($new_size[1] < 1) $new_size[1] = 1;
		
		$thumb = imagecreatetruecolor($thumb_width, $thumb_height);
		imagecopyresampled($thumb, $src, 0, 0, $src_pos[0], $src_pos[1], $new_size[0], $new_size[1], $src_size[0], $src_size[1]);
	
		if ($src_size['mime'] === 'image/jpeg'){
			imagejpeg($thumb, "thumbnails/{$_GET['img']}");
		}else if($src_size['mime'] === 'image/png'){
			imagepng($thumb, "thumbnails/{$_GET['img']}");
		}else if($src_size['mime'] === 'image/gif'){
			imagegif($thumb, "thumbnails/{$_GET['img']}");
		}
		
		header("Location: thumbnails/{$_GET['img']}");

	}
	
	die();
	
}

if (is_dir('./thumbnails') === false){
	mkdir('./thumbnails', 0744);
	
}

$images = glob('*.{jpg,jpeg,png,gif}', GLOB_BRACE);

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type="text/css">
a, img { float:left; }

</style>
<title>Untitled Document</title>
</head>

<body>
<div>
<?php

foreach ($images as $image){
	if (file_exists("./thumbnails/{$image}")){
		echo "<a href=\"\"><img src=\"thumbnails/{$image}\" alt=\"{$image}\" /></a>";
	}else{
		echo "<a href=\"\"><img src=\"?img={$image}\" alt=\"{$image}\" /></a>";
	}
	
}

?>


</div>
</body>
</html>












the only other thing i can thing of is that the user_system folder is primarily a protected area, apart from the pages that are part of and $exceptions array, but surely even then if you tried to access the image you would be directed to the log in page, it wouldn't say "page not found" would it? :?

Re: image gallery issue.

Posted: Sun May 27, 2012 9:13 pm
by jacek
Okay :?

Well after you run the script do you see the file on this page http://www.thepram.co.uk/user_system/im ... humbnails/ ?

What happens if you browse to index.php?img=100.png (make sure there are no images in the thumbnails folder first) ?