Image Gallery

Post here is you are having problems with any of the tutorials.
Post Reply
Mindvision
Posts: 8
Joined: Thu Jan 05, 2012 10:38 am

Image Gallery

Post by Mindvision »

can anyone tell me where is the problem in this script?
it doesn't create images in thumb folder .

<?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 look like not an image.');
	}
	
	$thumb_width  = 250;
	$thumb_height = 200;
	
	
	
	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_heigth),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) * ($src_size[1] / $new_size[1])) / 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) * ($src_size[0] / $new_size[0])) /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_heigth);
  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);


?>
<!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" />
<style type="text/css">
a, img{ float:left; }
</style>
<title>Php Gallery By Mindvision</title>
</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>
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Image Gallery

Post by jacek »

What does it do ?
Image
Mindvision
Posts: 8
Joined: Thu Jan 05, 2012 10:38 am

Re: Image Gallery

Post by Mindvision »

I follow the tutorial of automatic image gallery but my problem is that the script is not converting the images from root folder to thumbs folder.
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Image Gallery

Post by jacek »

Mindvision wrote:I follow the tutorial of automatic image gallery but my problem is that the script is not converting the images from root folder to thumbs folder.
Okay, what does it do instead of that ? Do you see any errors ? Do the images get created at all ?
Image
Mindvision
Posts: 8
Joined: Thu Jan 05, 2012 10:38 am

Re: Image Gallery

Post by Mindvision »

no error. it only show the name of images and when i click on the image link it will open the image in default resolution and thumbs folder will be empty.
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Image Gallery

Post by jacek »

Okay, if you open the location of the thumbnail in your browser you will probably see the error hat is causing it.

So go to index.php?file=something.jpg replacing something.jpg with a file that actually exists. You may need top comment out the line
header("Location: thumbs/{$_GET['img']}");  
to be able to see the error before you get redirected.
Image
Mindvision
Posts: 8
Joined: Thu Jan 05, 2012 10:38 am

Re: Image Gallery

Post by Mindvision »

Nothing happens.It just reload the same page see.

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

Re: Image Gallery

Post by jacek »

Sorry, file should have been img int he url.
Image
Mindvision
Posts: 8
Joined: Thu Jan 05, 2012 10:38 am

Re: Image Gallery

Post by Mindvision »

Didn't understand :?
Mindvision
Posts: 8
Joined: Thu Jan 05, 2012 10:38 am

Re: Image Gallery

Post by Mindvision »

The requested URL /BetterPhp/Gallery/thumbs/1.jpg was not found on this server.
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Image Gallery

Post by jacek »

Sorry, I mean go to gallery.php?img=1.jpg :D
Image
Mindvision
Posts: 8
Joined: Thu Jan 05, 2012 10:38 am

Re: Image Gallery

Post by Mindvision »

This error is coming because there is no image file in thumbs folder .

The requested URL /BetterPhp/Gallery/thumbs/1.jpg was not found on this server.
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Image Gallery

Post by jacek »

Mindvision wrote:This error is coming because there is no image file in thumbs folder .

The requested URL /BetterPhp/Gallery/thumbs/1.jpg was not found on this server.
Yeah that is why you need to remove the redirect line. At the moment you are not seeing the error because you are being redirected.
Image
Mindvision
Posts: 8
Joined: Thu Jan 05, 2012 10:38 am

Re: Image Gallery

Post by Mindvision »

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

Re: Image Gallery

Post by jacek »

Mindvision wrote:Thanks i fixed those errors :)
:D
Image
Post Reply