gpt4 book ai didi

php - 如何在php中保留上传图像的比例

转载 作者:行者123 更新时间:2023-11-29 18:53:08 26 4
gpt4 key购买 nike

我有以下代码,需要在脚本中保留上传图像的宽高比:

<?php
ob_start();
session_start();
?>
<?Php
require "includes/config.php";
set_time_limit (0);
$max_file_size=8000; // This is in KB

@$gal_id=$_POST['cat_id'];
@$todo=$_POST['todo'];
$userid=$_SESSION['art_id'];

/// for thumbnail image size //
$n_width=300;
$n_height=300;
$required_image_width=890; // Width of resized image after uploading

if($todo=='upload'){
if(!($gal_id > 0)){
echo "Selectează o categorie ";
exit;
}

while(list($key,$value) = each($_FILES['userfile']['name']))
{
$dt=date("Y-m-d");

$sql=$dbo->prepare("insert into lucrari (cat_id,poza,art_id) values('$gal_id','$value','$userid')");
if($sql->execute()){
$id=$dbo->lastInsertId();
$file_name=$id."_".$value;
}
else{//echo mysql_error();
echo "O problemă pe server. Contactaţi administratorul! ";
exit;}

$add = $path_upload.$file_name; // upload directory path is set

copy($_FILES['userfile']['tmp_name'][$key], $add); // upload the file to the server

chmod("$add",0777); // set permission to the file.

$sql=$dbo->prepare("update lucrari set poza = '$file_name' WHERE lucrare_id=$id");
$sql->execute();

//////////ThumbNail creation //////////////////

if(file_exists($add)){
$tsrc=$path_thumbnail.$file_name;
$im=ImageCreateFromJPEG($add);
$width=ImageSx($im); // Original picture width is stored
$height=ImageSy($im); // Original picture height is stored
$newimage=imagecreatetruecolor($n_width,$n_height);
imageCopyResized($newimage,$im,0,0,0,0,$n_width,$n_height,$width,$height);
ImageJpeg($newimage,$tsrc);
chmod("$tsrc",0777);
}// end of if
////////Ending of thumb nail ////////

/////////// Resize if width is more than 890 /////

if($required_image_width < $width){
$adjusted_height=round(($required_image_width/$width) * $height);
$im=ImageCreateFromJPEG($add);
$newimage=imagecreatetruecolor($required_image_width,$adjusted_height);
imageCopyResized($newimage,$im,0,0,0,0,$required_image_width,$adjusted_height,$width,$height);
ImageJpeg($newimage,$add);
chmod("$add",0777);
}

echo " &nbsp; <a href=poza.php?lucrare_id=$id target='new'><img src='$tsrc'></a>";
//sleep(5);
}
}

?>

我想要的是编辑此脚本以保留图像的比例,而不是使用:

$n_width=300;
$n_height=300;

发生了什么 $n_宽度=300; $n_高度=300;就我而言,我需要在那里写什么而不是值?谢谢

最佳答案

您需要做的是获取宽高比。例如,480x640 肖像图像的比例为 3:4、3/4 或 0.75。然后,您需要将最大尺寸(在本例中为 640 的高度)设置为所需的高度(在您的示例中为 300)。然后,您需要将 0.75 比率(以保持“纵横比”)应用于最小尺寸(宽度 480):480 * 0.75 = 360px。

记住要做什么的一个简单方法是“使大尺寸变小,使最小尺寸更小[通过将其乘以比率分数]”。

这里有一些代码来说明我的意思:

<?php
// Set the maximum width and height we want to use
$n_width = 300;
$n_height = 300;

// Get the width to height ratio
$ratio = 0;
// Determine if the image is landscape...
if ($width > $height)
{
$ratio = $height / $width;
$n_height *= $ratio;
}
// ...Or portrait
else
{
$ratio = $width / $height;
$n_width *= $ratio;
}
?>

关于php - 如何在php中保留上传图像的比例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44286794/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com