Image resize javascript for Photoshop

Hi all,

Currently I have a range of images some portrait in size and some landscape, what I want to do is batch resize these images so they all fit the fixed dimensions of 180px x 120px

I understand that you can add .jsx files into the scripts folder of photoshop but right now I can only find scripts that resize the image based on 1 side which isnt that helpful.

Does anyone know were I could find examples of the code I need or can anyone offer me some advice?

Thanks

Kyle

There are also tools like imageMagick that do this.

Thanks Kalon, Il def give this code a go!

You have your query in the javascript thread and so I am not sure what type of script you want because afaik javascript cannot output files to a disk.

If it’s any help to you, this php script will resize images to fit within whatever max_width and max_heoght you specify and maintain the original image’s aspect ration.

 
<?php
 /*************************************************************************
    This script resizes an image so that it fits within a user specified
    max_width and max_height.
    Input Parameters:
    -----------------    
    $image   = path to image to be resized.
    maxWidth  = maximum width.
    maxHeight  = maximum height.
    *************************************************************************/
 
    $image = 'pic1.jpg';    
    $outFile = 'pic1_resized.jpg';
    $max_width = 100;  
    $max_height = 150;  
 
    //get the original image attributes
    $size   = GetImageSize($image);
    $width   = $size[0];
    $height  = $size[1];
    $imageType  = $size[2];
 
    //scaling factors
    $xRatio = $max_width / $width;  
    $yRatio = $max_height / $height;
 
    //calculate the new width and height
    if($width <= $max_width && $height <= $max_height) //image does not need resizing
    {
     $toWidth  = $width;
        $toHeight  = $height;
    }
    else if($xRatio * $height < $max_height)
    {
     $toHeight = round($xRatio * $height);
        $toWidth  = $max_width;        
    }
    else
    {
     $toWidth = round($yRatio * $width);
        $toHeight  = $max_height;
    }
 
    //create the resized image
    //Type of image  1=GIF  2=JPG 3=PNG 4=SWF 5=PSD 6=BMP 7=TIFF(intel byte order) 8=TIFF(motorola byte order) 9=JPC 10=JP2 11=JPX 12=JB2 13=SWC 14=IFF 15=WBMP 16=XBM
 
    $newImage = ImageCreateTrueColor($toWidth,$toHeight); //create the new image canvas
 
    switch ($imageType)
    {    
        case 1:  //gif file
         $oldImage = ImageCreateFromGif($image);
            break;
 
        case 3:  //png file
         $oldImage = ImageCreateFromPng($image);
            break; 
 
        default: //jpg file
         $oldImage = ImageCreateFromJpeg($image);
            break;      
    }
 
    ImageCopyResampled($newImage,$oldImage,0,0,0,0,$toWidth,$toHeight,$width,$height); //resize the new image
 
    //output the new resized image
    switch ($imageType)
    {    
        case 1:  //gif file
            header('Content-type: image/gif');
            ImageGif($newImage,$outFile);
            break; 
 
        case 3:  //png file
            header('Content-type: image/png');
            ImagePng($newImage,$outFile);
            break; 
 
        default: //jpg file
            header('Content-type: image/jpeg');
            ImageJpeg($newImage,$outFile);  
            break;      
    }
 
  //clean up resources
    ImageDestroy($oldImage);
    ImageDestroy($newImage);
 
    echo' Done...!!!';
?>

if you need anymore help, just post back :slight_smile:

To batch process multiple images I would put the code in a class file. I haven’t included any error checking code in this demo.

The class you would normally be put in a separate file and then include() it in your script.

The class needs 4 inputs for the consructor:

1 - folder containing the original images
2 - folder to output the resized images
3 - max width
4 - max height

The folder names must end with a /

 
<?php
class Image_Batch_Processor {
 
    private $srcDir;
    private $outDir;
    private $maxWidth;
    private $maxHeight;
 
