Page 1 of 1

Image Gallery

Posted: Fri Jan 06, 2012 6:17 pm
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>

Re: Image Gallery

Posted: Sun Jan 08, 2012 12:40 am
by jacek
What does it do ?

Re: Image Gallery

Posted: Sun Jan 08, 2012 6:35 am
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.

Re: Image Gallery

Posted: Mon Jan 09, 2012 9:44 pm
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 ?

Re: Image Gallery

Posted: Tue Jan 10, 2012 4:00 pm
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.

Re: Image Gallery

Posted: Wed Jan 11, 2012 12:26 am
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.

Re: Image Gallery

Posted: Thu Jan 12, 2012 2:48 pm
by Mindvision
Nothing happens.It just reload the same page see.

Image

Re: Image Gallery

Posted: Fri Jan 13, 2012 2:03 pm
by jacek
Sorry, file should have been img int he url.

Re: Image Gallery

Posted: Fri Jan 13, 2012 2:53 pm
by Mindvision
Didn't understand :?

Re: Image Gallery

Posted: Fri Jan 13, 2012 2:55 pm
by Mindvision
The requested URL /BetterPhp/Gallery/thumbs/1.jpg was not found on this server.

Re: Image Gallery

Posted: Fri Jan 13, 2012 3:11 pm
by jacek
Sorry, I mean go to gallery.php?img=1.jpg :D

Re: Image Gallery

Posted: Sat Jan 14, 2012 11:37 am
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.

Re: Image Gallery

Posted: Sat Jan 14, 2012 1:58 pm
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.

Re: Image Gallery

Posted: Sun Jan 15, 2012 8:33 am
by Mindvision
Thanks i fixed those errors :)

Re: Image Gallery

Posted: Sun Jan 15, 2012 7:57 pm
by jacek
Mindvision wrote:Thanks i fixed those errors :)
:D