Jump to content
xisto Community
Sign in to follow this  
apple

My Code Doesnt Resize Large Images, Please Help.

Recommended Posts

Can someone please have a look at the following code, this uploads an image, and make it in 2 sizes, one size is max. 600 x 800, uploads to images folder and second 120 x 120 and uploads to thumbs folder.this script works fine, with normal size images, but if i try to upload large pics( for example, an image with dimension 2432 x 3300, it shows blank page, and uploads the original image without sizing to "image" folder, and doesnt make any small thumbnail... I hope u understand.. Please someone help me, i shall be so thankful.

<?PHPsession_start(); header("Cache-control: private");function Resize_Image($save,$file,$t_w,$t_h,$s_path,$o_path) {    $s_path = trim($s_path);    $o_path = trim($o_path);    $save = $s_path . $save;    $file = $o_path . $file;    $ext = strtolower(end(explode('.',$save)));    list($width, $height) = getimagesize($file); 	    if(($width>$t_w) OR ($height>$t_h)) {        $r1 = $t_w/$width;        $r2 = $t_h/$height;		        if($r1<$r2) {          $size = $t_w/$width;		          }else{          $size = $t_h/$height;		  		          }    }else{        $size=1;    }    $modwidth = $width * $size;     $modheight = $height * $size;    	  	    $tn = imagecreatetruecolor($modwidth, $modheight); 	    switch ($ext) {        case 'jpg':        case 'jpeg':                    $image = @imagecreatefromjpeg($file) or die ("Seems to be problem");         break;        case 'gif':                    $image = imagecreatefromgif($file);         break;        case 'png':                    $image = imagecreatefrompng($file);         break;    }    imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height) or die("he");     imagejpeg($tn, $save, 100) or die ("heheh");;     return;}function write_beg($filename, $data){  $handle = fopen ($filename, "r");  $old_content = fread ($handle, filesize ($filename));  fclose ($handle);  $final_content = $data.$old_content;  $handle2 = fopen ($filename, "w");  $finalwrite = fwrite ($handle2, $final_content);  fclose ($handle2);}  # check to see if form submitted# if yes, process form variables# if no, display formif($_POST["action"]== "Upload Image"){  # define the constant variables  $uploadDir = 'images/';    // main picture folder  $max_height = 600;           // largest height you allowed; 0 means any  $max_width = 800;            //  largest width you allowed; 0 means any  $max_file = 2000000;         // set the max file size in bytes  $image_overwrite = 1;        // 0 means overwite; 1 means new name  $allowed_type01 = array(     "image/gif",      "image/pjpeg",      "image/jpeg",      "image/png",       "image/x-png",     "image/jpg");           // add or delete allowed image types  $do_thumb = 1;               // 1 make thumbnails; 0 means do NOT make   $thumbDir = "thumbs/";          // thumbnail folder  $thumb_prefix = "";       // prefix for thumbnails  $thumb_width = 120;        // max thumb width  $thumb_height = 120;         // max thumb height  $flat_file = 1;              // 1 flat file for data; 0 database  $what_error = array();  #  get basic info about uploaded file  $original_name = $_FILES['imagename']['name'];   $original_tmp = $_FILES['imagename']['tmp_name'];   $original_size = $_FILES['imagename']['size'];   $original_type = $_FILES['imagename']['type'];   #  do some basic error trapping and cleanup  if($original_size>$max_file) {     //too large, go back to form     array_push($what_error, "File Size Exceeds Limit");  }  if( $original_size<1) {     //no file was uploaded     array_push($what_error, "Please Select A File To Upload");  }  if(!in_array($original_type, $allowed_type01)) {     // wrong file type     array_push($what_error, "File Type Is Not Allowed");  }  if(count($what_error)>0) {     // send to error page     $_SESSION["resultset"] = $what_error;	 echo $what_error;     //echo '<meta http-equiv="refresh" content="0;URL=mpg_error.php"> ';     exit();  }  #  check to see if file already exists in the folder  #  if it exists AND rename option is , create new name  $does_it = FALSE;     $original_name = strtolower($original_name);  if(file_exists($uploadDir . $original_name)) {     if($image_overwrite == 1) {       //rename       while(!$does_it) {          $add_this_name = rand(1,200);          $original_name = $add_this_name . $original_name;          if(!file_exists($uploadDir . $original_name)) {               $does_it = TRUE;          }       }     }  }  #  attempt to MOVE the image to its proper location  $uploadFile = $uploadDir . $original_name;   if (move_uploaded_file($_FILES['imagename']['tmp_name'], $uploadFile) ) {      // continue  } else {      // send to error page     array_push($what_error, "error - unknown cause");     $_SESSION["resultset"] = $what_error;	 echo "error - unknown cause";     //echo '<meta http-equiv="refresh" content="0;URL=mpg_error.php"> ';     exit();  }   #  check to see if image needs resizing and act accordingly  list($original_width, $original_height) = getimagesize($uploadDir . $original_name);//echo $original_height;  if($max_height<$original_height OR $max_width<$original_width) {     // dimensions are too large - resize    Resize_Image($original_name,$original_name,$max_width,$max_height,$uploadDir,$uploadDir);	echo "image resized done<br>";  }  #  check to see if make thumbnails is true  #  if yes make and store thumb  $thumb_name = $thumb_prefix . $original_name;  if($do_thumb == 1) {    // make a thumb    Resize_Image($thumb_name,$original_name,$thumb_width,$thumb_height,$thumbDir,$uploadDir);	echo "thumb too";	  }}else{?><html><head><title>My Photo Gallery Upload Form</title><link rel="stylesheet" type="text/css" href="shared_files/textsize.css"></head><body bgcolor="#ffffff" text="#0000a6" link="#0000a6" vlink="#0000a6" alink="#0000a6"><div style="position: absolute; top: 50px; left: 300px; width:90%; font-size:10pt; padding:10px;filter:shadow(color:gray);"><div style="HEIGHT:250px; border : solid 2px #0000a0; padding : 4px; WIDTH:380px; OVERFLOW:auto; background-color:#fcfcfc; layer-background-color:#fcfcfc; visibility: visible; "><form action="<?php echo $PHP_SELF ?>" method="post" enctype="multipart/form-data"> <input type="hidden" name="MAX_FILE_SIZE" value="2000000">Browse a File to Upload: <br> <input type="file" name="imagename"><br>Enter a title for the picture<br><input type="text" name="imagetitle" size="40" maxlength="80" value=""><br>Enter your name<br><input type="text" name="artistname" size="40" maxlength="80" value=""><br>Enter a description for the picture<br><input type="text" name="imagedescript" size="40" maxlength="250" value=""><br><input type="submit" value="Upload Image" name="action"><br><br>note: <font color="#ff0080">max file size is: 2megs</font></div></div></body></html><?PHP}?>

Notice from truefusion:
Placed code into codebox.
Edited by truefusion (see edit history)

Share this post


Link to post
Share on other sites

Just wondering if the file size for those really LARGE images exceeds the max file size for uploads?How many megs are they and what are the server limits for max file size?

Share this post


Link to post
Share on other sites

The fact that only large files don't work makes me think that the script is not working due to a lack of memory. I believe Xisto allocates 4 megabytes of RAM for each account to use, and no more. I'm not familiar with the internal workings of the image functions of PHP, but it could be very likely its converting the file to a different (and probably larger) format, so that it can be manipulated by all the imaging functions of PHP with ease.My advice: Get a server with PHP running on your own computer, and try it there. If it works, its a memory problem. If it doesn't, post back :).

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.