PHP Tutorial: Automatic Image Gallery [part 01]

Post here is you are having problems with any of the tutorials.
Post Reply
jan24
Posts: 8
Joined: Sun Jan 29, 2012 6:05 pm

PHP Tutorial: Automatic Image Gallery [part 01]

Post by jan24 »

I get stuck at 11:30 from the toturial video.
When I type in this code:
if(isset($_GET["img"])){
die();
}
It doesn't kill the code.
But at the video it kills the video.
It doesn't display any error either though, it just displays the boxes where the images should be like in the video before 11:30.

Then I tried to get some error from it and typed this code:
print_r($_GET["img"]);
I got the following error out of it:
"Notice: Undefined index: img in"

So it doesn't find any img?
I hope somebody could help me out, thanks in advance.

My full code is here:
<?php
	if(isset($_GET['img'])){
		die();
	}
	
	if(is_dir('./thumbs') === false){
		mkdir('./thumbs', 0744);
	}
?>
<!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" />
<title>Untitled Document</title>
</head>
<?php
	$images = glob('*.{jpg,jpeg,png,gif}', GLOB_BRACE);
	
	foreach($images as $image){
		if(file_exists("./thumbs/{$image}")){
			echo "<a href=\"{$images}\"><img src=\"./thumbs/{$image}\" alt=\"{$image}\" /></a>";
		}else{
			echo "<a href=\"{$images}\"><img src=\"?img={$image}\" alt=\"{$image}\" /></a>";
		}
	}
	print_r($_GET["img"]);
?>
<body>
</body>
</html>
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: PHP Tutorial: Automatic Image Gallery [part 01]

Post by jacek »

Are you sure the variable is there in the url, you should see somethign like ?img=something in the address bar ?
Image
jan24
Posts: 8
Joined: Sun Jan 29, 2012 6:05 pm

Re: PHP Tutorial: Automatic Image Gallery [part 01]

Post by jan24 »

jacek wrote:Are you sure the variable is there in the url, you should see somethign like ?img=something in the address bar ?
Thanks for your response.
I had indeed nothing in the url like ?img=img001.jpg
When I type something like that in the url, the error gets away, but also the gallery disappears.
And I also want to let the gallery to get displayed.
So when I type something like ?img=img001.jpg after the url I get a blank page.

EDIT:
Well I mostly got it working now.
It does makes the thumbnail (when I type ?img=img001.jpg behind the url).
When I click on it, it does goes to the right image.
It doesn't show any errors anymore.
Although.. it doesn't show the thumbnails in the gallery.
It shows all broken images.

EDIT:
Alright I got it all working!
Although I would like to have it a bit diffrent.
You putted the file "gallery.php" in the same folder as the images.
But I want to put the "gallery.php" in the root folder then a folder "/img" where I put all big ones in and after that "/img/thumbs" where I put all my thumbnails in.

I hope you can help me out with this last fix, thanks a lot in advance!

PS. You said in the video there is a option that we can download some free version.
Although I can't find it on the website.
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: PHP Tutorial: Automatic Image Gallery [part 01]

Post by jacek »

jan24 wrote:But I want to put the "gallery.php" in the root folder then a folder "/img" where I put all big ones in and after that "/img/thumbs" where I put all my thumbnails in.
To do that you would just need to make sure to add /img/ before every path, if you knew what was going on as you made it you should not have too many problems.
jan24 wrote:PS. You said in the video there is a option that we can download some free version.
Although I can't find it on the website.
It probably doesn't exist anymore :(
Image
jan24
Posts: 8
Joined: Sun Jan 29, 2012 6:05 pm

Re: PHP Tutorial: Automatic Image Gallery [part 01]

Post by jan24 »

Well the problem is.
When I going to type everwhere img/ for it will not solve it.

