图像处理 - 上传/调整大小/创建Thumb&保存

时间:2013-11-08 13:09:29

标签: php image upload resize

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

更新:现在可以工作并调整主图像的大小并创建缩略图,但仅适用于小图像尺寸< 2500px宽...但我需要它在4320px宽的图像上工作....

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

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

?>

1 个答案:

答案 0 :(得分:0)

unlink($fulluploadfilename);

创建所有缩略图后。