PHP图像大小调整 [英] PHP Image Resizing
问题描述
我有一个将文件上传到服务器的脚本,并将文件名添加到数据库中,但是我想要在上传之前限制图像的最大尺寸。所以,如果我上传的图片是1000 x 500,它将被限制,但仍然保持它的尺寸,将被更改为200 x 100,但300 x 300的图像将被限制为200 x 200元/ b
$ b
<?php
//这是保存图片的目录
$ target =uploads /;
$ target = $ target。 basename($ _FILES ['photo'] ['name']);
//这将从表单
$ name = $ _ POST ['name'];
$ pic =($ _ FILES ['photo'] ['name']);
//连接到你的数据库
mysql_connect(hostname,username,password)或die(mysql_error());
mysql_select_db(database)或die(mysql_error());
$ b $ //将信息写入数据库
mysql_query(INSERT INTO`table`(name,photo)VALUES('$ name','$ pic'));
$ b $ //将照片写入服务器
if(move_uploaded_file($ _ FILES ['photo'] ['tmp_name'],$ target))
{
//告诉你它是否全部ok
回显The file。 basename($ _FILES ['uploadedfile'] ['name'])。 已上传;
}
else {
//给出错误,如果不是
则回显抱歉,上传文件时出现问题。
}
?>
感谢您的帮助
据我所知,您不能在上载之前调整图片大小。 (我可能是错的!)然而,当你上传图像时,它会进入一个临时文件。您可以调整临时图像大小,并将调整大小的图像复制到最终目的地。
这段代码是根据FliquidStudios的代码片段调整的:。
由于(看起来)你想保持宽度不变,你不需要做很多比率测试。
更新:
您应该可以简单地使用它来代替您的原始代码。大部分是不变的。
<?php
//调整图片大小给定的像素宽度。
//使用BMP,PNG,JPEG和GIF
// $文件被覆盖
函数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,$ 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文件变量
$ pic = $ _FILES ['photo'] ['name'];
$ target ='uploads /'。 basename($ _FILES ['photo'] ['name']);
$ temp_name = $ _FILES ['photo'] ['tmp_name'];
$ type = $ _FILES [photo] [type];
//连接到你的数据库
mysql_connect(hostname,username,password)或die(mysql_error());
mysql_select_db(database)或die(mysql_error());
//获取表单数据
$ name = mysql_real_escape_string(isset($ _ POST ['name'])?$ _POST ['name']:'No name');
$ b $ //将信息写入数据库
mysql_query(INSERT INTO`table`(name,photo)VALUES('$ name','$ pic'));
//在tmp中调整图片大小
fit_image_file_to_width($ temp_name,200,$ type);
//将照片写入服务器
if(move_uploaded_file($ temp_name,$ target)){
//告诉你它是否全部ok
回显文件。 basename($ _FILES ['photo'] ['name'])。 已上传;
$ b} else {
//给出错误,如果不是
回显对不起,上传文件时出现问题。
}
?>
I've got a script that uploads files to the server as well as adds the filename to a database, but what I'd like to do it restrict the maximum dimensions of the image before uploading. So if I upload an image that is 1000 x 500 it will be restricted but still keep it's dimensions and will be changed to 200 x 100, but an image that is 300 x 300 will be restricted to 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.";
}
?>
Thanks for your help
To my knowledge, you can’t resize the image before uploading it. (I could be wrong!) However, when you upload the image it goes into a temporary file. You can resize the temporary image and copy the resized image to its final destination.
This code was adapted from a snippet at FliquidStudios: Resizing images in PHP with GD and Imagick.
Since (it seems) you want to keep the width constant, you don’t really need to do a lot of ratio tests.
Update:
You should be able to simply use this in place of your original code. Most of it is unchanged.
<?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.";
}
?>
这篇关于PHP图像大小调整的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!