thumbnails

A

Anonymous

Guest
Hi
I have this function I call for creating thumbnails
Code:
<?php
 chdir("uploads");
function create_thumbs(){
  
   if (isset($_GET['img'])) {
               
	 $src_size = getimagesize($_GET['img']);

             if($src_size === false){
	      die("This is not an image");
             }
        
	 $thumb_width   = 120;
	 $thumb_height  = 140;
	  	  
	if ($src_size['mime'] === 'image/jpeg') {
	     $src=imagecreatefromjpeg($_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) {
	     $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){
	     $new_size= array(($thumb_width / $src_size[1])* $src_size[0], $thumb_height);
		 $src_pos =array(($new_size[0] - $thumb_width) /2, 0);
	}else{
	     $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']}"));
	  }
       
     header("Location: thumbs/{$_GET['img']}");
    }  
  }
$images = glob('*.{jpg,jpeg,gif,png}',GLOB_BRACE);

?> 
<html>
    <style>
      a,img{float:left;}
    </style>
         <div>
          <?php
	  foreach($images as $image) {
	     if(file_exists("./thumbs/{$image}")) {
		echo"<a href=\"{$image}\"><img scr=\"thumbs/{$image}\" alt=\"{$image}\" /></a>";
	     }else{
                echo "<a href=\"{$image}\"><img src=\"?img={$image}\" alt=\"{$image}\" /></a>";
		
           }

	}		 
	?>
As you can see it creates thumbs and it is supose to display them on the screen but it does not.
It creates the thumbs puts them in the thumbs dir, but does not display them. It looks like it fails on this line
Code:
if(file_exists("./thumbs/{$image}")) {
because it displays the names of the images on the page, if I edit any of the lines in this area
Code:
$images = glob('*.{jpg,jpeg,gif,png}',GLOB_BRACE);

?> 
<html>
    <style>
      a,img{float:left;}
    </style>
         <div>
          <?php
	  foreach($images as $image) {
	     if(file_exists("./thumbs/{$image}")) {
		echo"<a href=\"{$image}\"><img scr=\"thumbs/{$image}\" alt=\"{$image}\" /></a>";
	     }else{
                echo "<a href=\"{$image}\"><img src=\"?img={$image}\" alt=\"{$image}\" /></a>";
		
           }

	}
It will fail , there will be no thumbs in the thumbs dir and nothing on the page.

Can anyone see what I am doing wrong or what I have missed.

Thank you in advance for your help
 
Have you looked in your thumbs directory to see that you have created one successfully?

Your test:
Code:
if(file_exists("./thumbs/{$image}"))
to see if the file exists will still try to output a link to the image that you are trying to display, is this supposed to do something else?

Code:
chdir("uploads");
This is likely to give you problems, what are you trying to achieve by using it?

Have a good read through this
 
Back
Top