裁剪图像php变形、扭曲


crop image php deformed, warped

我没有太多时间写,所以我会告诉你我的问题,我有一个裁剪图像的php代码。好的是,当我把$thumb_width和$thumb_height放在相同的值时,它裁剪的图像真的很好,但在这种情况下,我不想要这个分辨率(200*150)的图像这是代码,我找不到错误。

<?php
function create_thumb($directory, $image, $destination) {
$image_file = $image;
$image = $directory.$image;
if (file_exists($image)) {
$source_size = getimagesize($image);
if ($source_size !== false) {
  $thumb_width = 200;
  $thumb_height = 150;
  switch($source_size['mime']) {
    case 'image/jpeg':
         $source = imagecreatefromjpeg($image);
    break;
    case 'image/png':
         $source = imagecreatefrompng($image);
    break;
    case 'image/gif':
         $source = imagecreatefromgif($image);
    break;
  }
  $source_aspect = round(($source_size[0] / $source_size[1]), 1);
  $thumb_aspect = round(($thumb_width / $thumb_height), 1);
  if ($source_aspect < $thumb_aspect) {
    $new_size = array($thumb_width, ($thumb_width / $source_size[0]) * $source_size[1]);
    $source_pos = array(0, ($new_size[1] - $thumb_height) / 2);
  } else if ($source_aspect > $thumb_aspect) {
    $new_size = array(($thumb_width / $source_size[1]) * $source_size[0], $thumb_height);
    $source_pos = array(($new_size[0] - $thumb_width) / 2, 0);
  } else {
    $new_size = array($thumb_width, $thumb_height);
    $source_pos = array(0, 0);
  }
  if ($new_size[0] < 1) $new_size[0] = 1;
  if ($new_size[1] < 1) $new_size[1] = 1;
  $thumb = imagecreatetruecolor($thumb_width, $thumb_height);
  imagecopyresampled($thumb, $source, 0, 0, $source_pos[0], $source_pos[1], $new_size[0],                                                                $new_size[1], $source_size[0], $source_size[1]);
  switch($source_size['mime']) {
    case 'image/jpeg':
         imagejpeg($thumb, $destination.$image_file);
    break;
    case 'image/png':
          imagepng($thumb, $destination.$image_file);
    break;
    case 'image/gif':
         imagegif($thumb, $destination.$image_file);
    break;
}

}
}
}
?>

在这个例子中,如果我放了一个(533px*300px)的图像,它变形了,包装为"对不起我的英语=)",请我需要帮助。如果你能找到解决办法,我会非常高兴的。

谢谢你,马蒂亚斯。

这是dilli。

在第46行中,您提供了更多的空格,因此需要删除该行中不需要的空格。

是。通常只有一个空位,即使我们给了不止一个空位。但在这里,像" "的倍数这样的空格是存在的。

语法错误:imagecopyresompled($thumb,$source,0,0,$source_pos[0],$source_pros[1],$new_size[0],nbsp;

正确语法:imagecopyrasesampled($thumb,$source,0,0,$source_pos[0],$source_pros[1],$new_size[0],$new_size[1],$source_size[0],$source_size[1]);