[英]Calculating width and height to resize image
我想計算調整大小的圖像寬度和大小。 我認為可能有三種情況:
1、圖片寬度超過最大寬度限制:然后將圖片寬度調整為最大寬度,根據最大寬度限制計算高度。 例如,圖像寬度為 2248,高度為 532。寬度超過但高度較小。 因此將寬度減小到最大 1024 並計算高度,大約為 242。
2.圖片高度超過最大高度限制:同樣,將高度調整為最大高度並相應地計算寬度。
3. 高度和寬度都超過最大高度和最大寬度:進一步處理,判斷圖像是垂直的還是水平的。 如果圖像是水平的,則將寬度調整為最大寬度並據此計算高度。 否則,如果圖像是垂直的或方形的,則將高度調整為最大值並據此計算寬度。
對於這些情況,你能看看我下面的代碼,給出你的專家意見,如果它有什么好處嗎? 或者可以改進嗎? 如何?
PS。 我昨天問了類似的問題。 在我之前的問題中,我不確定邏輯應該是什么,現在我知道它應該是什么(上面提到過),並且想知道它是否有好處。 謝謝你的幫助。
<?
$max_width = 1024;
$max_height = 768;
$img = "t2.jpg";
list($original_width, $original_height) = getimagesize($img);
if (($original_width > $max_width) OR ($original_height > $max_height))
{
//original width exceeds, so reduce the original width to maximum limit.
//calculate the height according to the maximum width.
if(($original_width > $max_width) AND ($original_height <= $max_height))
{
$percent = $max_width/$original_width;
$new_width = $max_width;
$new_height = round ($original_height * $percent);
}
//image height exceeds, recudece the height to maxmimum limit.
//calculate the width according to the maximum height limit.
if(($original_width <= $max_width) AND ($original_height > $max_height))
{
$percent = $max_height/$original_height;
$new_height = $max_height;
$new_width = round ($original_width * $percent);
}
//both height and width exceeds.
//but image can be vertical or horizontal.
if(($original_width > $max_width) AND ($original_height > $max_height))
{
//if image has more width than height
//resize width to maximum width.
if ($original_width > $original_height)
{
$percent = $max_width/$original_width;
$new_width = $max_width;
$new_height = round ($original_height * $percent );
}
//image is vertical or square. More height than width.
//resize height to maximum height.
else
{
$new_height = $max_height;
$percent = $max_height/$original_height;
$new_height = $max_height;
$new_width = round ($original_width * $percent);
}
}
}
?>
// Usage example to find the proper dimensions to resize an image down to 300x400 pixels maximum:
list($width, $height) = getimagesize($image);
$new_dimensions = resize_dimensions(100,100,$width,$height);
// Calculates restricted dimensions with a maximum of $goal_width by $goal_height
function resize_dimensions($goal_width,$goal_height,$width,$height) {
$return = array('width' => $width, 'height' => $height);
// If the ratio > goal ratio and the width > goal width resize down to goal width
if ($width/$height > $goal_width/$goal_height && $width > $goal_width) {
$return['width'] = $goal_width;
$return['height'] = $goal_width/$width * $height;
}
// Otherwise, if the height > goal, resize down to goal height
else if ($height > $goal_height) {
$return['width'] = $goal_height/$height * $width;
$return['height'] = $goal_height;
}
return $return;
}
使用相同的變量名和 if 語句防止升級:
<?php
$max_width = 1024;
$max_height = 768;
$img = "t2.jpg";
list($original_width, $original_height) = getimagesize($img);
$ratio = ($max_width / $original_width < $max_height / $original_height
? $max_width / $original_width
: $max_height / $original_width
);
if ($original_width > $max_width || $original_height > $max_height) {
$new_width = $original_width * $ratio;
$new_height = $original_height * ratio;
}
?>
我發現這是擴大或縮小規模的更優雅的解決方案,如果您只想縮小規模,只需添加 if($scale_percent<1) {}
<?php
$width = $max_width = 400;
$height = $max_height = 400;
$size = getimagesize($img);
if(count($size)) {
$width = $size[0];
$height = $size[1];
$width_percent = $max_width/$width;
$height_percent = $max_height/$height;
$scale = ($width_percent>$height_percent)?'height':'width';
$scale_percent = $scale.'_percent';
$scale_percent = $$scale_percent;
$width*=$scale_percent;
$height*=$scale_percent;
}
?>
我不知道 PHP,但我會這樣做(在 C++ 偽代碼中):
float ratio = 1.0;
float ratio_w = max_width/static_cast<float>(original_width);
float ratio_h = max_height/static_cast<float>(original_height);
float ratio_max = std::max(ratio_w, ratio_h);
if (ratio_max < 1.0)
{
// width & height are larger than allowed
// scale to the larger scaling factor
ratio = ratio_max;
}
else
{
// pick a scaling factor <= 1.0
ratio = std::min(ratio, ratio_w);
ratio = std::min(ratio, ratio_h);
}
if (ratio < 1.0) // this if-clause is not necessary
{
new_width = original_width * ratio;
new_height = original_height * ratio;
}
我編輯了我的代碼以包含我之前忽略的第三種情況。 由於可讀性(個人風格)和部分性能原因(主要是迷信;-)),我通常會盡量避免嵌套 if 語句。
我不知道我的版本是否比原始版本更快或更易讀,但至少$new_width
和$new_height
分配的冗余消失了。
獲取圖像的寬度和高度試試這個
$image_info = getimagesize($_FILES["file_field_name"]["tmp_name"]);
$image_width = $image_info[0];
$image_height = $image_info[1];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.