我有这个调整大小和裁剪功能:
$file = imagecreatefromjpeg($imagefile["tmp_name"]);
if($imagefile["type"] == "image/jpg" || $imagefile["type"] == "image/jpeg") {
$uploaded_file = "/var/www/adimages/".$fid."-".$imagefile["name"];
} elseif($imagefile["type"] == "image/png") {
$uploaded_file = "/var/www/adimages/".$fid."-".$imagefile["name"];
}
imagejpeg($file, $uploaded_file, 100);
chmod($uploaded_file, 0644);
list($width, $height, $type, $attr) = getimagesize($uploaded_file);
$this->imageSizeW = $width;
$this->imageSizeH = $height;
$this->newSizeW = 250;
$this->newSizeH = 400;
$aspect = $width / $height;
$new_aspect = $this->newSizeW / $this->newSizeH;
if($aspect >= $new_aspect) {
$new_height = $this->newSizeH;
$new_width = $width / ($height / $this->newSizeH);
} else {
$new_width = $this->newSizeW;
$new_height = $height / ($width / $this->newSizeW);
}
$img_source = imagecreatefromjpeg($uploaded_file);
$newimage = imagecreatetruecolor($this->newSizeW, $this->newSizeH);
imagecopyresampled($newimage, $img_source, 0 - ($new_width - $this->newSizeW) / 2, 0 - ($new_height - $this->newSizeH), 0, 0, $new_width, $new_height, $width, $height);
imagejpeg($newimage, $uploaded_file, 100);
}
这几乎可以按预期工作-当它裁剪时,它会删除部分图像。我可以以某种方式扩展/配置此功能,以便它调整图像大小/居中并添加空白,因此图像将与原始图像一样但仍为 250 x 400 像素