Php自动调整图像,图像自动调整,但不作为调整大小的图像保存在文件夹中

时间:2013-03-29 05:24:07

标签: php html mysql

自动调整图像后.....我必须将图像移动到文件夹中,我将调整后的图像正确放入php变量但不能移入文件夹这里是我的代码

    $ename=$_FILES['userfile']['name'];
    $etype=$_FILES['userfile']['type'];
    $ecname=str_replace(" ","_",$ename);

    $tmp_name=isset($_FILES['userfile']['tmp_name']);
    $target_path="nurse_photo/";
    $target_path=$target_path.basename($ecname);

    $imgData=imagecrop($_FILES['userfile']['tmp_name'],$_FILES['userfile']['name'],$_FILES['userfile']['type'],85,85);
    //echo $imgData;



    if(move_uploaded_file($imgData,$target_path))
    {
                 //insert query
             }

3 个答案:

答案 0 :(得分:1)

move_uploaded_file()意味着第一个参数是文件名,即。 $ tmp_name是正确的选择。

我不知道imagecrop()是什么。这不是PHP内部,但我想这是一种GD处理。你应该清楚$ imgData类型是什么!如果是图像正文,请执行

file_put_contents($target_path, $image);

但如果是GD图像资源,请执行

imagejpeg($image, $target_path);

答案 1 :(得分:0)

根据要上传的图片类型,您可以使用imagejpeg

等功能

示例:

$imgData = imagecrop($_FILES['userfile']['tmp_name'], $_FILES['userfile']['name'], $_FILES['userfile']['type'], 85, 85);
// If $imgData is a jpg/jpeg:
imagejpeg($imgData, "/path/to/save/image", "9"); // Where the last arguement (9, in my case), is the image quality.

这会将图像保存到您在函数中设置的所需路径。

当然,还有imagepngimagegif和其他人。

答案 2 :(得分:0)

<?php
$allowedExts = array("jpg", "jpeg", "gif", "png");
$extension = end(explode(".", $_FILES["pic"]["name"]));
if ((($_FILES["pic"]["type"] == "image/gif")
|| ($_FILES["pic"]["type"] == "image/jpeg")
|| ($_FILES["pic"]["type"] == "image/png")
|| ($_FILES["pic"]["type"] == "image/pjpeg"))
&& ($_FILES["pic"]["size"] < 2097152)
&& in_array($extension, $allowedExts))
{

if ($_FILES["pic"]["error"] > 0)
{
echo "Return Code: " . $_FILES["pic"]["error"] . "<br>";
}
else
{

if ($_FILES["pic"]["size"] > 2097152) { // if file is larger than we want to allow
echo "ERROR: Your file was larger than 2MB in file size.";
}
if (file_exists("Image/" . $_FILES["pic"]["name"]))
{
echo "<script language='javascript'>alert('Picture Already Exists..!');</script>"; 

//echo $_FILES["pic"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["pic"]["tmp_name"],"Image/" . $_FILES["pic"]["name"]);
//echo "your photo has been uploaded successfully!";
$abc = $_FILES["pic"]["name"];
}
}
}

使用此代码确保解决您的问题。