0

我的所有图像都有以下内容/images/500x500//images/250x250/我需要将图像大小调整为 250x250 并让它们使用相同的文件名进入。

它不会在新目录中创建新图像,而是替换同一目录中的大图像?

<?php

$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 = 250;

  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, $outputFile, 100);

$outputFile = 
    realpath(
        pathinfo($file, PATHINFO_DIRNAME) 
        . '/../250x250/'
    ) . pathinfo($file, PATHINFO_BASENAME);

  }
}

?>
4

1 回答 1

0
$outputFile = 
    realpath(
        pathinfo($file, PATHINFO_DIRNAME) 
        . '/../'
    ) . pathinfo($file, PATHINFO_BASENAME);

首先,您需要路径,您可以使用realpath(),以及pathinfo()。然后,只需附加 a..和文件名。

此外,您可能不需要realpath(). 我没有测试过。

于 2012-11-11T00:54:38.320 回答