simple image management

Small, short code snippets that other people may find useful. Do you have a good regex that you would like to share? Share it! Even better, the code can be commented on, and improved.

Moderator: General Moderators

Post Reply
magicrobotmonkey
Forum Regular
Posts: 888
Joined: Sun Mar 21, 2004 1:09 pm
Location: Cambridge, MA

simple image management

Post by magicrobotmonkey »

I wrote this script for my wife. I put it in a directory on the webserver and then all she has to do is put pictures there and it makes thumbnails and shrinks them down to manageable sizes. Feel free to critique.

Code: Select all

<?PHP
//standard height for thumbnails
define('HEIGHT',80);

//this is the max height and width for the big pictures. We will size them down so 
//that their smaller side is less then the appropriate number
define('MAX_HEIGHT',450);
define('MAX_WIDTH',800);

//initialie variables
$i = 0;
$javascript = "";
$thumbNailDisp = "";

//check if there is a thumbs directory, and if not, create one
if(!is_dir('thumbs'))
&#123;
  mkdir('thumbs');
&#125;


//open the directory with the thumbs
if($readMe = opendir('thumbs'))
&#123;
	while(false !== ($file = readdir($readMe)))
	&#123;//loop through files
		if(strpos($file, '.jpg') or strpos($file, '.JPG'))
    &#123;
      //build an arrat of thumbnails so we know whats already been done
      $currThumbs&#1111;$file] = true;
    &#125;
	&#125;//end loop through files
&#125;//end if directory opened
closedir($readMe);

