如何在php中保存上载的图像
我有以下代码,需要在脚本中保留上载图像的纵横比:如何在php中保存上载的图像,php,mysql,Php,Mysql,我有以下代码,需要在脚本中保留上载图像的纵横比: <?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']; //
<?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 " <a href=poza.php?lucrare_id=$id target='new'><img src='$tsrc'></a>";
//sleep(5);
}
}
?>
发生了什么事
$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;
}
?>
首先,您需要检查源图片是否处于纵向或横向模式(例如,ifimagesx(…)>imagesy(…)
)。如果处于纵向模式:设置$n_width=300/imagesy(…)*imagesx(…)代码>和$n_高度=300代码>。如果它处于横向模式,反之亦然。通过这种方式,长边的大小调整为300px,相应地短边的大小以保持纵横比。我在php方面并不先进,所以这就是我在这里提出的问题。我首先需要了解代码,图片保存率的机制。。。。
<?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;
}
?>