Page 1 of 1

Problem on PHP Tutorial: Automatic Image Gallery tutorial

Posted: Sun Oct 14, 2012 8:53 am
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 2253 times

Re: Problem on PHP Tutorial: Automatic Image Gallery tutoria

Posted: Sun Oct 14, 2012 8:59 am
by Helx
Could you right click on the page and click 'view source'

Just paste a snippet of whats there, not the whole thing. :)

Re: Problem on PHP Tutorial: Automatic Image Gallery tutoria

Posted: Sun Oct 14, 2012 9:07 am
by ijohnrussel
[syntax=php]<?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>
[/syntax]

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

Re: Problem on PHP Tutorial: Automatic Image Gallery tutoria

Posted: Sun Oct 14, 2012 9:13 am
by Helx
You posted the source of the page them removed it?
Could you please post it again in [syntax=text] tags.

I'm having trouble trying to figure out what your problem with this script is :)

Re: Problem on PHP Tutorial: Automatic Image Gallery tutoria

Posted: Sun Oct 14, 2012 9:17 am
by ijohnrussel
[syntax=text]<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>[/syntax]


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

Re: Problem on PHP Tutorial: Automatic Image Gallery tutoria

Posted: Sun Oct 14, 2012 9:23 am
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.

Re: Problem on PHP Tutorial: Automatic Image Gallery tutoria

Posted: Sun Oct 14, 2012 9:28 am
by ijohnrussel
Still not working, I have same problem with the small icon showing on the page...
Thanks for the reply :D

Re: Problem on PHP Tutorial: Automatic Image Gallery tutoria

Posted: Sun Oct 14, 2012 9:35 am
by Helx
Hmm,

Get rid of the line with $image in it,
and replace it with

[syntax=php]foreach(glob("*.{jpg,JPG,jpeg,JPEG,png,PNG,gif,GIF}", GLOB_BRACE) as $file){
$image[] = basename($file);
}[/syntax]

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".

Re: Problem on PHP Tutorial: Automatic Image Gallery tutoria

Posted: Sun Oct 14, 2012 9:45 am
by ijohnrussel
[syntax=xhtml]<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>[/syntax]



i thnk its almost going fine... now this is the new problem its almost done.. ;)

Re: Problem on PHP Tutorial: Automatic Image Gallery tutoria

Posted: Sun Oct 14, 2012 9:47 am
by Helx
... Is it displaying the images?

Re: Problem on PHP Tutorial: Automatic Image Gallery tutoria

Posted: Sun Oct 14, 2012 9:51 am
by ijohnrussel
only small icons :|

Re: Problem on PHP Tutorial: Automatic Image Gallery tutoria

Posted: Sun Oct 14, 2012 9:53 am
by Helx
Small icons of the images?
Or the error things?

Re: Problem on PHP Tutorial: Automatic Image Gallery tutoria

Posted: Sun Oct 14, 2012 9:55 am
by ijohnrussel
small icons like on the first post i made with the attach image..

Re: Problem on PHP Tutorial: Automatic Image Gallery tutoria

Posted: Sun Oct 14, 2012 4:35 pm
by Temor
[syntax=php] <?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>";



}
}

?>[/syntax]

$images is an array. You need to use $image when you echo.

[syntax=php] <?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>";



}
}

?>[/syntax]

I am also fairly sure that you need a slash between thumbs and {$image} here:
[syntax=php] if (file_exists("./thumbs{$image}")){[/syntax]

don't quote me on that though. Try it for yourself.

Re: Problem on PHP Tutorial: Automatic Image Gallery tutoria

Posted: Sun Oct 21, 2012 2:59 am
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:

Re: Problem on PHP Tutorial: Automatic Image Gallery tutoria

Posted: Sat Oct 27, 2012 9:44 pm
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.