PHP imagecopyresampled 的比例、调整大小和裁剪问题
好吧,我正在编写一个图像上传,我们希望强制图像为 795x440px。它们可以调整大小,但必须保持纵横比,以便也可以裁剪。
新图像的尺寸正确,但原始文件裁剪后的图像比例错误,尝试了一些不同的方法,但无法得到正确的结果。
我现在正在测试的图像,原始文件
http://image.bayimg.com/jaiflaada.jpg
裁剪后的结果
http://image.bayimg.com/jaifmaada.jpg
怎么办我做对了,所以图像总是获得最佳尺寸并裁剪其余部分?
list($width, $height) = getimagesize($save_dir);
$prop = (795 / $width);
$height = floor($height * $prop);
$new_image = imagecreatetruecolor(795, 440);
$bgColor = imagecolorallocate($new_image, 255,255,255) or die("Couldn't allocate color");
imagefill($new_image , 0,0 , $bgColor) or die("Couldnt fill with color");
imagecopyresampled($new_image,$source_image,0,0,0,0,795,440,795,$height);
imagejpeg($new_image,$new_directory,100);
Okey so i am writing a image upload where we want to force the images to be 795x440px. They can be rezised but they have to keep their aspect ratio so they can be cropped also.
The new images comes out in the right size, but the cropped image from original file has the wrong ratio, tried some diffrent ways but can't get it right.
The image i am testing right now, original file
http://image.bayimg.com/jaiflaada.jpg
The result from cropping
http://image.bayimg.com/jaifmaada.jpg
How do i get this right, so the image always gets the best size and crops the rest?
list($width, $height) = getimagesize($save_dir);
$prop = (795 / $width);
$height = floor($height * $prop);
$new_image = imagecreatetruecolor(795, 440);
$bgColor = imagecolorallocate($new_image, 255,255,255) or die("Couldn't allocate color");
imagefill($new_image , 0,0 , $bgColor) or die("Couldnt fill with color");
imagecopyresampled($new_image,$source_image,0,0,0,0,795,440,795,$height);
imagejpeg($new_image,$new_directory,100);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我已经做得很简单了。使用相同的技术,但是 - 图像始终保持其纵横比。并在目标图像的中心调整大小。
I have done it quite simple. Using the same technique, but - image ALWAYS keeps its aspect ratio. And is resized in the center of target image.
我这样做:
I do it like that:
你可以试试这个功能。
You can try this function.