PHP resize image to fit within a certain area

2019-08-07 02:09发布

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?

Thanks

标签: php image resize
1条回答
太酷不给撩
2楼-- · 2019-08-07 02:39

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;
}

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

查看更多
登录 后发表回答