0

#

警告:imagecreatetruecolor():第 13 行 C:\xampp\htdocs\try\resize.php 中的图像尺寸无效

注意:未定义变量:C:\xampp\htdocs\try\resize.php 中第 14 行的 2048

警告:imagecopyresampled() 期望参数 1 是资源,布尔值在第 14 行的 C:\xampp\htdocs\try\resize.php 中给出

警告:imagejpeg() 期望参数 1 是资源,布尔值在第 15 行的 C:\xampp\htdocs\try\resize.php 中给出

imagecopyresampled 不起作用。


    $size = $_POST['max'];
    $target = $_FILES['image']['name'];
    $new = "resize_".$target;
    $type = $_FILES['image']['type'];
    $image_name = $_FILES['image']['tmp_name'];
    list($originalWidth, $originalHeight) = getimagesize($target);
    $ratio = $originalWidth / $originalHeight;
    $targetWidth = $targetHeight = min($size, max($originalWidth, $originalHeight));


    if ($ratio < 1) {
    $targetWidth = $targetHeight * $ratio;
    } else {
        $targetHeight = $targetWidth / $ratio;
    }

    $srcWidth = $originalWidth;
    $srcHeight = $originalHeight;
    $srcX = $srcY = 0;


    $targetWidth = $targetHeight = min($originalWidth, $originalHeight, $size);

    if ($ratio < 1) {
        $srcX = 0;
        $srcY = ($originalHeight / 2) - ($originalWidth / 2);
        $srcWidth = $srcHeight = $originalWidth;
    } else {
        $srcY = 0;
        $srcX = ($originalWidth / 2) - ($originalHeight / 2);
        $srcWidth = $srcHeight = $originalHeight;
    }
    resize($target,$new,$srcX,$srcY,$targetWidth,$targetHeight,$type,$srcWidth,$srcHeight);
}
<form action='' method="post" enctype="multipart/form-data">
    <input type="text" name ="max">
    <input type="file" name ="image">
    <input type="submit" name="Submit" value="upload">

</form>

这是我的功能


function resize($target,$new,$srcX,$srcY,$targetWidth,$targetHeight,$type,$srcWidth,$srcHeight){
    list($originalWidth, $originalHeight) = getimagesize($target);
    if($type=="image/jpeg"){
        $nen = imagecreatefromjpeg($target);
    }
    else if($type=="image/gif"){
        $nen = imagecreatefromgif($target);
    }
    else if($type=="image/png"){
        $nen = imagecreatefrompng($target);
    }
    $chen = imagecreatetruecolor($srcX, $srcY);
    imagecopyresampled($chen, $nen, 0, 0, $srcX, $srcY, $originalWidth, $originalHeight,$srcWidth, $srcHeight);
    imagejpeg($chen,$new,80);
}
4

1 回答 1

0

I am not sure what your code is trying to achieve but there are lots of errors.

I am finding it hard to believe it was copying images as you say it was.

See inline comments

