问题描述
我正在使用PHP处理图像上传脚本,我发现有人提供了该脚本并尝试对其进行修改,但是,我遇到了一些问题.
I'm working on an image upload script with PHP, I found one someone was offering and tried modifying it, however, i'm running into a few problems.
我要执行以下操作:检测图像的最长边(即纵向或横向)然后调整图像大小,最长边为800px并保持比例.
I want to do the following:Detect the longest side of the image (ie. portrait or landscape)And then resize the image, with the longest side being 800px AND keep proportions.
这是我到目前为止所拥有的代码.对于横向图像,它可以正常工作,但是对于纵向图像,它像疯了似的扭曲了它们.PS.我正在制作更大的图像以及缩略图.
Here is the code I have so far.. For landscape images it works fine, but with portrait ones it distorts them like crazy.PS. I'm making a larger image as well as a thumbnail.
list($width,$height)=getimagesize($uploadedfile);
if($width > $height){
$newwidth=800;
$newheight=($height/$width)*$newwidth;
$newwidth1=150;
$newheight1=($height/$width)*$newwidth1;
} else {
$newheight=800;
$newwidth=($height/$width)*$newheight;
$newheight1=150;
$newwidth1=($height/$width)*$newheight;
}
$tmp=imagecreatetruecolor($newwidth,$newheight);
$tmp1=imagecreatetruecolor($newwidth1,$newheight1);
推荐答案
您可能会误会:
当$width > $height
表示风景.将最大宽度设置为800表示(高度/宽度)* 800 =新高度.另一方面,$height > $width
表示将maxheight设置为800,因此(width/height)* 800是新宽度.
When $width > $height
that means it's landscape. Setting maxwidth to 800 means (height/width)*800 = new height. On the other hand $height > $width
means setting maxheight to 800 and thus having (width/height)*800 is new width.
现在,您既可以使用高度/宽度比例,也可以使用其他比例.示例:
Right now your using both the height/width ratio instead of the other way around. Example:
Image: 1600 (w) x 1200 (h)
Type: Landscape
New Width: 800
New Height: (1200 (h) / 1600(w) * 800 (nw) = 600
Image 1200 (w) x 1600 (h)
Type: Portrait
New Height: 800
New Width: (1200 (w) / 1600(h) * 800 (nh) = 600
希望您能理解我的意思,您只是将它们切换了:)另外请注意,您为肖像缩略图乘以$ newheight而不是$ newheight1
Hope you get what I'm saying, you just switched them :) Also notice that you multiply with $newheight instead of $newheight1 for the portrait thumbnail
这篇关于使用PHP调整图像大小,检测最长边并根据调整大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!