计算要调整大小的最大级别的图像大小比率

时间:2013-08-26 06:36:09

标签: php

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

注意:未定义的变量:第14行的C:\ xampp \ htdocs \ try \ resize.php中的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);
}

1 个答案:

答案 0 :(得分:0)

我不确定你的代码试图实现什么,但是有很多错误。

我发现很难相信它是像你说的那样复制图像。

参见内联评论

<?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>

<强>已更新

我已经将代码重写为应该做我认为你想做的事情

<?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>