图像处理 - 上传/调整大小/创建拇指和保存


Image Processing - Upload/Resize/Create Thumb & Save

我正在寻求帮助。我有以下代码,可以上传图像并创建缩略图并将它们存储在适当的文件夹中......但我的问题是空间。上传的主要图像是巨大的 4320 x 3240,该网站需要很长时间才能加载......我只想要 2 张图像:主图像最大宽度 1024,缩略图最大宽度 100....

更新:现在可以调整主图像的大小并创建缩略图,但仅适用于 2500 像素宽

这是我到目前为止的代码:

    <?php ob_start();
session_start(); 
include("../../config.php");
ini_set("memory_limit", "500M");
$folder = "../images/stock/".$_SESSION['info_id']."";
$thumbs =$folder.'/thumbs/';
if(!file_exists($folder)){
    mkdir ($folder,0777);
    $uploadfolder = $folder."/";
    //echo $folder.'<br>';
    }
    else
    {
        //echo 'folder already created<br>';
        $uploadfolder = $folder."/";
        //echo $folder.'<br>';
    }
if(!file_exists($thumbs)){
    mkdir($thumbs,0777);
    $thumbnailfolder = $thumbs ;
    //echo $thumbs.'<br>';
}else{
    $thumbnailfolder = $thumbs ;
    //echo $thumbnailfolder.'<br>';
}
$allowedfiletypes = array("jpeg","jpg");
//$uploadfolder = $s;
$thumbnailheight = 100; //in pixels
$imagesresizeheight = 500;
$action = $_POST['action'];
if ($action == "upload") {
    //$_SESSION['result'] = "Uploading image... " ;
    if(empty($_FILES['uploadimage']['name'])){
        $_SESSION['result'] = "<strong>Error: File not uploaded 1!</strong><br>" ;
    } else {
            $uploadfilename = $_FILES['uploadimage']['name'];
        $fileext = strtolower(substr($uploadfilename,strrpos($uploadfilename,".")+1));
        if (!in_array($fileext,$allowedfiletypes)) { $_SESSION['result'] = "<strong>Error: Invalid file extension!</strong><br>" ; }
        else {
            $fulluploadfilename = $uploadfolder.$uploadfilename ;
            if (move_uploaded_file($_FILES['uploadimage']['tmp_name'], $fulluploadfilename)) {
                $im = imagecreatefromjpeg($fulluploadfilename);
                if (!$im) { $_SESSION['result'] = "<p><strong>Error: Couldn't Resize Image!</strong><br>" ; }
                else {
                    ////  Resize Image Creation //////
                    $imw = imagesx($im); // uploaded image width
                    $imh = imagesy($im); // uploaded image height
                    $nh = $imagesresizeheight; // thumbnail height
                    $nw = round(($nh / $imh) * $imw); //thumnail width
                    $newim = imagecreatetruecolor ($nw, $nh);
                    imagecopyresampled ($newim,$im, 0, 0, 0, 0, $nw, $nh, $imw, $imh) ;
                    $imagefilename = $uploadfolder.$uploadfilename ;
                    imagejpeg($newim, $imagefilename) or die($_SESSION['result'] ="<strong>Error: Couldn't save image resize!</strong><br>");
                    echo $imw. $imh. $nw. $imagefilename;       
                    //// Thumbnail Creation //////
                    $imw = imagesx($im); // uploaded image width
                    $imh = imagesy($im); // uploaded image height
                    $nh = $thumbnailheight; // thumbnail height
                    $nw = round(($nh / $imh) * $imw); //thumnail width
                    $newim = imagecreatetruecolor ($nw, $nh);
                    imagecopyresampled ($newim,$im, 0, 0, 0, 0, $nw, $nh, $imw, $imh) ;
                    $thumbfilename = $thumbnailfolder.$uploadfilename ;
                    imagejpeg($newim, $thumbfilename) or die($_SESSION['result'] ="<strong>Error: Couldn't save thumnbail!</strong><br>");
                    //echo $thumbfilename;
                    //$_SESSION['result'] ='<img src="'.$thumbfilename.'"/><br>' ;
                }
            } else { $_SESSION['result'] = "<strong>Error: Couldn't save file($fulluploadfilename)!</strong><br>";
            }
        }
    }
}
//unlink($fulluploadfilename);          
            ////////////////////////////////////////////////
//connect to mysql server
    $c = mysql_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB);
    if (!$c){
        $_SESSION['result'] .= mysql_error().'<br>';
        return;
    }

    if (!mysql_select_db(DB)){
        $_SESSION['result'] .= mysql_error().'<br>';
        mysql_close($c);
        return;
    }
    //5. insert settings
    $sql = "INSERT INTO photo(photo_id,photo_filename) VALUES ('".$_SESSION['info_id']."','".$uploadfilename."')";

    if (!mysql_query($sql,$c)){
            mysql_close($c);
            return;
    }
    mysql_close($c);
//////////////////////////////////////////////
//$ref = getenv("HTTP_REFERER");
//header("Location: ".$ref);
?>

只是

unlink($fulluploadfilename);

创建所有缩略图后。