0
我有這樣的調整大小和裁剪功能:調整大小/裁切圖像,並添加空格
$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 px