willdanceforfun
willdanceforfun

Reputation: 11250

Resize/scale all images in a directory and save them

I am making a little script which goes through all of the images in a directory, checks them for a width, and if they are not this width - resize.

What I have so far is this:

$files = glob("*.{png,jpg,jpeg}", GLOB_BRACE);

foreach ($files as $file) 
{
  // get the image size
  $imagesize = getimagesize($file);
  $width_orig = $imagesize[0];
  $height_orig = $imagesize[1];

  $dst_w = 900;

  if($width_orig != $dst_w)
  {

    $dst_h_multiplier = $dst_w / $width_orig;
    $dst_h = $dst_h_multiplier * $height_orig;

    $dst = imagecreatetruecolor($dst_w, $dst_h);
    $image = imagecreatefromjpeg($file);
    imagecopyresampled($dst, $image, 0, 0, 0, 0, $dst_w, $dst_h ,$width_orig, $height_orig);

    imagejpeg($dst,null,100);  
  }
}

It would seem like the script executes ok - but the image files in the same directory where this script is executed are not modified.

What am I missing here? How can I debug this?

Upvotes: 1

Views: 1643

Answers (1)

dynamic
dynamic

Reputation: 48141

You shouldn't pass null as second argument. Pass your filename

 imagejpeg($dst,$file.'edited',100);  

Upvotes: 1

Related Questions