Page 1 of 1

Auto Image tutorial

Posted: Sun Apr 01, 2012 12:55 pm
by wilse
Hi Jacek,

I'm stuck with your Auto Image tutorial.
I have a thumbnails folder created OK but no thumbnails in it and no images displaying.

In the bit in video where you click on the broken image link and the browser address bar has something ending with ".png", mine all say the same thing, "%7Bimage%7D" instead of ".jpg" or ".png" or whatever.

Dunno if that gives any clues?

Anyway, thanks for the videos. I'm just trying to learn this and you provide a great resource.

Cheers,
Robert.

-EDIT-

Here's 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', '512');
			
			$src_size = getimagesize($_GET['img']);
			
			echo "aye";
			
			
			if ($src_size === false)
			{
				die ("That is not a photie ya chancer.");
			}
			
			//set thumbnail dimensions
			$thumb_width  = 150;
			$thumb_height = 150;
			
			if ($src_size['mime'] === image/jpeg)
			{
				$src = imagecreatefromjpeg($_GET['img']);
			}
			else if ($src_size['mime'] === image/gif)
			{
				$src = imagecreatefromgif($_GET['img']);
			}
			else if ($src_size['mime'] === image/png)
			{
				$src = imagecreatefrompng($_GET['img']);
			}

			//define aspect ratios using width over height
			$src_aspect = round(($src_size[0] / $src_size[1]), 1);
			$thumb_aspect = round(($thumb_width / $thumb_height), 1);
						
			//trimming
			if ($src_aspect < $thumb_aspect)
			{ //too high
				$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)
			{ //too wide
				$new_size = array(($thumb_width / $src_size[1]) * $src_size[0], $thumb_height);
				$src_pos = array((($new_size[0] - $thumb_width) * ($src_size[1] / $new_size[1])) / 2, 0);
			}
			else
			{ //same
				$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/gif)
			{
				imagegif($thumb, "thumbs/{$_GET['img']}");
			}
			else if ($src_size['mime'] === image/png)
			{
				imagepng($thumb, "thumbs/{$_GET['img']}");
			}
			
//			header ("Location: thumbs/{$_GET['img']}");
		}
		
		die();
	}
else echo "help";	

//check to see if thumbnail folder needs to be created
if (is_dir('./thumbs') === false) 
{
		mkdir('./thumbs', 0744);	
}	

//return an array of image files
$images = glob('*.{jpg,JPG,jpeg,png,gif}', GLOB_BRACE);

?>

<html>
	<head>
	<style type = "text/css">
		a, img {float: left}
	</style>	
	<h1>Abel Image Gallery</h1>
	</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: Auto Image tutorial

Posted: Sun Apr 01, 2012 5:42 pm
by jacek
First thing I spotted was tha oyu missed a $ from here
<a href=\"{image}\">
but that should not cause the problem you see.

The problem here is that any errors will be hidden, try browsing to index.php?img=file_name.jpg (replace the file name with one that actually exists). this should show you the reason why the image is not being created. you may need to comment out the header() lines to be abl to see the message,

Re: Auto Image tutorial

Posted: Mon Apr 02, 2012 10:06 am
by wilse
jacek wrote:First thing I spotted was tha oyu missed a $ from here
<a href=\"{image}\">
but that should not cause the problem you see.
Whoops, thanks! That certainly helped a bit. =)

The broken images now have ".jpg" extensions and clicking on them now displays them.
The problem here is that any errors will be hidden, try browsing to index.php?img=file_name.jpg (replace the file name with one that actually exists). this should show you the reason why the image is not being created. you may need to comment out the header() lines to be abl to see the message,
I commented out "header ("Location: thumbs/{$_GET['img']}");" but it didn't make a difference.

It seemed as though nothing in the "if (isset($_GET['img']))" statement was being reached so I added a line at the end of the 'if' statement, "else echo "help";

Sure enough, 'help' appears on screen. So I'm thinking the issue might be to with that part?

Thanks again,
Robert.

Re: Auto Image tutorial

Posted: Tue Apr 03, 2012 1:38 am
by jacek
Which URL are you going to to test it ? There should be a file name in the url ? If there is make sure you have E_NOTICE level errors shown (by setting your error_reporting setting to E_ALL), that should show up any typos with variable names.

Re: Auto Image tutorial

Posted: Tue Apr 03, 2012 11:38 am
by wilse
Hi Jacek,

thanks for the reply.
jacek wrote:Which URL are you going to to test it ? There should be a file name in the url ?
There are a number of them but here's an example:
http://localhost:8888/abelsoul/images/Ace.jpg


If there is make sure you have E_NOTICE level errors shown (by setting your error_reporting setting to E_ALL), that should show up any typos with variable names.
I added the line:

error_reporting(E_ALL);

to the next line of the file after "<?php" and commented out the header line but nothing's changed.

Re: Auto Image tutorial

Posted: Tue Apr 03, 2012 7:24 pm
by jacek
wilse wrote:There are a number of them but here's an example:
http://localhost:8888/abelsoul/images/Ace.jpg
Ah okay, that is the location of the full size image, you need to go to the URL that would create the thumbnail. It should be something like index.php?img=Ace.jpg

Re: Auto Image tutorial

Posted: Tue Apr 03, 2012 8:27 pm
by wilse
jacek wrote:
wilse wrote:There are a number of them but here's an example:
http://localhost:8888/abelsoul/images/Ace.jpg
Ah okay, that is the location of the full size image, you need to go to the URL that would create the thumbnail. It should be something like index.php?img=Ace.jpg
OK, I went to:

http://localhost:8888/abelsoul/images/g ... mg=Ace.jpg

-EDIT-
The URL ends in:

images/gallery.php?img=Ace.jpg

And all that appears on screen is "aye" which was from a line I had inserted earlier to try to track down errors.
(line 14 of the code in my first post).

If I remove that line I get a blank page.

Re: Auto Image tutorial

Posted: Thu Apr 05, 2012 12:59 pm
by jacek
Just spotted
                        if ($src_size['mime'] === image/jpeg)
                        {
                                imagejpeg($thumb, "thumbs/{$_GET['img']}");
                        }
                        else if ($src_size['mime'] === image/gif)
                        {
                                imagegif($thumb, "thumbs/{$_GET['img']}");
                        }
                        else if ($src_size['mime'] === image/png)
                        {
                                imagepng($thumb, "thumbs/{$_GET['img']}");
                        }
The types here should have quotes around them
                        if ($src_size['mime'] === 'image/jpeg')
for example.

That should have thrown a syntax error which must mean you have error reporting turned off ?