<?php

    if ($_POST) {

        // lets set a default value here as MAX field is not set in the form shown below
        $size = empty($_POST['max']) ? 500 : $_POST['max'];

        $target = $_FILES['image']['name'];

        $new = "resize_".$target;
        $type = $_FILES['image']['type'];

        $image_name = $_FILES['image']['tmp_name'];

        // should use image_name as that is the absolute filename, not $target
        // list($originalWidth, $originalHeight) = getimagesize($target);
        list($originalWidth, $originalHeight) = getimagesize($image_name);

        $ratio = $originalWidth / $originalHeight;

        $targetWidth = $targetHeight = min($size, max($originalWidth, $originalHeight));

        if ($ratio < 1) {
            $targetWidth = $targetHeight * $ratio;
        } else {
            $targetHeight = $targetWidth / $ratio;
        }

        // why are we setting $srcWidth,$srcHeight,$srcX,$srcY here, if we are overwriting them in the next bit of code
        $srcWidth = $originalWidth;
        $srcHeight = $originalHeight;
        $srcX = $srcY = 0;


        $targetWidth = $targetHeight = min($originalWidth, $originalHeight, $size);

        // this if/else could be merged with one above as it is based on the same conditions
        if ($ratio < 1) {
            $srcX = 0;
            $srcY = ($originalHeight / 2) - ($originalWidth / 2);
            $srcWidth = $srcHeight = $originalWidth;
        } else {
            $srcY = 0;
            $srcX = ($originalWidth / 2) - ($originalHeight / 2);
            $srcWidth = $srcHeight = $originalHeight;
        }

        // $srcX OR $srcY is now set to 0, this causes error in resize function
        // should use $image_name as that is the absolute filename, not $target
      // resize($target,$new,$srcX,$srcY,$targetWidth,$targetHeight,$type,$srcWidth,$srcHeight);
        resize($image_name,$new,$srcX,$srcY,$targetWidth,$targetHeight,$type,$srcWidth,$srcHeight);

    }

    function resize($target,$new,$srcX,$srcY,$targetWidth,$targetHeight,$type,$srcWidth,$srcHeight){

        // Why are we getting the width/height again this could be passed from calling function
        list($originalWidth, $originalHeight) = getimagesize($target);

//        if($type=="image/jpeg"){
//            $nen = imagecreatefromjpeg($target);
//        }
//        else if($type=="image/gif"){
//            $nen = imagecreatefromgif($target);
//        }
//        else if($type=="image/png"){
//            $nen = imagecreatefrompng($target);
//        }

        // let php decide the image type instead
        $nen = imagecreatefromstring(file_get_contents($target));

        // srcX or srcY is set to 0 see calling function. we cannot create a image that is 0 in width or height
        // i am guessing this should be $targetWidth,$targetHeight instead
        // $chen = imagecreatetruecolor($srcX, $srcY);
        $chen = imagecreatetruecolor($targetWidth,$targetHeight);

        imagecopyresampled($chen, $nen, 0, 0, $srcX, $srcY, $originalWidth, $originalHeight,$srcWidth, $srcHeight);


        imagejpeg($chen,$new,80);
    }

?>


<form action='' method="post" enctype="multipart/form-data">
    <input type="text" name ="max">
    <input type="file" name ="image">
    <input type="submit" name="Submit" value="upload">

</form>

UPDATED

I have rewritten the code into something that should do what I think you want to do

<?php

    if ($_POST) {

        $original_filename = $_FILES['image']['tmp_name'];

        $target_dir = "/temp/"; // change to your target dir with trailing slash

        // original filename without the extension, as we are going to convert to jpg anyway
        $path_info = pathinfo($_FILES['image']['name']);
        $target_filename = $path_info['filename'];

        // specify multiple sizes
        $target_sizes = array(2000, 500, 300, 200, 100);

        resize($original_filename, $target_dir, $target_filename, $target_sizes);

        // or just one size
        resize($original_filename, $target_dir, $target_filename, 375);
    }


    function resize($original_filename, $target_dir, $target_filename, $target_sizes){

        // convert target_sizes to array
        if (! is_array($target_sizes)) {
            $target_sizes = array($target_sizes);
        }

        // get original image dimensions
        list($original_w, $original_h) = getimagesize($original_filename);


        // load image, let php decide the image type
        $original_img = imagecreatefromstring(file_get_contents($original_filename));

        foreach ($target_sizes as $target_size) {

            // determine max size
            $max_size = min($original_w, $original_h, $target_size);

            // calculate target size based on ratio
            if ($original_w < $original_h) {
                $target_h = $max_size;
                $ratio = $original_w / $original_h;
                $target_w = $target_h * $ratio;
            } else {
                $target_w = $max_size;
                $ratio = $original_h / $original_w;
                $target_h = $target_w * $ratio;
            }

            // create new target image to target dimensions
            $target_img = imagecreatetruecolor($target_w, $target_h);

            // resize original
            imagecopyresampled($target_img, $original_img, 0, 0, 0, 0, $target_w, $target_h, $original_w, $original_h);

            // create new filename, with image size suffix
            $new_filename = "{$target_dir}/{$target_filename}_{$max_size}.jpg";

            // save new image
            imagejpeg($target_img, $new_filename, 80);

            // destroy image after we are finished using it
            imagedestroy($target_img);
        }
    }

?>


<form action='' method="post" enctype="multipart/form-data">
    <input type="text" name ="max">
    <input type="file" name ="image">
    <input type="submit" name="Submit" value="upload">

</form>
于 2013-08-26T07:44:06.110 回答