找到你要的答案

Q:Resize image when uploaded

Q:上传时调整图像大小

What is wrong with my code I need to resize image when upload in variable $new_width and $new_height is dimension for image. I think it's problem in $newname variable because it is working before when I added :D

<meta charset="utf-8" /> 
<?php
include 'config.php';
mysqli_set_charset($db, 'utf8');
$id = $_COOKIE["id"];
if (isset($_POST["action"])){
    $newname = substr(md5(rand() * time()), 5,10);
    $folder = "image/";
    $folder2 = "image/thumb/";

    $filetmp = $_FILES["filep"]["tmp_name"];
    $filename = $_FILES["filep"]["name"];
    $filetype = $_FILES["filep"]["type"];
    $filesize = $_FILES["filep"]["size"];
    $fileinfo = getimagesize($_FILES["filep"]["tmp_name"]);
    $filewidth = $fileinfo[0];
    $fileheight = $fileinfo[1];
    $filepath = "$folder".$newname.$filename;
    $filepath_thumb = "$folder".$filename;

    if($filetmp == ""){
        echo "Upload image";
    }
    else {
        if($filesize > 2097152){            
            echo "Less then 2 mb";          
        }
        else{
            if($filetype != "image/jpeg" && $filetype != "image/png" 
                && $filetype != "image/gif" && $filetype != "image/jpg"){

                echo ".jpeg, .jpg, .gif, .png";
            }
            else{               
                move_uploaded_file($filetmp,$filepath);

                if($filetype == "image/jpeg"){
                    $imagecreate = "imagecreatefromjpeg";
                    $imageformat = "imagejpeg";
                }
                if($filetype == "image/png"){                        
                    $imagecreate = "imagecreatefrompng";
                    $imageformat = "imagepng";
                }
                if($filetype == "image/gif"){                        
                    $imagecreate= "imagecreatefromgif";
                    $imageformat = "imagegif";
                }
                if($filetype == "image/jpg"){                        
                    $imagecreate= "imagecreatefromjpg";
                    $imageformat = "imagejpg";
                }   

                $new_width = "200";
                $new_height = "150";

                $image_p = imagecreatetruecolor($new_width, $new_height);
                $image = $imagecreate($filepath); //photo folder

                imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $filewidth, $fileheight);                     
                $imageformat($image_p, $filepath_thumb);//thumb folder  

                $res = mysqli_query($db, "UPDATE Imager SET Cover='".$newname.$filename."' WHERE IDImg ='$id'");

                if($res) {
                    echo "Good.";
                }
                else {
                    echo "Not Good.";
                }
            }
        }
    }
}

header( "Refresh:2; url=img.php", true, 303);
?>

什么是错误的与我的代码,我需要调整图像,上传new_width美元美元和new_height可变尺寸的图像。我认为这是因为它是新的美元变问题工作之前,当我说:D

<meta charset="utf-8" /> 
<?php
include 'config.php';
mysqli_set_charset($db, 'utf8');
$id = $_COOKIE["id"];
if (isset($_POST["action"])){
    $newname = substr(md5(rand() * time()), 5,10);
    $folder = "image/";
    $folder2 = "image/thumb/";

    $filetmp = $_FILES["filep"]["tmp_name"];
    $filename = $_FILES["filep"]["name"];
    $filetype = $_FILES["filep"]["type"];
    $filesize = $_FILES["filep"]["size"];
    $fileinfo = getimagesize($_FILES["filep"]["tmp_name"]);
    $filewidth = $fileinfo[0];
    $fileheight = $fileinfo[1];
    $filepath = "$folder".$newname.$filename;
    $filepath_thumb = "$folder".$filename;

    if($filetmp == ""){
        echo "Upload image";
    }
    else {
        if($filesize > 2097152){            
            echo "Less then 2 mb";          
        }
        else{
            if($filetype != "image/jpeg" && $filetype != "image/png" 
                && $filetype != "image/gif" && $filetype != "image/jpg"){

                echo ".jpeg, .jpg, .gif, .png";
            }
            else{               
                move_uploaded_file($filetmp,$filepath);

                if($filetype == "image/jpeg"){
                    $imagecreate = "imagecreatefromjpeg";
                    $imageformat = "imagejpeg";
                }
                if($filetype == "image/png"){                        
                    $imagecreate = "imagecreatefrompng";
                    $imageformat = "imagepng";
                }
                if($filetype == "image/gif"){                        
                    $imagecreate= "imagecreatefromgif";
                    $imageformat = "imagegif";
                }
                if($filetype == "image/jpg"){                        
                    $imagecreate= "imagecreatefromjpg";
                    $imageformat = "imagejpg";
                }   

                $new_width = "200";
                $new_height = "150";

                $image_p = imagecreatetruecolor($new_width, $new_height);
                $image = $imagecreate($filepath); //photo folder

                imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $filewidth, $fileheight);                     
                $imageformat($image_p, $filepath_thumb);//thumb folder  

                $res = mysqli_query($db, "UPDATE Imager SET Cover='".$newname.$filename."' WHERE IDImg ='$id'");

                if($res) {
                    echo "Good.";
                }
                else {
                    echo "Not Good.";
                }
            }
        }
    }
}

header( "Refresh:2; url=img.php", true, 303);
?>
answer1: 回答1:

I would suggest you use a library like Imagine - it will make this process much simpler.

我建议你使用像想象一样的库它会使这个过程简单得多。

php  mysql