【问题标题】:function does not create image yet image it passed函数尚未创建图像但它传递的图像
【发布时间】:2012-03-02 08:44:51
【问题描述】:

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

这里是上传脚本(注意:这是注入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 的用户已登录,他或她可以通过单击一个图标来更改页面上的标题,该图标将对 edit.php 进行 jQuery 调用,传递所需的查询字符串。从那里自己的用户可以上传一个新图像,该图像将被重命名为 header.jpg 并覆盖原始图像

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

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

知道我做错了什么或没有做这件事会导致这个烦人的问题吗?

【问题讨论】:

  • 您的网络服务器是否有权写入您存储图像的目录?
  • @thenetimp 是的,它确实如此,这是一个非常非常古老的功能,并且已经在许多服务器上进行了测试,一直有效,它是对我在自定义论坛软件中使用的内容的轻微重写。它只是不适用于这个项目,不确定这是否适用于 AJAX
  • 发现你的问题,看看我的回答,你在回显图像标签后调用图像的创建,所以当时图像不存在。
  • @thenetimp nop 仍然不起作用,图像被上传到缓存但函数对它们没有任何作用,因此上传目录不变,里面的图像也是如此
  • ist($txt, $ext) = explode(".", $name); 应该是list($txt, $ext) = explode(".", $name);

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


【解决方案1】:

$path = "缓存/";

$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;
    }

【讨论】:

  • 甚至看不到函数输出消息
  • 做一个测试它确实有效,因为我没有收到错误但看起来图像没有通过 createHeader($new_file)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-24
相关资源
最近更新 更多