尝试使用PHP调整图片大小

3

有人知道这段代码出了什么问题吗?它并没有给我一个实际的图片,而是给了我一个图片错误。我认为我已经正确设置了所有的函数和其他内容。有任何建议都将不胜感激。

<?php
function resizeImage($filename, $max_width, $max_height) {
  list($orig_width, $orig_height) = getimagesize($filename);

  $width = $orig_width;
  $height = $orig_height;

  # taller
  if ($height > $max_height) {
    $width = ($max_height / $height) * $width;
    $height = $max_height;
  }

  # wider
  if ($width > $max_width) {
    $height = ($max_width / $width) * $height;
    $width = $max_width;
  }

  $image_p = imagecreatetruecolor($width, $height);

  $image = imagecreatefromjpeg($filename);

  imagecopyresampled($image_p, $image, 0, 0, 0, 0, 
                     $width, $height, $orig_width, $orig_height);

  return $image_p;
}

$directory = "uploads/";
$images = glob($directory."*");

foreach($images as $image) {

  // Set a maximum height and width
  $width = 200;
  $height = 200;
  $newImage = resizeImage($image, $width, $height);

  echo '<img src="'.$newImage.'" /><br />';
}      
?>

哦,我必须把调整大小后的图像保存在某个地方吗? - Jae Kim
如果您只需要显示一张图片,则无需这样做。但是在此处,您正在同一页中显示多个图像。 - Susheel Singh
@JaeKim:如果你不想保存这些图片,那么请查看我的答案。 - Subin Thomas
1个回答

1
如果您不想保存图像,那么可以使用以下代码。
<?php
    function resizeImage($filename, $max_width, $max_height) {
        list($orig_width, $orig_height) = getimagesize($filename);

        $width = $orig_width;
        $height = $orig_height;

        # taller
        if ($height > $max_height) {
            $width = ($max_height / $height) * $width;
            $height = $max_height;
        }

        # wider
        if ($width > $max_width) {
            $height = ($max_width / $width) * $height;
            $width = $max_width;
        }

        $image_p = imagecreatetruecolor($width, $height);
        $image = imagecreatefromjpeg($filename);
        imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $orig_width, $orig_height);
        ob_start();
        imagejpeg($image_p); 
        $imgSrc = 'data:image/jpeg;base64,'.base64_encode(ob_get_clean());
        return $imgSrc;
    }

    $directory = "uploads/";
    $images = glob($directory."*");

    foreach($images as $image) {
        // Set a maximum height and width
        $width = 200;
        $height = 200;
        $imgSrc = resizeImage($image, $width, $height);
        echo '<img src="'.$imgSrc.'" /><br />';
    }      
?>

如果您想保存图像,那么您需要创建另一个文件夹,例如resized,并将裁剪后的图像保存在其中,并按照下面给出的方式进行链接。(不要更改旧代码,只需添加此代码)

<?php    
    $directory = "uploads/";
    $images = glob($directory."*");

    $i=1;//added
    foreach($images as $image) {
       // Set a maximum height and width
        $width = 200;
        $height = 200;
        $newImage = resizeImage($image, $width, $height);
        imagejpeg($newImage,"resized/newimage".$i.".jpg");  //added
        echo '<img src="resized/newimage'.$i.'.jpg" /><br />';//modified
        $i++;//added
    }      
?>

$imgSrc = 'data:image/jpeg;base64,'.base64_encode(ob_get_clean()); 这样会起作用吗? - Subin Thomas
是的,它获取当前缓冲区内容并删除当前输出缓冲区。http://php.net/manual/zh/function.ob-get-clean.php - Susheel Singh

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接