我有自己的PHP函数,允许用户更新他们的配置文件图像。
这一切都很好,然而,我遇到了一个问题,用户可以上传他们想要的任何尺寸的图像;即CCD_ 1。
我不希望发生这种事。我希望他们选择上传的图像以1:1的比例裁剪并居中;即:它从CCD_ 2到CCD_。
我发现的脚本似乎都不适用于我的网站(至少是我希望的方式)。
这是我目前用来更新他们头像的代码。它包括检查它们是否具有正确的文件扩展名&如果图像大小小于256kb:
$ext = array('jpg', 'jpeg', 'png');
$file = $_FILES['avatar']['name'];
$fileext = strtolower(end(explode('.', $file)));
$filetmp = $_FILES['avatar']['tmp_name'];
if(!in_array($fileext, $ext)){
$errors[] = 'Please select a valid file type. (JPG, JPEG or PNG)'; }
if($_FILES['avatar']['size'] > 256000){
$errors[] = 'Avatars cannot exceed a 256kb file size.';
}
if(empty($errors)){
updateAvatar($conn, $username, $filetmp, $fileext);
} else if(!empty($errors)){
echo output_errors($errors);
}
if(isset($_SESSION['updateAvat'])){
flash('You have successfully updated your avatar.');
unset($_SESSION['updateAvat']);
}
这是我制作的updateAvatar()
函数,在第13行调用:
function updateAvatar($conn, $username, $filetmp, $fileext){
$file = md5(microtime() . $filetmp) . '.' . $fileext;
$filepth = './data/user_data/img/udid/prof/' . $file;
move_uploaded_file($filetmp, $filepth);
if(mysqli_query($conn, "UPDATE users SET profile = '$file' WHERE username = '$username'")){
$_SESSION['updateAvat'] = md5(microtime() . $filetmp);
} else {
$errors[] = 'There was a problem updating your avatar. Please try again.';
}
}
然而,这还不够,也不允许我的用户个人资料页面以应有的方式工作或看起来,我会像谷歌或推特那样做他们的头像。
感谢所有的帮助。干杯
php:中有imagecrop()
函数
$tmpfile = $_FILES['avatar']['tmp_name'];
switch ($fileext) {
case 'jpg':
case 'jpeg':
$image = imagecreatefromjpeg($tmpfile);
break;
case 'png':
$image = imagecreatefrompng($tmpfile);
break;
default:
die("wtf is this extension??");
}
list($w, $h) = getimagesize($imgfile);
// get width $w and height $h for the image
if ($w < $h) // then keep the width and scale the height
$image = imagecrop($image, array(
"x" => 0,
"y" => ($h - $w) / 2,
"width" => $w,
"height" => $w
));
else if ($h < $w) // then keep the height and scale the width
$image = imagecrop($image, array(
"x" => ($w - $h) / 2,
"y" => 0,
"width" => $h,
"height" => $h
));
我还没有尝试过这个代码,但我确信它是正确的。试试看,如果不管用就告诉我。
更新:然后,您可以使用imagejpeg()
或imagepng()
保存资源$image
,以使用相同的扩展名保存图像,这样您就不会遇到数据库问题:
imagejpeg($image, "/path/to/your/image.jpg");
这是对流程工作原理的解释,而不是复制和粘贴代码,但需要对逻辑过程进行描述了解在PHP 中调整图像大小
您希望使用PHP imagecopyresampled
拍摄原始图像,然后用数学方法计算出边距,使其成为正方形,然后调整该正方形的大小。
流程:
-
上传的图像被检查为真实图像和真实文件类型。
-
使用
564 x 346
0(或类似的PNG等),您可以导入上传的图像文件资源。 -
使用
getimagesize
可以找到尺寸,然后可以找到图像的纵横比。 -
如果纵横比>1,则宽度大于高度,因此需要假设一个与高度大小相同的正方形(因为它是较小的值)。例如:
Width: 450
Height: 300
Ratio = 450/300 = 3/2 = 1.5 = >1
如果纵横比是<1,则高度大于宽度。
所以你想要一个300x300的正方形,所以你需要把宽度上多余的150切掉。这被称为边距,为了使图像居中,您需要从每一侧切掉一半的边距,因此:
$fullMargin = (width- height)
$margin = $fullMargin/2
- 就在调整和保存新图像的大小之前,应该生成一个新的图像资源:
$max_width = $max_height = 256;
$smallestDimensionValue = $originalHeight //(in this example);
$newImage = imagecreatetruecolor($max_width,$max_height);
$sourceHeight = $originalHeight;
$sourceWidth = $originalWidth - $fullMargin;
- 最后,图像复制和重采样以及输出应符合预期:
imagecopyresampled($nwImage, $sourceImage, 0,0,$margin,0,$max_width,$max_height,$sourceWidth,$sourceHeight);
- PHP会自动缩放
imagecopyresampled
中的图像,但要理解这一点,一旦为全尺寸图像确定了边距大小,就必须将边距乘以与宽度和高度相同的值,才能将整个图像缩放到合适的大小
举个例子,你想让头像的最大大小为256px,然后:
maximum_allowed_width / original_width = scale_factor.
因此,对于上面的例子,这是:
256 / 450 = 0.56888888
这是上面定义的边距值以及目标图像的高度和宽度乘以的值。