函数未创建图像但它传递了图像

发布于 2025-01-04 11:49:35 字数 2750 浏览 2 评论 0原文

好吧,问题似乎出在函数中没有创建图像,或者在脚本本身中,它没有将图像传递给函数

这是上传脚本(注意:这是注入到 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/ 目录中。

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

知道我可能做错了什么或没有做导致这个恼人的问题吗?

Ok so the problem seems to be in the function not creating the image, or in the script it self which does not pass the image to the function

Here is the upload script (note: this is injected into 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;
        }

now here is the function

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);

}

In logic what the code must do is if a user using my cms is loged-in he or she can change the header on the page by clicking an icon which will make a jQuery call to edit.php passing needed query string. From there own user can upload a new image which will be renamed header.jpg and over-write the original image

The header.jpg has to be uploaded into uploads/ directory and a copy of original image cache/ should be unlinked ones the function can create a copy and put it into uploads/ directory.

What is happening is that the first script is uploading the file and is putting it into cache/ directory but the function does not seem to work and does nothing for uploads/ directory.

Any idea what I may be doing wrong or not doing that is causing this annoying problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我的痛♀有谁懂 2025-01-11 11:49:35

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

$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;
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文