php图像调整大小-从中心平方/裁剪缩略图?
我有一个图像上传脚本,可将上传图像的大小调整为 150x150 像素。如果图像是方形的,那就太好了,但如果有人上传 640x200 像素的图像,它看起来就不漂亮了。
所以我基本上需要它根据图像的中心自动创建一个方形缩略图。如果图像较宽,则应裁剪掉左侧和右侧。如果图像较高,则应裁剪掉顶部和底部。
我在网上找到了代码修改,准确的说是:
我对 PHP 不太熟悉,我已经在这方面花了几个小时了,试图将下面的代码与上面的选项结合起来。如果有人能帮助我那就太好了:)
$target_path = "avatars/";
$image = $_FILES['uploadedfile']['name'];
$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);
$_POST["userpic"]=$_FILES['uploadedfile']['name'];
if($_FILES['uploadedfile']['tmp_name']!="") {
$imagetype=explode(".",$_POST["userpic"]);
if($imagetype[1]=="jpg" || $imagetype[1]=="JPG" || $imagetype[1]=="gif" || $imagetype[1]=="GIF")
{
$target_path = "avatars/";
$thaid=$_POST["user_id"];
$target_path = $target_path .$thaid.".".$imagetype[1];
$target_path2 =$thaid.".".$imagetype[1];
move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path);
$_POST["userpic"]=$target_path2;
$n_width=$setts['avatar_width'];
$n_height=$setts['avatar_height'];
$tsrc=$target_path;
$add=$target_path;
if($imagetype[1]=="jpg" || $imagetype[1]=="JPG")
{
$im=imagecreatefromjpeg($add);
$width=imagesx($im);
$height=imagesy($im);
$newimage=imagecreatetruecolor($n_width,$n_height);
$ar = 1.00;
if ($ar < 1) { // "tall" crop
$cropWidth = min($height * $ar, $width);
$cropHeight = $cropWidth / $ar;
}
else { // "wide" crop
$cropHeight = min($width / $ar, $height);
$cropWidth = $cropHeight * $ar;
}
imagecopyresized($newimage,$im,0,0,0,0,$n_width,$n_height,$cropWidth,$cropHeight);
imagejpeg($newimage,$tsrc,100);
}
if($imagetype[1]=="gif" || $imagetype[1]=="GIF")
{
$im=imagecreatefromgif($add);
$width=imagesx($im);
$height=imagesy($im);
$newimage=imagecreatetruecolor($n_width,$n_height);
imagecopyresized($newimage,$im,0,0,0,0,$n_width,$n_height,$width,$height);
imagegif($newimage,$tsrc,100);
}
}
else
{
$_POST["userpic"]="noimage.jpg";
}
}
I have an image upload script that resizes the uploaded image to 150x150 pixels. That's great if the image is square, but if someone uploads an image with let's say 640x200 pixels, it doesn't look pretty.
So I basically need it to automatically create a squared thumbnail based on the center of the image. If the image is wider it should crop off the left and right sides. If the image is higher, it should crop off the top and bottom.
I found a code modification online, here to be exact:
Upload, resize, and crop center of image with PHP
I'm not great with PHP and I've been at this for a few hours now, trying to combine my code below with the option above. If anyone could help me that would be great :)
$target_path = "avatars/";
$image = $_FILES['uploadedfile']['name'];
$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);
$_POST["userpic"]=$_FILES['uploadedfile']['name'];
if($_FILES['uploadedfile']['tmp_name']!="") {
$imagetype=explode(".",$_POST["userpic"]);
if($imagetype[1]=="jpg" || $imagetype[1]=="JPG" || $imagetype[1]=="gif" || $imagetype[1]=="GIF")
{
$target_path = "avatars/";
$thaid=$_POST["user_id"];
$target_path = $target_path .$thaid.".".$imagetype[1];
$target_path2 =$thaid.".".$imagetype[1];
move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path);
$_POST["userpic"]=$target_path2;
$n_width=$setts['avatar_width'];
$n_height=$setts['avatar_height'];
$tsrc=$target_path;
$add=$target_path;
if($imagetype[1]=="jpg" || $imagetype[1]=="JPG")
{
$im=imagecreatefromjpeg($add);
$width=imagesx($im);
$height=imagesy($im);
$newimage=imagecreatetruecolor($n_width,$n_height);
$ar = 1.00;
if ($ar < 1) { // "tall" crop
$cropWidth = min($height * $ar, $width);
$cropHeight = $cropWidth / $ar;
}
else { // "wide" crop
$cropHeight = min($width / $ar, $height);
$cropWidth = $cropHeight * $ar;
}
imagecopyresized($newimage,$im,0,0,0,0,$n_width,$n_height,$cropWidth,$cropHeight);
imagejpeg($newimage,$tsrc,100);
}
if($imagetype[1]=="gif" || $imagetype[1]=="GIF")
{
$im=imagecreatefromgif($add);
$width=imagesx($im);
$height=imagesy($im);
$newimage=imagecreatetruecolor($n_width,$n_height);
imagecopyresized($newimage,$im,0,0,0,0,$n_width,$n_height,$width,$height);
imagegif($newimage,$tsrc,100);
}
}
else
{
$_POST["userpic"]="noimage.jpg";
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您安装了 ImageMagick,您可以执行以下操作:
这样,您实际上不必担心数学。
If you have ImageMagick installed, you can do something like:
This way, you don't actually have to worry about the maths.
计算要裁剪区域的尺寸的数学并不复杂。我已经给出了这个问题的答案,它允许您针对裁剪区域的任何纵横比进行计算;由于您想要方形缩略图,因此应将宽高比设置为 1。
然后,了解原始图像和缩略图的尺寸,就可以轻松计算需要传递给
imagecopyresized
的值。The math to calculate the dimensions for the region to crop is not complicated. I 've given an answer to this question that allows you to calculate this for any aspect ratio of the crop region; since you want a square thumbnail, you should set the aspect ratio to 1.
Then, knowing the dimensions of your original image and the thumbnail it's easy to calculate the values you need to pass to
imagecopyresized
.