简体   繁体   中英

Image Processing - Upload/Resize/Create Thumb & Save

I'm looking for some help. I have the following code which uploads the image & creates a thumbnail and stores them in the appropriate folders... But my problem is space.. the uploaded main images are HUGE 4320 x 3240 and the site takes ages to load... I only want 2 images: A main image max width 1024, and a thumbnail max width 100....

UPDATE: Now works and resizes main image and creates thumbnail but only works on small image sizes < 2500px wide... but I need it to work on an image 4320px wide....

here's my code so far:

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

?>

just

unlink($fulluploadfilename);

after all thumbnails are created.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM