Automatic Image Gallery

Post here is you are having problems with any of the tutorials.
Romulas
Posts: 24
Joined: Mon Sep 12, 2011 3:59 am

Automatic Image Gallery

Post by Romulas »

Hi
I have been trying to make the automatic image gallery work for some time now and i am unable to find what is wrong
I hope someone can help, here is my code:
<?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("This is not an image");
	 }
	  
	  $thumb_width   = 200;
	  $thumb_heigth  = 200;
	  	  
	//print_r($src_size);
	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){
	     $newsize= 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,gif,png}',GLOB_BRACE);

?>


<html>
  <head>
    <meta http-equiv="Content Type" content="text/html; charset=utf=8" />
    <style type="text/css">
      a,img{float:left;}
    </style>
	<title>Image Gallery</title>
  </head>	
	<body>
	   <div>
	     <?php
		foreach($images as $image) {
		     if(file_exists("./thumbs/{$image}")) {
		     echo "<a href=\"{$image}\"><img scr=\"thumbs/{$image}\" alt=\"{$image}\" /></a>";
		 }else{
		      echo "<a href=\"{$image}\"><img scr=\"?img={$image}\" alt=\"{$image}\" /></a>"; 
		  }
		}
		 
		 ?>
		</div>
	</body>
</html>
All i get when i run this code is the blue rectangles with the file name written in them, it seems the the thumbnail part
does'nt work. The code does create the the thumbs folder.

At this point(code below) the Betterphp video outputs the blue rectangles with the file name written in them, mine outputs nothing.
However i don't see anything wrong with this part of the code
if(is_dir('./thumbs')===false){
 mkdir('./thumbs', 0744);
 }
 
 $images = glob('*.{jpg,jpeg,gif,png}',GLOB_BRACE);

?>
<html>
  <head>
    <meta http-equiv="Content Type" content="text/html; charset=utf=8" />
    <style type="text/css">
      a,img{float:left;}
    </style>
	<title>Image Gallery</title>
  </head>	
	<body>
	   <div>
	     <?php
		foreach($images as $image) {
		     if(file_exists("./thumbs/{$image}")) {
		     echo "<a href=\"{$image}\"><img scr=\"thumbs/{$image}\" alt=\"{$image}\" /></a>";
		 }else{
		      echo "<a href=\"{$image}\"><img scr=\"?img={$image}\" alt=\"{$image}\" /></a>"; 
		  }
		}
		 
		 ?>
		</div>
	</body>
Thank you
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Automatic Image Gallery

Post by jacek »

If you rightclick on the image and go to view image you might see the cause of the problem. You are probably getting a php error that is causing the image to be invalid.
Image
Romulas
Posts: 24
Joined: Mon Sep 12, 2011 3:59 am

Re: Automatic Image Gallery

Post by Romulas »

Hi jacek

I don't get your answer, there are no images to right clik on, except for those in the folder that i started with. Anyway i decided to start again and i got to this line
$images=glob(`*.{jpg,jpeg,gif,png}`,GLOB_BRACE);
When i run the program at this point using the print_r i get

Array([0]=>)

Thats not what was displayed on the video, However i can't see anything wrong with the code so far

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

Re: Automatic Image Gallery

Post by jacek »

I meant the gap where the images should be..

Those ` should be '

Thins surrounded by ` will be executed as a shell command.
Image
Romulas
Posts: 24
Joined: Mon Sep 12, 2011 3:59 am

Re: Automatic Image Gallery

Post by Romulas »

Sorry, but i still have no idea of what you want me to do,perhaps a pic would help

Thanks
User avatar
Temor
Posts: 1186
Joined: Thu May 05, 2011 8:04 pm

Re: Automatic Image Gallery

Post by Temor »

Romulas wrote:Sorry, but i still have no idea of what you want me to do,perhaps a pic would help

Thanks
You're supposed to change the backticks ( ` ) to single quotes ( ' )
Romulas
Posts: 24
Joined: Mon Sep 12, 2011 3:59 am

Re: Automatic Image Gallery

Post by Romulas »

Thanks
I removed all the back ticks but it did'nt fix the problem, the script sill does not work.

I think the snippet of code below has something to do with the problem, because when the author of the video executes the
print_r($src_size); it returns this:

Array([0]=> 320[1]=>480[2]=>3[3]=>width="320"height="480"[bit=>48[mime]=>image/png)

When i execute the same snippet i get nothing. Am i on the right track and what do i need to do to fix it?
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("This is not an image");
         }
         
          $thumb_width   = 200;
          $thumb_heigth  = 200;
                 
        print_r($src_size);
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Automatic Image Gallery

Post by jacek »

What are you passing in as $_GET['img'] ? Can you post the full code ?
Image
Romulas
Posts: 24
Joined: Mon Sep 12, 2011 3:59 am

Re: Automatic Image Gallery

Post by Romulas »

I posted all the code in my first post at the top of the page
What was passed in to $_GET['img'] in the video? . I did'nt
see that bit
Thanks
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Automatic Image Gallery

Post by jacek »

Romulas wrote:I posted all the code in my first post at the top of the page
I know, but you have made some changes now.
Romulas wrote:What was passed in to $_GET['img'] in the video? . I did'nt
You may have made a mistake.

Can you post the full code ?
Image
Romulas
Posts: 24
Joined: Mon Sep 12, 2011 3:59 am

Re: Automatic Image Gallery

Post by Romulas »

Hi
Here is all the code
 <?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("This is not an image");
	 }
	   $thumb_width   = 100;
	   $thumb_heigth  = 100;
	  	  
	//print_r($src_size);
	  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){
	     $newsize= 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,gif,png}',GLOB_BRACE);

?>


<html>
  <head>
    <meta http-equiv="Content Type" content="text/html; charset=utf=8" />
    <style type="text/css">
      a,img{float:left;}
    </style>
	<title>Image Gallery</title>
  </head>	
	<body>
	    <div>
	     <?php
		foreach($images as $image) {
		     if(file_exists("./thumbs/{$image}")) {
		     echo "<a href=\"{$image}\"><img scr=\"thumbs/{$image}\" alt=\"{$image}\" /></a>";
		 }else{
		      echo "<a href=\"{$image}\"><img scr=\"?img={$image}\" alt=\"{$image}\" /></a>"; 
		  }
		}
		 
		 ?>
		</div>
	</body>
</html>  
I watched the videos again and relised how you are passing to $_GET['img'], that was very subtle, at the time I thought you
where demonstarting something, when i tried that in firefox it did'nt work, firefox doesn't have those options in the
right click menu, so i added it into the address bar. the extra bit "?img=picture.jpg" (without the quotes) but it didn't work.
Is it possible that this only works in Chrome. I could be wrong i have been before.

Thank you jacek for your patience, i realy appreciate your help,
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Automatic Image Gallery

Post by jacek »

Romulas wrote:Is it possible that this only works in Chrome.
Not really since the image processing is done on the server and has nothing to do with the browser.
Romulas wrote: so i added it into the address bar. the extra bit "?img=picture.jpg" (without the quotes) but it didn't work.
What did you see when you did that ? In what way did it not work ?
Image
Romulas
Posts: 24
Joined: Mon Sep 12, 2011 3:59 am

Re: Automatic Image Gallery

Post by Romulas »

This is the address when i run the script, with all the broken image links visible
http://localhost/ImageGallery/gallery.php ( i addedd this)?img=picture.jpg
and i got this

Not Found
The requested URL /ImageGallery/thumbs/picture.jpg was not found on the server

Why is it looking in the thumbs directory for a thumb that has not been created?

Thats all i can tell you,I hope it helps
Thank you
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Automatic Image Gallery

Post by jacek »

On line 50 of the last code you posted, the browser is redirected to the file after it has been created.

Try commenting out the header line on line 50 and see if you see the cause of the file not being created then.
Image
Romulas
Posts: 24
Joined: Mon Sep 12, 2011 3:59 am

Re: Automatic Image Gallery

Post by Romulas »

Finally, i am getting somewhere commenting out that line has produced a stack of errors,

I have:
Undefined variable thumb_height line 25,31and 40
Divide by zero line 25
Undefined variable new_size line 31
Undefined offset 1 line 37 and 38
Warning imagecreatetruecolor() invalid image dimention line 40
Warning imagecpoyresampled() expects parameter 1 to be resourse line 41
Warning imagejpeg() expects parameter 1 to be resourse line 44

Any tips for fixing all these errors woul be appreciated

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

Re: Automatic Image Gallery

Post by jacek »

Ah now we are getting somewhere !

Most of those scary errors (divide by zero) will be caused by the undefined variables.
$thumb_heigth  = 100;
heigth should be height.
$newsize= array(($thumb_width / $src_size[1])* $src_size[0], $thumb_height);
newsize should be new size
Image
Romulas
Posts: 24
Joined: Mon Sep 12, 2011 3:59 am

Re: Automatic Image Gallery

Post by Romulas »

I have fixed all the errors and it produced a thumbnail(fantastc). The only problem now is
that it only produces the thumbnail i select by adding ?img=picture.jpg to the
addressbar. How can i make it produce all images automatically?
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Automatic Image Gallery

Post by jacek »

Romulas wrote:I have fixed all the errors and it produced a thumbnail(fantastc). The only problem now is
that it only produces the thumbnail i select by adding ?img=picture.jpg to the
addressbar. How can i make it produce all images automatically?
It should do that. It will only recreate the images if the thumbnail files don't exist already so you may need to remove the broken ones.
Image
Romulas
Posts: 24
Joined: Mon Sep 12, 2011 3:59 am

Re: Automatic Image Gallery

Post by Romulas »

I deleted the thumbnail and started again, by excuting the the script, should'nt that create thumbs for all the pictures in my case the ImageGallery folder. When you say "remove the broken ones", what do you mean?

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

Re: Automatic Image Gallery

Post by jacek »

If the script fails for some reason it will sometimes generate blank images in the thumb folder, having those there stops it generating new ones.

But if you deleted the that can't be it, can you post your code as it is now ?
Image
Romulas
Posts: 24
Joined: Mon Sep 12, 2011 3:59 am

Re: Automatic Image Gallery

Post by Romulas »

Here is the script
<?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("This is not an image");
	 }
	 $thumb_width   = 220;
	 $thumb_height  = 240;
	  	  
	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,gif,png}',GLOB_BRACE);

?>

<html>
  <head>
    <meta http-equiv="Content Type" content="text/html; charset=utf=8" />
    <style type="text/css">
      a,img{float:left;}
    </style>
	<title>Image Gallery</title>
  </head>	
	<body>
	
	    <div>
	     <?php
		foreach($images as $image) {
		     if(file_exists("./thumbs/{$image}")) {
		    echo "<a href=\"{$image}\"><img scr=\"thumbs/{$image}\" alt=\"{$image}\" /></a>";
		 }else{
		      echo "<a href=\"{$image}\"><img scr=\"?img={$image}\" alt=\"{$image}\" /></a>"; 
		  }
		}
		 
		 ?>
		</div>
	</body>
</html> 
Post Reply