php - 上传时调整图像大小

标签 php mysql

我的代码有什么问题,我需要在变量 $new_width 中上传时调整图像大小,而 $new_height 是图像的尺寸。我认为 $newname 变量有问题,因为它在我添加之前就可以工作: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);
?>

最佳答案

我建议你使用像 Imagine 这样的库- 这将使这个过程变得更加简单。

关于php - 上传时调整图像大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30001230/

相关文章:

php - 如何记录试图进行sql注入(inject)的人

php - 你将如何在没有循环的情况下重写它?

php - MySQL/PHP如何在WHERE子句中包含数组

MySql - 创建 View 时出现问题

MySQL 合并来自多个 JSON 行的数组

mysql - 用最新的外键条目连接两个表

javascript - CouchDB 不区分大小写和 OR 选项,就像在 mysql 中一样

php - 这可以在不使用两个 for 循环的情况下计算吗

php - 想要在php中的bootstrap slider 中制作动态网格

php - Symfony2,EntityManager::getRepository(命名空间\To\Some\Class::class 或 '**Bundle:Entity')