0

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

?>
  • 1
    What exactly is your question? What does your code do / in which respect does it behave differently to what you expected? – Hulk Nov 08 '13 at 13:18
  • My code uploads an image, then makes an thumbnail of that image, then stores the main image in one file and the thumbnail in a thumbs file... I need to resize the main image from 4032 wide down to 1024 wide... – Damian Shine Nov 08 '13 at 14:05
  • well, you already know how to do that - it's just the same like creating the thumbnail (almost, this time you know the target width instead of the height). Instead of just storing the original, you can store a second downsampled version (which can be named like the original, though I wouldn't recommend that). – Hulk Nov 08 '13 at 14:09
  • Trying that at the moment but can't get it to work.... – Damian Shine Nov 08 '13 at 14:16
  • My problem seems to be that if an image is 2000px or less in width, the image is uploaded, re-sized, thumbnail created and all saved to dir. But, when uploading images 4320 px in width, (which is what I'm stuck with), the script fails and does nothing... I think its something with the imagecreatefromjpeg size.... – Damian Shine Nov 08 '13 at 15:00
  • What is the error message? You could check `$_FILES['uploadimage']['error']` to see if the upload itself fails – Hulk Nov 08 '13 at 15:07
  • All I get back is '1'... I reckon there's a max file size for imagecreatefromjpeg.. – Damian Shine Nov 08 '13 at 15:39
  • Here's the error I keep running into: Fatal error: Allowed memory size of 52428800 bytes exhausted (tried to allocate 17280 bytes) in /photo_check.php on line 45 – Damian Shine Nov 08 '13 at 16:28
  • It might be due to external limitations of your hosting service (i.e. the environment limiting you to ~50MB). See e.g. [this answer to a similar question.](http://stackoverflow.com/a/3534434/2513200) – Hulk Nov 11 '13 at 09:39

1 Answers1

0

just

unlink($fulluploadfilename);

after all thumbnails are created.

Michael Livach
  • 518
  • 3
  • 13