Jump to content

Creating thumbs script - slight change needed


ukscotth

Recommended Posts

Hi,

 

I have this script for creating thumbnails which works fine but can someone please show me how to modify it so instead of it changeing the width every time it changes either the width or height, whichever one is bigger. So if i set it to say 150, neither the height or width will ever be bigger than 150.

 

Also so it doesnt increase the size if both the width and height are already lower than 150.

 

Hope that makes sense and i hope someone can help :)

 


<?php
function makeThumbs( $pathToImages, $pathToThumbs, $thumbWidth ) {

  $dir = opendir($pathToImages);

  while (false !== ($fname = readdir($dir))) {

    $info = pathinfo($pathToImages . $fname);

    if (strtolower($info['extension']) == 'jpg') {
       
    	echo "Creating thumbnail for $fname <br />";

    $img = imagecreatefromjpeg($pathToImages . $fname);
    
    $width = imagesx($img);
    
    $height = imagesy($img);
      
    $new_width = $thumbWidth;
    
    $new_height = floor($height * ($thumbWidth / $width));
    
    $tmp_img = imagecreatetruecolor($new_width, $new_height);

      	imagecopyresized( $tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height );

      	imagejpeg( $tmp_img, "{$pathToThumbs}{$fname}");
    }
    
  }

  closedir($dir);
  
}

makeThumbs("pictures/","thumbs/",150);
?>


Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.