Problem on PHP Tutorial: Automatic Image Gallery tutorial

Ask about a PHP problem here.
Post Reply
ijohnrussel
Posts: 8
Joined: Sun Oct 14, 2012 8:45 am

Problem on PHP Tutorial: Automatic Image Gallery tutorial

Post by ijohnrussel »

Hi master's of PHP, I have a problem with one of your tutorials about automatic image galleries.
Maybe its just me, but every time I load the page the error icons show instead of the actual image.
Please help me :)
Problem ..
Problem ..
problem.png (13.45 KiB) Viewed 2499 times
Last edited by Helx on Sun Oct 14, 2012 9:40 am, edited 2 times in total.
Reason: Moved to PHP subforum, more gramatical issues
User avatar
Helx
Posts: 350
Joined: Thu May 17, 2012 6:45 am
Location: Auckland, New Zealand

Re: Problem on PHP Tutorial: Automatic Image Gallery tutoria

Post by Helx »

Could you right click on the page and click 'view source'

Just paste a snippet of whats there, not the whole thing. :)
ijohnrussel
Posts: 8
Joined: Sun Oct 14, 2012 8:45 am

Re: Problem on PHP Tutorial: Automatic Image Gallery tutoria

Post by ijohnrussel »

<?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 ('That does not look like an image');
		}
		
		$thumb_width = 1100;
		$thumb_height = 1100;
		
		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[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);

?>

<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
		<style stype="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=\"\"><img src=\"images/{$images}\" alt\"{$images}\" /></a>";
				}else{
					echo "<a href=\"\"><img src=\"?img/{$images}\" alt\"{$images}\" /></a>";
					
					
				
				}
			}
			
			?>
		
		</div>
	</body>
</html>
I'll just send the codes I copy on tutorial...
Because when I view the source code only the HTML code is shown alongside the alt array thing...
Thanks.

I'm making a photo sharing website and I include this thing into my page so that people, well,
know who's currently registered on my webpage :)

Thanks
Last edited by Helx on Sun Oct 14, 2012 9:11 am, edited 2 times in total.
Reason: Code tags.. English...
User avatar
Helx
Posts: 350
Joined: Thu May 17, 2012 6:45 am
Location: Auckland, New Zealand

Re: Problem on PHP Tutorial: Automatic Image Gallery tutoria

Post by Helx »

You posted the source of the page them removed it?
Could you please post it again in
 tags.

I'm having trouble trying to figure out what your problem with this script is :)
ijohnrussel
Posts: 8
Joined: Sun Oct 14, 2012 8:45 am

Re: Problem on PHP Tutorial: Automatic Image Gallery tutoria

Post by ijohnrussel »

<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
		<style stype="text/css">
			a, img{float:left;}
		</style>
		<title>Image Gallery</title>
	</head>
	<body>
		<div>
			<a href=""><img src="?img/Array" alt"Array" /></a>
                        <a href=""><img src="?img/Array" alt"Array" /></a>
			<a href=""><img src="?img/Array" alt"Array" /></a>
                        <a href=""><img src="?img/Array" alt"Array" /></a>
			<a href=""><img src="?img/Array" alt"Array" /></a>
                        <a href=""><img src="?img/Array" alt"Array" /></a>
			<a href=""><img src="?img/Array" alt"Array" /></a>
                        <a href=""><img src="?img/Array" alt"Array" /></a>
			<a href=""><img src="?img/Array" alt"Array" /></a>
                        <a href=""><img src="?img/Array" alt"Array" /></a>
			<a href=""><img src="?img/Array" alt"Array" /></a>
                        <a href=""><img src="?img/Array" alt"Array" /></a>
			<a href=""><img src="?img/Array" alt"Array" /></a>
                        <a href=""><img src="?img/Array" alt"Array" /></a>
			<a href=""><img src="?img/Array" alt"Array" /></a>
                        <a href=""><img src="?img/Array" alt"Array" /></a>
			<a href=""><img src="?img/Array" alt"Array" /></a>
                        <a href=""><img src="?img/Array" alt"Array" /></a>
			<a href=""><img src="?img/Array" alt"Array" /></a>
                        <a href=""><img src="?img/Array" alt"Array" /></a>
			<a href=""><img src="?img/Array" alt"Array" /></a>
                        <a href=""><img src="?img/Array" alt"Array" /></a>
		</div>
	</body>
