我有一个上传文件到服务器以及添加文件名到数据库的脚本,但我想要做的是在上传之前限制图像的最大尺寸。因此,如果我上传的图像是1000 x 500,它将受到限制,但仍将保持其尺寸,并将更改为200 x 100,但300 x 300的图像将被限制为200 x 200
<?php
//This is the directory where images will be saved
$target = "uploads/";
$target = $target . basename( $_FILES['photo']['name']);
//This gets all the other information from the form
$name=$_POST['name'];
$pic=($_FILES['photo']['name']);
// Connects to your Database
mysql_connect("hostname", "username", "password") or die(mysql_error()) ;
mysql_select_db("database") or die(mysql_error()) ;
//Writes the information to the database
mysql_query("INSERT INTO `table` (name, photo) VALUES ('$name','$pic')") ;
//Writes the photo to the server
if(move_uploaded_file($_FILES['photo']['tmp_name'], $target))
{
//Tells you if its all ok
echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded";
}
else {
//Gives and error if its not
echo "Sorry, there was a problem uploading your file.";
}
?> 谢谢你的帮忙
发布于 2011-09-26 18:08:23
据我所知,你不能在上传之前调整图片的大小。(我可能错了!)但是,当您上传图像时,它会变成一个临时文件。您可以调整临时图像的大小,并将调整后的图像复制到其最终目标。
这段代码改编自FliquidStudios:Resizing images in PHP with GD and Imagick上的一段代码。
既然(看起来)你想保持宽度不变,你实际上不需要做很多比率测试。
更新:
您应该能够简单地使用它来代替您的原始代码。它的大部分是不变的。
<?php
// resizes an image to fit a given width in pixels.
// works with BMP, PNG, JPEG, and GIF
// $file is overwritten
function fit_image_file_to_width($file, $w, $mime = 'image/jpeg') {
list($width, $height) = getimagesize($file);
$newwidth = $w;
$newheight = $w * $height / $width;
switch ($mime) {
case 'image/jpeg':
$src = imagecreatefromjpeg($file);
break;
case 'image/png';
$src = imagecreatefrompng($file);
break;
case 'image/bmp';
$src = imagecreatefromwbmp($file);
break;
case 'image/gif';
$src = imagecreatefromgif($file);
break;
}
$dst = imagecreatetruecolor($newwidth, $newheight);
imagecopyresampled($dst, $src, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
switch ($mime) {
case 'image/jpeg':
imagejpeg($dst, $file);
break;
case 'image/png';
imagealphablending($dst, false);
imagesavealpha($dst, true);
imagepng($dst, $file);
break;
case 'image/bmp';
imagewbmp($dst, $file);
break;
case 'image/gif';
imagegif($dst, $file);
break;
}
imagedestroy($dst);
}
// init file vars
$pic = $_FILES['photo']['name'];
$target = 'uploads/' . basename( $_FILES['photo']['name']);
$temp_name = $_FILES['photo']['tmp_name'];
$type = $_FILES["photo"]["type"];
// Connects to your Database
mysql_connect("hostname", "username", "password") or die(mysql_error()) ;
mysql_select_db("database") or die(mysql_error()) ;
// get form data
$name = mysql_real_escape_string(isset($_POST['name']) ? $_POST['name'] : 'No name');
//Writes the information to the database
mysql_query("INSERT INTO `table` (name, photo) VALUES ('$name','$pic')") ;
// resize the image in the tmp directorys
fit_image_file_to_width($temp_name, 200, $type);
//Writes the photo to the server
if(move_uploaded_file($temp_name, $target)) {
//Tells you if its all ok
echo "The file ". basename( $_FILES['photo']['name'] ). " has been uploaded";
} else {
//Gives and error if its not
echo "Sorry, there was a problem uploading your file.";
}
?>发布于 2011-09-26 18:07:17
我在过去使用这个函数来生成适合给定尺寸的缩略图,保持纵横比,也许你可以使用它:
function resize_img_nofill($src_name,$dst_name,$width,$height,$dontExpand=false) {
$MAGIC_QUOTES = set_magic_quotes_runtime();
set_magic_quotes_runtime(0);
$type = strtolower(substr(strrchr($src_name,"."),1));
if($type == "jpg") {
$src = imagecreatefromjpeg($src_name);
} else if($type == "png") {
$src = imagecreatefrompng($src_name);
} else if($type == "gif") {
$src = imagecreatefromgif($src_name);
} else {
if($src_name != $dst_name) copy($src_name,$dst_name);
set_magic_quotes_runtime($MAGIC_QUOTES);
return;
}
$d_width = $s_width = imagesx($src);
$d_height = $s_height = imagesy($src);
if($s_width*$height > $width*$s_height && (!$dontExpand || $width < $s_width)) {
$d_width = $width;
$d_height = (int)round($s_height*$d_width/$s_width);
} else if(!$dontExpand || $height < $s_height) {
$d_height = $height;
$d_width = (int)round($s_width*$d_height/$s_height);
}
if($s_width != $d_width || $s_height != $d_height) {
if($type == "jpg") {
$dst = imagecreatetruecolor($d_width,$d_height);
} else if($type == "png") {
$dst = imagecreate($d_width,$d_height);
} else if($type == "gif") {
$dst = imagecreate($d_width,$d_height);
}
$white = imagecolorallocate($dst,255,255,255);
imagefilledrectangle($dst,0,0,$d_width,$d_height,$white);
imagecopyresampled($dst,$src,0,0,0,0,$d_width,$d_height,$s_width,$s_height);
if($type == "jpg")
imagejpeg($dst,$dst_name, 80);
else if($type == "png")
imagepng($dst,$dst_name);
else if($type == "gif")
imagegif($dst,$dst_name);
imagedestroy($dst);
imagedestroy($src);
} else {
copy($src_name,$dst_name);
}
set_magic_quotes_runtime($MAGIC_QUOTES);
return array($d_width,$d_height);
}https://stackoverflow.com/questions/7553247
复制相似问题