//open the directory with the files
if($readMe = opendir(getcwd()))
&#123;
	while(false !== ($file = readdir($readMe)))
	&#123; //loop through files
		if(strpos($file, '.jpg') or strpos($file, '.JPG'))
    &#123;
      //build an array of pictures
      $pics&#1111;$file] = true;

      //find out the size of the image we're dealing with here
      list($width, $height) = getimagesize($file);

      //this is for preloading the images and needs some work
      $i++;
      $javascript .= "Image$i= new Image($width,$height);\n
                      Image$i.src = '$file';\n\n";

      //if there is no thumbnail for this image, create it! 
      if(!isset($currThumbs&#1111;$file]))
      &#123;//create thumbnail  
        $im=imagecreatefromjpeg($file);   
        $newHeight = HEIGHT;
        $newWidth = ($width*HEIGHT)/$height;
        $small = imagecreatetruecolor($newWidth , $newHeight);    // new image
        ImageCopyResampled($small, $im, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);
        ImageDestroy($im);  
        ImageJPEG($small,'thumbs/'.$file,100);
        ImageDestroy($small); 
      &#125;

      //array of valid thumbnails (so we can delete the others!)
      $validThumbs&#1111;$file] = true; 
   
      //make sure the picture isn't too big
      if($height<$width AND $height > MAX_HEIGHT)
      &#123;//if picture is landscape and taller then max height, shrink it
        $im=imagecreatefromjpeg($file);
        $shrunkHeight = MAX_HEIGHT;//set size for new image
        $shrunkWidth = round(($width*MAX_HEIGHT)/$height);
        $small = imagecreatetruecolor($shrunkWidth , $shrunkHeight);    // new image
        ImageCopyResampled($small, $im, 0, 0, 0, 0, $shrunkWidth, $shrunkHeight, $width, $height);
        ImageDestroy($im); 
        ImageJPEG($small,$file,100);
        ImageDestroy($small);
        $width=$shrunkWidth;
        $height=$shrunkHeight;
      &#125;
      else if($height>$width AND $width > MAX_WIDTH)
      &#123;//if its portrait style and too wide, shrink it
        $im=imagecreatefromjpeg($file);
         // path to your galleryunlink($file);
        $shrunkWidth = MAX_WIDTH;//set size for new image
        $shrunkHeight = ($height*MAX_WIDTH)/$width;
        $small = imagecreatetruecolor($shrunkWidth , $shrunkHeight);    // new image
        ImageCopyResampled($small, $im, 0, 0, 0, 0, $shrunkWidth, $shrunkHeight, $width, $height);
        ImageDestroy($im); 
        ImageJPEG($small,$file,100);
        ImageDestroy($small); 
        $width=$shrunkWidth;
        $height=$shrunkHeight;
      &#125;

      $thumbNailDisp .= "<a href="javascript:changePicture('$file', '".$width."', '".$height."');"'><img src='thumbs/".$file."'></a>";
  	&#125;
	&#125;//end loop through files
&#125;//end if directory opened
closedir($readMe);

if(isset($currThumbs) and is_array($currThumbs))
&#123;
  //get thumbnails whose pictures no longer exist
  $invalidThumbs = array_diff(array_keys($currThumbs),array_keys($validThumbs));

  //loop through invalid thumbnails and delete them
  foreach($invalidThumbs as $fileName)
  &#123;
    unlink("thumbs/$fileName");
  &#125;
&#125;

$picNum=0;

?>
<html>
  <head>
  <style type="text/css">
		<!--
		body &#123;
			margin-left: 0px;
			margin-top: 0px;
			margin-right: 0px;
			margin-bottom: 0px;
		&#125;
		-->
	</style>
  <SCRIPT language="javascript">
  <!--
    
    var maxHeight=<?=MAX_HEIGHT?>;
    var maxWidth=<?=MAX_WIDTH?>;
	
  	function changePicture(source, newWidth, newHeight)
    &#123;
     var img=document.getElementById("picItself");  
     img.src=source;
     img.width=newWidth;
     img.height=newHeight;


     var e=document.getElementById("picHolder");
     if(newWidth>900)
        e.style.width = maxWidth+'px';
     else
        e.style.width = (newWidth*1+20)+'px';
     if(newHeight>500)
        e.style.height = maxHeight+'px';
     else
        e.style.height = (newHeight*1+20)+'px';
    &#125;


  -->
  </SCRIPT>
  </head>
  <body bgcolor = '#000033' >
    <center>
    <table width = '900' margin = 0 border = 0>
      <tr>
        <td nowrap style='overflow : auto;' bgcolor='#000033'>
          <div style='width : 900px; height : 110px; overflow : auto;'>
            <?=$thumbNailDisp ?>
          </div>
        </td>
      </tr>
      <tr>
        <td valign='top' align='center' bgcolor='#000033'>
          <div id = 'picHolder' style='width : 900px; height : 110px; overflow : auto;'>
          <img src='instruct.gif' id='picItself'>
          </div>
        </td>
      </tr>
    </table>
    </center>
  <SCRIPT language="javascript">
  <!--


    <?=$javascript?>

  -->
  </SCRIPT>
  </body>
</html>
Dale
Forum Contributor
Posts: 466
Joined: Fri Jun 21, 2002 5:57 pm
Location: Atherstone, Warks

Post by Dale »

I got loads of errors:

Code: Select all

Warning: mkdir(thumbs): Permission denied in /home/dframe/public_html/msn.php on line 18

Warning: opendir(thumbs): failed to open dir: No such file or directory in /home/dframe/public_html/msn.php on line 23

Warning: closedir(): supplied argument is not a valid Directory resource in /home/dframe/public_html/msn.php on line 34

Warning: imagejpeg(): Unable to open 'thumbs/resi.jpg' for writing in /home/dframe/public_html/msn.php on line 63

Warning: imagejpeg(): Unable to open 'resi.jpg' for writing in /home/dframe/public_html/msn.php on line 79

Warning: imagejpeg(): Unable to open 'thumbs/simpsons.jpg' for writing in /home/dframe/public_html/msn.php on line 63

Warning: imagejpeg(): Unable to open 'simpsons.jpg' for writing in /home/dframe/public_html/msn.php on line 79
Hmmm... permissions problem probably?
magicrobotmonkey
Forum Regular
Posts: 888
Joined: Sun Mar 21, 2004 1:09 pm
Location: Cambridge, MA

Post by magicrobotmonkey »

yea looks that way. I only used it on a WAMP machine so I didn't really test it with any permissions.
Dale
Forum Contributor
Posts: 466
Joined: Fri Jun 21, 2002 5:57 pm
Location: Atherstone, Warks

Post by Dale »

Ahhh ok ;)
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

a simple chmod on the upload directory should fix that, though.
User avatar
fresh
Forum Contributor
Posts: 259
Joined: Mon Jun 14, 2004 10:39 am
Location: Amerika

Post by fresh »

there is no limit to what man will do for women.. :roll:

good code, thanks :)

regards
Post Reply