大家好,
在我调整大小后,如何裁剪从中心上传的200x200大小的图像? 此时,我按比例调整为200px。
看看调整大小的评论。
我的PHP脚本:
<?
if (isset($_POST['avatar-form'])) {
$image_uploaded = $_FILES['avatar'];
// get name - type - ext
if (is_uploaded_file($image_uploaded['tmp_name'])) {
$valid_ext = array('jpg', 'jpeg', 'png');
$valid_type = array('image/jpg', 'image/jpeg', 'image/png');
$image_uploadedName = basename($image_uploaded['name']);
$ext = strtolower(explode('.', $image_uploadedName)[1]);
$name = strtolower(explode('.', $image_uploadedName)[0]);
// valid ext
if (in_array($ext, $valid_ext)) {
if (in_array($image_uploaded['type'], $valid_type)) {
if (in_array('image/jpg', $valid_type) || in_array('image/jpeg', $valid_type)) {
$image = imagecreatefromjpeg($image_uploaded['tmp_name']);
}
else if (in_array('image/png', $valid_type)) {
$image = imagecreatefrompng($image_uploaded['tmp_name']);
}
// resizing
$imgSize = getimagesize($image_uploaded['tmp_name']);
$resizeSizeImg = 200;
if ($imgSize[0] > $imgSize[1]) {
$thumbnailWidth = $resizeSizeImg;
$thumbnailHeight = round($imgSize[1] / $imgSize[0] * $resizeSizeImg);
} else {
$thumbnailWidth = round($imgSize[0] / $imgSize[1] * $resizeSizeImg);
$thumbnailHeight = $resizeSizeImg;
}
// create img
$thumbnail = imagecreatetruecolor($thumbnailWidth, $thumbnailHeight);
imagecopyresampled($thumbnail, $image, 0, 0, 0, 0, $thumbnailWidth, $thumbnailHeight, $imgSize[0],$imgSize[1]);
if (in_array('image/jpg', $valid_type) || in_array('image/jpeg', $valid_type)) {
imagejpeg($thumbnail, 'public/img/upload/avatar/'.$name.'.'.$ext);
}
else if (in_array('image/png', $valid_type)) {
imagepng($thumbnail, 'public/img/upload/avatar'.$name.'.'.$ext);
}
} else {
echo '<p class="uk-text-danger">Not an image.</p>';
}
} else {
echo '<p class="uk-text-danger">Not an image.</p>';
}
}
}
?>
非常感谢您!