Page 1 of 1

thumbnails are black

Posted: Mon Jun 20, 2011 12:41 pm
by Robbedoesie
Hello,
i have finished the four parts of your automatic image gallery tutorial. Almost everything works fine except that all the thumbnails are black. I'll thought that the problem might be in the imagecreatetruecolor or that the path to the thumbnails is wrong, but whatever i do, the thumbnails are staying black.
Hopefully someone can help me out.
<?php

if (isset($_GET['img'])){
	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('Dit is geen afbeelding.');
		}
		
		$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']);
		}
		
		$thumb = imagecreatetruecolor($thumb_width, $thumb_height);
		
		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=\"{$image}\"><img src=\"thumbs/{$image}\" alt=\"{$image}\" /></a>";
	}else{
		echo "<a href=\"{$image}\"><img src=\"?img={$image}\" alt=\"{$image}\" /></a>";
	}
}
?>
</div>

Re: thumbnails are black

Posted: Mon Jun 20, 2011 12:45 pm
by jacek
You seem to have missed a bit.

you need to copy the original image, $src, onto the thumbnail image, $thumb. what you are doing here is creating a blank image and then outputting it.

Re: thumbnails are black

Posted: Wed Jun 22, 2011 3:14 pm
by Robbedoesie
Hello Jecek,
i'll have put the imagecopyresampled with all the parameters under the thumb variable, but the thumbnails are staying black.
How can i copy the original image onto the thumbnail?

The reason that my php file is not complete is that i want to use my thumbnails different.
The reason that i still follow this tutorial is that it is a different tutorial than the other tutorials i have followed, it is more clear for me were the main image is in the code and what i can do with it. And i learn a lot from this tutorial with for me a clear explanation.

regards,
Robbedoesie

Re: thumbnails are black

Posted: Wed Jun 22, 2011 4:23 pm
by jacek
Robbedoesie wrote:How can i copy the original image onto the thumbnail?
using imagecopyresampled(), post your code if that did not work.
Robbedoesie wrote: And i learn a lot from this tutorial with for me a clear explanation.
:) well that is good.

Re: thumbnails are black

Posted: Wed Jun 22, 2011 5:38 pm
by Robbedoesie
The full code is now;
<?php

if (isset($_GET['img'])){
	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('Dit is geen afbeelding.');
		}
		
		$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']);
		}
		
		$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=\"{$image}\"><img src=\"thumbs/{$image}\" alt=\"{$image}\" /></a>";
	}else{
		echo "<a href=\"{$image}\"><img src=\"?img={$image}\" alt=\"{$image}\" /></a>";
	}
}
?>
</div>
</body>
</html>

Re: thumbnails are black

Posted: Wed Jun 22, 2011 6:34 pm
by jacek
You have not defined the $src_post and the $new_size variables.

If you comment out this line
header("location: thumbs/{$_GET['img']}");
You will probably see the errors causing the problem.

Re: thumbnails are black

Posted: Wed Jun 22, 2011 8:45 pm
by Robbedoesie
I am gonna define the $src_post and the $new_size variables and let you know if the thumbnails appeared or not.

I commented out the header line, but nothing chanced, no error lines, the black thumbnails where staying were they are.

Re: thumbnails are black

Posted: Fri Jun 24, 2011 2:33 pm
by Robbedoesie
Hello Jecek,
i have completed the tutorial and also defined all the variables. Unfortunately the thumbnails are staying black.
I have checked the code but can't find something wrong, but maybe you see something wrong about it?
<?php

if (isset($_GET['img'])){
	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('Dit is geen afbeelding.');
		}
		
		$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){
			$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){
			$new_size = array(($thumb_width / $src_size[1]) * $src_size[0], $thumb_height);
			$src_pos = array(($new_size[0] - $thumb_width) / 2, 0);
		}else{
			$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, "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=\"{$image}\"><img src=\"thumbs/{$image}\" alt=\"{$image}\" /></a>";
	}else{
		echo "<a href=\"{$image}\"><img src=\"?img={$image}\" alt=\"{$image}\" /></a>";
	}
}
?>
</div>
</body>
</html>

Re: thumbnails are black

Posted: Fri Jun 24, 2011 3:42 pm
by jacek
Comment out the header line
header("location: thumbs/{$_GET['img']}");
Then add
error_reporting(E_ALL);
to the top of the page.

Now delete all black thumbnails (since they are only generated once) and browse to the page that creates the thumbnail like index.php?img=example_img.png

If it's not working, you will get the error that explains why.

Re: thumbnails are black

Posted: Fri Jun 24, 2011 8:31 pm
by Robbedoesie
Hello again,
well, when i removed all of the thumbnails the problem solved itself. New, good thumbnails came back. I have changed for example the thumbnailsize. I have pushed all the refresh buttons, including the button from the map where the thumbnails are in and nothing happens. I'll delete all the thumbnails and the size of the thumbnails are chanched. Anyway, it works. Thanks.

I don't get the line like "index.php?img=example_img.png" in the adressbalk(i don't now the English word for it, hope you understand what i mean). I'll get the folder where the images and script are in and then the imagename. Hope that this is also allright and that it can't give any problems.

Re: thumbnails are black

Posted: Fri Jun 24, 2011 10:00 pm
by jacek
Robbedoesie wrote:I have pushed all the refresh buttons, including the button from the map where the thumbnails are in and nothing happens. I'll delete all the thumbnails and the size of the thumbnails are chanched. Anyway, it works. Thanks.
That is because the thumbnails are n=only generated once, so to have them regenerate you need to manually remove the current ones.

Goods news that you got it working anyway :)
Robbedoesie wrote:I don't get the line like "index.php?img=example_img.png"
You will if the thumbnails are not yet made, well you won't see it but the image src="" will be set to something like that. Don't worry about it too much if its working now.
Robbedoesie wrote:in the adressbalk(i don't now the English word for it, hope you understand what i mean).
I think you mean the address bar, the bit of the browser where you type the website in :) According to google translate that's what you mean anyway http://translate.google.com/#auto|en|adressbalk