For example when I type it here:
$images = glob("img/*.{jpg,JPG,jpeg,JPEG,png,PNG,gif,GIF}", GLOB_BRACE);
It will give problems at the foreach:
foreach($images as $image){
	if(file_exists("img/thumbs/{$image}")){
		echo "<a href=\"{$image}\"><img src=\"img/thumbs/{$image}\" alt=\"{$image}\" /></a>";
Here it only shows the imageboxes where the image should be and give such icon that they can't find the picture.

So if I skip it at this line like this:
$images = glob("*.{jpg,JPG,jpeg,JPEG,png,PNG,gif,GIF}", GLOB_BRACE);
And then do this:
foreach($images as $image){
	if(file_exists("img/thumbs/{$image}")){
		echo "<a href=\"{$image}\"><img src=\"img/thumbs/{$image}\" alt=\"{$image}\" /></a>";
It totally doesn't show any images also no error.

Hope you still can help me out.
Thanks in advance.

Greetz,
Jan24
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: PHP Tutorial: Automatic Image Gallery [part 01]

Post by jacek »

You want to add the img here
    $images = glob("img/*.{jpg,JPG,jpeg,JPEG,png,PNG,gif,GIF}", GLOB_BRACE);
And then not add it here
echo "<a href=\"{$image}\"><img src=\"img/thumbs/{$image}\" alt=\"{$image}\" /></a>";
So it should be
    $images = glob("img/*.{jpg,JPG,jpeg,JPEG,png,PNG,gif,GIF}", GLOB_BRACE);
echo "<a href=\"{$image}\"><img src=\"thumbs/{$image}\" alt=\"{$image}\" /></a>";
Image
jan24
Posts: 8
Joined: Sun Jan 29, 2012 6:05 pm

Re: PHP Tutorial: Automatic Image Gallery [part 01]

Post by jan24 »

When I do this:
$images = glob("img/*.{jpg,JPG,jpeg,JPEG,png,PNG,gif,GIF}", GLOB_BRACE);
And this:
echo "<a href=\"{$image}\"><img src=\"thumbs/{$image}\" alt=\"{$image}\" /></a>";
It still doesn't work.
When I gonna watch the source code it displays this:
<a href="img/pic001.jpg"><img src="thumbs/img/pic001.jpg" alt="img/pic001.jpg" /></a>
The "img" and the "thumbs" need to be switch from place.
I tried already many ways, but didn't found a solution yet.

Hope you still can help me out.
Thanks in advance.
Greetz,

Jan24
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: PHP Tutorial: Automatic Image Gallery [part 01]

Post by jacek »

Ah okay, this is an awkward one then.

You need to get just the file names so you can set the paths manually further down, to do that you can make use of basename()
foreach (glob("img/*.{jpg,JPG,jpeg,JPEG,png,PNG,gif,GIF}", GLOB_BRACE) as $file){
    $images[] = basename($file);
}
That will just give you the filenames so then you can use the right folders. Give it a go and see if you get anywhere :)
Image
jan24
Posts: 8
Joined: Sun Jan 29, 2012 6:05 pm

Re: PHP Tutorial: Automatic Image Gallery [part 01]

Post by jan24 »

Now I got one last problem: when I now add a new picture to the folder "img", it doesn't make a thumbnail.

So whats working now:
Displays the thumbnails pictures.
Does the URL to big picture correctly.

What doesn't work now:
It doesn't make new thumbnails.

Hope you can help me out again.
Thanks in advance.
Greetz,

Jan24
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: PHP Tutorial: Automatic Image Gallery [part 01]

Post by jacek »

Post the code...
Image
jan24
Posts: 8
Joined: Sun Jan 29, 2012 6:05 pm

Re: PHP Tutorial: Automatic Image Gallery [part 01]

Post by jan24 »

My code:
            <?php
				if(is_dir('img/thumbs') === false){
					mkdir('img/thumbs', 0744);
				}
				if(isset($_GET["img"])){
					if(file_exists("img/".$_GET["img"])){
						ignore_user_abort(true);
						set_time_limit(120);
						ini_set('memory_limit', '512M');
						
						$src_size = getimagesize("img/".$_GET["img"]);
						
						if($src_size === false){
							die("Geen afbeelding...");
						}
						
						$thumb_width	= 150;
						$thumb_height	= 100;
						
						if($src_size["mime"] === "image/jpeg"){
							$src = imagecreatefromjpeg("img/{$_GET['img']}");
							//$src = imagecreatefromjpeg("".$_GET["img"]);
						}else if($src_size["mime"] === "image/png"){
							$src = imagecreatefrompng("img/{$_GET['img']}");
							//$src = imagecreatefrompng("".$_GET["img"]);
						}else if($src_size["mime"] === "image/gif"){
							$src = imagecreatefromgif("img/{$_GET['img']}");
							//$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) * ($src_size[1] / $new_size[1])) / 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) * ($src_size[0] / $new_size[0])) / 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;
						
						print_r($new_size);
						print_r($src_pos);
						
						$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, "img/thumbs/{$_GET['img']}");
						}else if($src_size["mime"] === "image/png"){
							imagepng($thumb, "img/thumbs/{$_GET['img']}");
						}else if($src_size["mime"] === "image/gif"){
							imagegif($thumb, "img/thumbs/{$_GET['img']}");
						}
						
						header("Location: img/thumbs/{$_GET['img']}");
					}
					echo "Script died";
				}
				//$images = glob("*.{jpg,JPG,jpeg,JPEG,png,PNG,gif,GIF}", GLOB_BRACE);
				foreach (glob("img/*.{jpg,JPG,jpeg,JPEG,png,PNG,gif,GIF}", GLOB_BRACE) as $file){
					$images[] = basename($file);
					
				}
				foreach($images as $image){
					if(file_exists("img/{$image}")){
						echo "<a href=\"img/{$image}\"><img src=\"img/thumbs/{$image}\" alt=\"{$image}\" /></a>";
					}else{
						echo "<a href=\"img/{$image}\"><img src=\"?img=img/{$image}\" alt=\"{$image}\" /></a>";
					}
				}
			?>
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: PHP Tutorial: Automatic Image Gallery [part 01]

Post by jacek »

In your img tag you have the src as
?img=img/{$image}
but then in the code to generate the thumb you add another img/
Image
jan24
Posts: 8
Joined: Sun Jan 29, 2012 6:05 pm

Re: PHP Tutorial: Automatic Image Gallery [part 01]

Post by jan24 »

I guess u meen like this?
 echo "<a href=\"img/{$image}\"><img src=\"?img=img/img/{$image}\" alt=\"{$image}\" /></a>";
But then it totally doesn't works.

It does makes the folder "thumbs" though.
Only he doesn't makes from the pictures thumbnails.

Thanks in advance.
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: PHP Tutorial: Automatic Image Gallery [part 01]

Post by jacek »

you did the opposite of what I meant ;)
Image
jan24
Posts: 8
Joined: Sun Jan 29, 2012 6:05 pm

Re: PHP Tutorial: Automatic Image Gallery [part 01]

Post by jan24 »

Alright now I did this:
 echo "<a href=\"img/{$image}\"><img src=\"?img={$image}\" alt=\"{$image}\" /></a>";
And that seems to work!
Thanks a lot.
I'm sorry all the trouble.
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: PHP Tutorial: Automatic Image Gallery [part 01]

Post by jacek »

that looks perfect :D
Image
Post Reply