0

自动调整图像大小后.....我必须将图像移动到文件夹中,我正在将图像正确调整到 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
             }
4

3 回答 3

1

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

我不知道 imagecrop() 是什么。它不是 PHP 内部的,但我想那是一种 GD 处理。你应该清楚 $imgData 类型是什么!如果是图像,做

file_put_contents($target_path, $image);

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

imagejpeg($image, $target_path);
于 2013-03-29T05:38:37.940 回答
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"];
}
}
}

使用此代码肯定可以解决您的问题。

于 2013-03-29T05:46:48.480 回答
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.

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

当然,还有imagepng, imagegif, 等。

于 2013-03-29T05:37:39.783 回答