函数不会创建它传递的图像

时间:2012-02-09 20:33:39

标签: php jquery ajax image-resizing image-uploading

好吧所以问题似乎是在没有创建图像的函数中,或者在自身的脚本中没有将图像传递给函数

这是上传脚本(注意:这是注入AJAX)

    $path = "cache/";

    $valid_formats = array("jpg", "png", "gif", "jpeg");
    if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST")
        {
        $name = $_FILES['photoimg']['name'];
        $size = $_FILES['photoimg']['size'];

        if(strlen($name))
                {
                ist($txt, $ext) = explode(".", $name);
                if(in_array($ext,$valid_formats))
                {
                if($size<(2024*2024))
                    {
                    $new_file = "header.".$ext;
                    $tmp = $_FILES['photoimg']['tmp_name'];
                    if(move_uploaded_file($tmp, $path.$new_file))
                    {

            echo "<img src='cache/".$new_file."?".time()."'  class='preview'>";

               createHeader($new_file);                                 
                    }

                    else
                    echo "failed";
                    }

                    else
                    echo "Image file size max 1 MB";                    
                    }
                    else
                echo "Invalid file format..";   
                }

            else
                echo "Please select image..!";

            exit;
        }

现在这里是函数

function createHeader($new_file) {

$path = "uploads/";
$image = "cache/";
$width = 800;

    if(preg_match('/[.](jpg)|(jpeg)$/', $new_file)) {  

        $im = imagecreatefromjpeg($image . $new_file); 

    } else if (preg_match('/[.](gif)$/', $new_file)) {  

        $im = imagecreatefromgif($image . $new_file);   
        imagecolortransparent($im, black);      

    } else if (preg_match('/[.](png)$/', $new_file)) {  

        $im = imagecreatefrompng($image . $new_file);   
    }  

    imagealphablending($im, false);
    imagesavealpha($im, true);  

    $ox = imagesx($im);  
    $oy = imagesy($im);  

    $nx = $width;  
    $ny = floor($oy * ($width / $ox) ); 

    imagecopyresampled($nm, $im, 0,0,0,0,$nx,$ny,$ox,$oy);      
    imagealphablending($im, true);  
    if(!file_exists($path)) {  
      if(!mkdir($path)) {  
           die("There was a problem. Please try again!");  
      }  
       }  

    imagejpeg($nm, $path . $new_file, 100);

    imagedestroy($im);
    imagedestroy($nm);

}

在逻辑中,代码必须做的是,如果用户使用我的cms,他或她可以通过单击一个图标来更改页面上的标题,该图标将使jQuery调用edit.php传递所需的查询字符串。从那里,用户可以上传一个新图像,该图像将被重命名为header.jpg并覆盖原始图像

header.jpg必须上传到uploads /目录中,并且原始图像缓存的副本应该是未链接的,函数可以创建副本并将其放入uploads /目录。

发生的事情是第一个脚本正在上传文件并将其放入缓存/目录,但该功能似乎不起作用,并且对上传/目录没有任何作用。

任何想法我可能做错了或不这样做都会导致这个烦人的问题?

1 个答案:

答案 0 :(得分:0)

$ path =“cache /”;

$valid_formats = array("jpg", "png", "gif", "jpeg");
if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST")
    {
    $name = $_FILES['photoimg']['name'];
    $size = $_FILES['photoimg']['size'];

    if(strlen($name))
            {
            list($txt, $ext) = explode(".", $name);
            if(in_array($ext,$valid_formats))
            {
            if($size<(2024*2024))
                {
                $new_file = "header.".$ext;
                $tmp = $_FILES['photoimg']['tmp_name'];
                if(move_uploaded_file($tmp, $path.$new_file))
                {

           // This needs to happen before the echo of the image tag
           createHeader($new_file);                                 

        echo "<img src='cache/".$new_file."?".time()."'  class='preview'>";
                }

                else
                echo "failed";
                }

                else
                echo "Image file size max 1 MB";                    
                }
                else
            echo "Invalid file format..";   
            }

        else
            echo "Please select image..!";

        exit;
    }