    /* -----------------------------------------------------------
     * Class Constructor
      ----------------------------------------------------------- */
    public function __construct($srcDir, $outDir, $maxWidth, $maxHeight) {
           $this->srcDir      = $srcDir;
            $this->outDir      = $outDir;
             $this->maxWidth    = $maxWidth;   
            $this->maxHeight   = $maxHeight;
    }
 
    /* -----------------------------------------------------------
     * Accessors
      ----------------------------------------------------------- */
    public function setSrcDir($srcDir) {
        $this->srcDir      = $srcDir;
    }
    public function setOutDir($outDir) {
        $this->outDir      = $outDir;
    }
    public function setMaxWidth($maxWidth) {
        $this->maxWidth      = $maxWidth;
    }
    public function setMaxheight($maxHeight) {
        $this->maxHeight      = $maxHeight;
    }
     public function getSrcDir($srcDir) {
        return $this->srcDir;
    }
    public function getOutDir($outDir) {
        return $this->outDir;
    }
    public function getMaxWidth($maxWidth) {
        return $this->maxWidth;
    }
    public function getMaxheight($maxHeight) {
        return $this->maxHeight;
    }
 
     /* -----------------------------------------------------------
          Class Methods
      ----------------------------------------------------------- */
    public function resize($imgsArr) {
     foreach($imgsArr as $file) {
        $image = $this->srcDir.$file;    
     $outFile = $this->outDir.$file;
 
     //get the original image attributes
     $size   = GetImageSize($image);
     $width   = $size[0];
     $height  = $size[1];
     $imageType  = $size[2];
 
     //scaling factors
     $xRatio = $this->maxWidth / $width;  
     $yRatio = $this->maxHeight / $height;
 
     //calculate the new width and height
     if($width <= $this->maxWidth && $height <= $this->maxHeight) {//image does not need resizing
       $toWidth  = $width;
          $toHeight  = $height;
     } else if($xRatio * $height < $this->maxHeight) {
        $toHeight = round($xRatio * $height);
          $toWidth  = $this->maxWidth;        
     } else {
        $toWidth = round($yRatio * $width);
          $toHeight  = $this->maxHeight;
     }
 
     //create the resized image
     //Type of image  1=GIF  2=JPG 3=PNG 4=SWF 5=PSD 6=BMP 7=TIFF(intel byte order) 8=TIFF(motorola byte order) 9=JPC 10=JP2 11=JPX 12=JB2 13=SWC 14=IFF 15=WBMP 16=XBM
     $newImage = ImageCreateTrueColor($toWidth,$toHeight); //create the new image canvas
 
      switch ($imageType) {    
         case 1:  //gif file
          $oldImage = ImageCreateFromGif($image);
            break;
 
         case 3:  //png file
          $oldImage = ImageCreateFromPng($image);
            break; 
 
         default: //jpg file
          $oldImage = ImageCreateFromJpeg($image);
            break;      
     }
 
     ImageCopyResampled($newImage,$oldImage,0,0,0,0,$toWidth,$toHeight,$width,$height); //resize the new image
 
      //output the new resized image
     switch ($imageType) {    
         case 1:  //gif file
            header('Content-type: image/gif');
            ImageGif($newImage,$outFile);
            break; 
 
         case 3:  //png file
            header('Content-type: image/png');
            ImagePng($newImage,$outFile);
            break; 
 
         default: //jpg file
            header('Content-type: image/jpeg');
            ImageJpeg($newImage,$outFile);  
            break;      
     }
 
    //clean up resources
     ImageDestroy($oldImage);
     ImageDestroy($newImage);
 }  
    }
 
} //end of class
 
//----------------------------------------------------------
//testing code
 
//you can hard code the files names or get them using scandir().
 
$imgA = array('pic1.jpg','pic2.jpg','pic3.jpg');
 
$batch_processor = new Image_Batch_Processor('./','./temp/',100,100);
 
$batch_processor->resize($imgA);
 
echo 'done';
?> 

Hi Kalon,

Thanks for this, can I ask how you use this for multiple images?

Kyle