调整/缩放目录中的所有图像并保存它们

时间:2012-10-31 18:00:18

标签: php gd

我正在创建一个小脚本,它遍历目录中的所有图像,检查它们的宽度,如果它们不是这个宽度 - 调整大小。

到目前为止我所拥有的是:

$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);  
  }
}

看起来脚本执行正常 - 但是执行此脚本的同一目录中的图像文件不会被修改。

我在这里缺少什么?我该怎么调试呢?

1 个答案:

答案 0 :(得分:1)

您不应该将null作为第二个参数传递。传递你的文件名

 imagejpeg($dst,$file.'edited',100);  
相关问题