PHP调整图像大小以适合特定区域 [英] PHP resize image to fit within a certain area

查看:92
本文介绍了PHP调整图像大小以适合特定区域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个图像文件,存储在变量$image中,我想调整该图像的大小,以使其适合 380px x 380px 的区域(因此,图像的另一边必须为380px,小于380px). 有没有人建议如何做?

I have an image file that is stored within the variable $image I want to resize this image so it would fit within an area of 380px by 380px (so which means that the tallest side of the image must be 380px on the other side smaller than 380px). Has anyone a suggestion on how to do this?

谢谢

推荐答案

这是我用来将其保持在800x600以下的方法

here is what I use to keep it under 800x600

$orig_image = imagecreatefromjpeg($file['tmp_name']);
list($width,$height) = getimagesize($file['tmp_name']);
if(max($width,$height) > 800){
  $scale = 800/max($width,$height);
  $new_width = floor($width*$scale);
  $new_height = floor($height*$scale);
  $save_image = imagecreatetruecolor($new_width,$new_height);
  imagecopyresampled($save_image,$orig_image,0,0,0,0,$new_width,$new_height,$width,$height);
  imagejpeg($save_image,self::$FILE_DIRECTORY."$year_month/$fileId.jpg");
  $orig_image = $save_image;
  $width = $new_width;
  $height = $new_height;
}

希望您可以从中推断出一个解决方案..也不是我的$ file变量来自$ _FILE数组中的上载文件.

hopefully you can extrapolate a solution off that.. also not that my $file variable is coming from an uploaded file in the $_FILE array.

这篇关于PHP调整图像大小以适合特定区域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