用PHP保存裁剪的图像

时间:2014-02-18 10:35:57

标签: php image crop

我有这个脚本来裁剪图像。如何将其保存到服务器?

  move_uploaded_file($_FILES["file"]["tmp_name"],
  "pictures/" . $_FILES["file"]["name"]);





$imgSrc = 'pictures/' . $_FILES["file"]["name"];

//getting the image dimensions
list($width, $height) = getimagesize($imgSrc);

//saving the image into memory (for manipulation with GD Library)
$myImage = imagecreatefromjpeg($imgSrc);

// calculating the part of the image to use for thumbnail
if ($width > $height) {
  $y = 0;
  $x = ($width - $height) / 2;
  $smallestSide = $height;
} else {
  $x = 0;
  $y = ($height - $width) / 2;
  $smallestSide = $width;
}

// copying the part into thumbnail
$thumbSize = 100;
$thumb = imagecreatetruecolor($thumbSize, $thumbSize);
imagecopyresampled($thumb, $myImage, 0, 0, $x, $y, $thumbSize, $thumbSize,     $smallestSide, $smallestSide);

//final output
header('Content-type: image/jpeg');
imagejpeg($thumb);

如您所见,我能够输出图像。但是我无法保存它。在此先感谢:)

2 个答案:

答案 0 :(得分:0)

尝试提供第二个参数,第二个参数将文件名作为参数,并将其保存在指定的路径

imagejpeg($thumb, 'filename');

例如

imagejpeg($thumb, 'uploads/yourfilename.jpg');

答案 1 :(得分:0)

使用http://nl1.php.net/move_uploaded_file保存文件

move_uploaded_file($_FILES['file']['tmp_name'], "dir/".$_FILES['file']['name']);

或使用imagejpeg然后你必须坚持使用jpeg。