</html>

Here it is PHP master! :)
I removed it because i thought this was like spamming code because its repeating..
Please help me.

I still got a lot of problems like sharing photos in a wall for everybody how to like the picuture and put a comment,
I cant see any tutorials thats why i need to fix this first :D
Last edited by Helx on Sun Oct 14, 2012 9:38 am, edited 3 times in total.
Reason: Added code tags, fixed pedantic spelling errors
User avatar
Helx
Posts: 350
Joined: Thu May 17, 2012 6:45 am
Location: Auckland, New Zealand

Re: Problem on PHP Tutorial: Automatic Image Gallery tutoria

Post by Helx »

I'm still a tad unsure,
But try editing line 87;

Try replacing <img src=\"?img/{$images}\" alt\"{$images}\" />
With <img src=\"?img={$images}\" alt\"{$images}\" />

(That was ?img/{$images} to ?img={$images})

Query strings (the question marks in URLs) are not like directories. They use the equals operator.
ijohnrussel
Posts: 8
Joined: Sun Oct 14, 2012 8:45 am

Re: Problem on PHP Tutorial: Automatic Image Gallery tutoria

Post by ijohnrussel »

Still not working, I have same problem with the small icon showing on the page...
Thanks for the reply :D
User avatar
Helx
Posts: 350
Joined: Thu May 17, 2012 6:45 am
Location: Auckland, New Zealand

Re: Problem on PHP Tutorial: Automatic Image Gallery tutoria

Post by Helx »

Hmm,

Get rid of the line with $image in it,
and replace it with
foreach(glob("*.{jpg,JPG,jpeg,JPEG,png,PNG,gif,GIF}", GLOB_BRACE) as $file){
	$image[] = basename($file);		
}
And lower down in the file, around line 83 down, change those {$images} to {$image}.

I think the problem was that you needed to state each part of the array, or it will just return the words 'Array".
ijohnrussel
Posts: 8
Joined: Sun Oct 14, 2012 8:45 am

Re: Problem on PHP Tutorial: Automatic Image Gallery tutoria

Post by ijohnrussel »

<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
		<style stype="text/css">
			a, img{float:left;}
		</style>
		<title>Image Gallery</title>
	</head>
	<body>
		<div>
			<a href=""> <img src="?img=5054193870_a648423b1e_o.jpg" alt"5054193870_a648423b1e_o.jpg" /></a><a href=""> <img src="?img=5106897146_98edafd5a1_o.jpg" alt"5106897146_98edafd5a1_o.jpg" /></a><a href=""> <img src="?img=5121878938_96735ba65f_o.jpg" alt"5121878938_96735ba65f_o.jpg" /></a><a href=""> <img src="?img=A little motivation 1024x768.jpg" alt"A little motivation 1024x768.jpg" /></a><a href=""> <img src="?img=ACE 1.jpg" alt"ACE 1.jpg" /></a><a href=""> <img src="?img=HOTTEST PAPERS ON THE NET (30).jpg" alt"HOTTEST PAPERS ON THE NET (30).jpg" /></a><a href=""> <img src="?img=Hydrangeas.jpg" alt"Hydrangeas.jpg" /></a><a href=""> <img src="?img=Jellyfish.jpg" alt"Jellyfish.jpg" /></a><a href=""> <img src="?img=Lighthouse.jpg" alt"Lighthouse.jpg" /></a><a href=""> <img src="?img=Untitled-2.jpg" alt"Untitled-2.jpg" /></a><a href=""> <img src="?img=anonymous-mask.jpg" alt"anonymous-mask.jpg" /></a><a href=""> <img src="?img=ep3ace.jpg" alt"ep3ace.jpg" /></a><a href=""> <img src="?img=fischfanger___think-t2.jpg" alt"fischfanger___think-t2.jpg" /></a><a href=""> <img src="?img=graffiti_technica_wallpaper_full.jpg" alt"graffiti_technica_wallpaper_full.jpg" /></a><a href=""> <img src="?img=hybridtech.jpg" alt"hybridtech.jpg" /></a><a href=""> <img src="?img=images.jpg" alt"images.jpg" /></a><a href=""> <img src="?img=micro.jpg" alt"micro.jpg" /></a><a href=""> <img src="?img=rainbow.jpg" alt"rainbow.jpg" /></a><a href=""> <img src="?img=tech.jpg" alt"tech.jpg" /></a><a href=""> <img src="?img=tech1.jpg" alt"tech1.jpg" /></a><a href=""> <img src="?img=tech3.jpg" alt"tech3.jpg" /></a><a href=""> <img src="?img=redline_f.JPG" alt"redline_f.JPG" /></a>		
		</div>
	</body>
