3

I'm creating a website where I need to use a jQuery zoom script. the problem is I need a 4x larger image of the original file uploaded by the admin. The code i wrote is:

$allowedExts = array("jpeg", "jpg");
$extension = end(explode(".", $_FILES["file"]["name"]));
if (in_array($extension, $allowedExts))
{
    if ($_FILES["file"]["error"] > 0)
    {
        $out = "Error: " . $_FILES["file"]["error"] ."<br>";
    }
    else
    {
        $fname =  "img/" . $_POST["map"] . "." . $extension;
        move_uploaded_file($_FILES["file"]["tmp_name"], $fname);

        //crear imagen grande
        if(file_exists($fname)){
            list( $width, $height ) = getimagesize($fname);
            $nwidth = $width * 4;
            $nheight = $height * 4;
            $nimage = imagecreatetruecolor( $nwidth, $nheight );
            $image = imagecreatefromjpeg( $fname );
            if(imagecopyresampled( $nimage, $image, 0, 0, 0, 0, $nwidth, $nheight, $width, $height)){
                $nfname = "img/" . $_POST["map"] . "_big." . $extension;
                imagejpeg( $nimage, $nfname, 100 );
            }
            else{
                echo "Failed At re-sizing the image";
            }
            imagedestroy($image);
            imagedestroy($nimage);
        }
        else{
            echo "Can't find the file";
        }

        $out = $_FILES["file"]["name"] . " has been uploaded sucessfully <br>";
    }
}
else
{
    $out = "Invalid file (JPG-JPEG Only)<br>";
}

And the form that sends the file is:

<form action="handler.php" method="post"
                enctype="multipart/form-data">
                <label for="file">Surface:</label>
                <input type="file" name="file" id="file"><br>
                <input type="hidden" name="id" value="2">
                <input type="hidden" name="map" value="surf">
                <input type="submit" class="buttons" name="submit" value="Submit" onmouseover="butOn(this,true)" onmouseout="butOn(this,false)">
              </form> <br><br>

The thing is that when the image is uploaded it gives me the "failed at re-sizing" thing so its the imagecopyresampled function that is failing. Also I checked the width and height vars by echo function and they are ok. The GD library is also working fine.

4

2 回答 2

2

The typical problem with GD functions is that they quickly reach the upper PHP memory limit.

Do a phpinfo() and check the current limit.

I'd suggest you to increase it to 64M or even 128M. You can change this in the PHP.ini file or add a .htaccess file with:

php_value memory_limit 64M

this is also possible from the .php file:

ini_set('memory_limit', '64M');

The column on the right of phpinfo() should show you the modified value.

于 2013-04-19T02:01:36.453 回答
0

Solved: it turns out that the uploaded image was broken or something, the OS image viewer couldn't open it

于 2013-04-20T19:55:44.213 回答