</html>


i thnk its almost going fine... now this is the new problem its almost done.. ;)
Last edited by Temor on Sun Oct 14, 2012 4:32 pm, edited 1 time in total.
Reason: Tags
User avatar
Helx
Posts: 350
Joined: Thu May 17, 2012 6:45 am
Location: Auckland, New Zealand

Re: Problem on PHP Tutorial: Automatic Image Gallery tutoria

Post by Helx »

... Is it displaying the images?
ijohnrussel
Posts: 8
Joined: Sun Oct 14, 2012 8:45 am

Re: Problem on PHP Tutorial: Automatic Image Gallery tutoria

Post by ijohnrussel »

only small icons :|
User avatar
Helx
Posts: 350
Joined: Thu May 17, 2012 6:45 am
Location: Auckland, New Zealand

Re: Problem on PHP Tutorial: Automatic Image Gallery tutoria

Post by Helx »

Small icons of the images?
Or the error things?
ijohnrussel
Posts: 8
Joined: Sun Oct 14, 2012 8:45 am

Re: Problem on PHP Tutorial: Automatic Image Gallery tutoria

Post by ijohnrussel »

small icons like on the first post i made with the attach image..
User avatar
Temor
Posts: 1186
Joined: Thu May 05, 2011 8:04 pm

Re: Problem on PHP Tutorial: Automatic Image Gallery tutoria

Post by Temor »

      <?php
                       
                        foreach($images as $image){
                                if (file_exists("./thumbs{$image}")){
                                        echo "<a href=\"\"><img src=\"images/{$images}\" alt\"{$images}\" /></a>";
                                }else{
                                        echo "<a href=\"\"><img src=\"?img/{$images}\" alt\"{$images}\" /></a>";
                                       
                                       
                               
                                }
                        }
                       
                        ?>
$images is an array. You need to use $image when you echo.
      <?php
                       
                        foreach($images as $image){
                                if (file_exists("./thumbs{$image}")){
                                        echo "<a href=\"\"><img src=\"images/{$image}\" alt\"{$image}\" /></a>";
                                }else{
                                        echo "<a href=\"\"><img src=\"?img/{$image}\" alt\"{$image}\" /></a>";
                                       
                                       
                               
                                }
                        }
                       
                        ?>
I am also fairly sure that you need a slash between thumbs and {$image} here:
 if (file_exists("./thumbs{$image}")){
don't quote me on that though. Try it for yourself.
ijohnrussel
Posts: 8
Joined: Sun Oct 14, 2012 8:45 am

Re: Problem on PHP Tutorial: Automatic Image Gallery tutoria

Post by ijohnrussel »

i follow all what you have told me but i have still the same problem.. same with the first image post i did... :oops:
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Problem on PHP Tutorial: Automatic Image Gallery tutoria

Post by jacek »

ijohnrussel wrote:i follow all what you have told me but i have still the same problem.. same with the first image post i did... :oops:
If you look at the image location "?img=5054193870_a648423b1e_o.jpg" it looks right, so if you paste that into your browser to actually view the thumbnail it might tell you why it's not generating one.
Image
Post Reply