Jump to content

weird problem with uploading big images


garydt

Recommended Posts

  • Replies 92
  • Created
  • Last Reply

OK new style

 

i may of messed the paths up but the logic follows this

 

create the 300x300 then from that create the 140x140 using the same function

so it will upload

create the 300 image then open the 300 image and create the 140 image.. this should reduce the time and memory required..

 

<?php

if(isset($_POST['sent']))

{

unset($submit);

if (isset($_POST['sent']))

{

$submit = true;

}else{

die('Error: No file got sent');

}

 

if($_FILES['file']['name'])

{

$arr  = getimagesize($_FILES['file']['tmp_name']);

 

list($width, $height, $type, $attr) = getimagesize($_FILES['file']['tmp_name']);

if($type == FALSE)

{

header("Location: uplodsmalpic3.php?picnum=$num");

exit();

}elseif($type != 2){

header("Location: uplodsmalpic3.php?picnum=$num");

exit();    

}

}

}

$fieldname = $_FILES['file'];

 

// make a note of the directory that will recieve the uploaded file

// full url $uploadsDirectory = $_SERVER['DOCUMENT_ROOT'] . $directory_self . 'uploads/';

$uploadsDirectory = './uploads/';

 

//Upload image

$uploadFilename = $uploadsDirectory.$fieldname['name'];

 

// now let's move the file to its final location and allocate the new filename to it

move_uploaded_file($fieldname['tmp_name'], $uploadFilename);

 

$uploadedphoto = $uploadsDirectory.$fieldname['name'];

 

$user = $_SESSION['MM_Username'];

 

CP("1");

$a = rand(1,99);

resizeimage('./uploads/'.$fieldname['name'], './smallpics/',300);

rename("smallpics/".$fieldname['name'], "smallpics/".$user.$a.$fieldname['name']);

$smallpics = './smallpics/'.$user.$a.$fieldname['name'];

CP("2");

resizeimage("smallpics/".$user.$a.$fieldname['name']), './thumbnails/',140);

rename("thumbnails/".$fieldname['name'], "thumbnails/".$user.$a.$fieldname['name']);

$smallimage = './thumbnails/'.$user.$a.$fieldname['name'];

CP("3");

unlink($uploadedphoto);

 

 

function CP($Num)

{

echo "CP: $Num<br />";

ob_flush();

flush();

set_time_limit ( 0 );

}

 

 

function resizeimage($name, $topath, $maxSize)

{

//$filename = './uploads/'.$name;

$filename = $name;

if(file_exists($filename))

{

ini_set('memory_limit', '500M');

$image = imagecreatefromjpeg($filename); //unresized image

$im_info = getimagesize($filename); //unresized image

$im_width = $im_info[0];

$im_height = $im_info[1];

$im_flag = $im_info[2];

 

//max height: 140px; max width: 140px

if($im_width >= $im_height) {

$im_divice = $im_width / $maxSize;

}else {

$im_divice = $im_height / $maxSize;

}

$thumb_width = $im_width / $im_divice;

$thumb_height = $im_height / $im_divice;

 

//create empty image

ini_set('memory_limit', '500M');

CP("startImage");

$thumb = imagecreatetruecolor($thumb_width, $thumb_height);

//resize image

imagecopyresampled($thumb, $image, 0, 0, 0, 0, $thumb_width, $thumb_height, $im_width, $im_height);

 

if(imagejpeg($thumb, $topath.$name, 80)) { //image-resource, filename, quality

return 1;

}

 

imagedestroy($thumb); //destroy temporary image-resource

imagedestroy($image); //destroy temporary image-resource

}

}

 

 

 

?>

 


Link to comment
Share on other sites

nothing much

echo's CP: startImage to the page

flushs the page and resets the timer..

 

i used a it a few times so created i function instead

 

CP function

 

function CP($Num)
{
echo "CP: $Num<br />";
ob_flush();
flush();
set_time_limit ( 0 );
}

 

 

Link to comment
Share on other sites

i don't think its the GDversion but i would suggest breaking the script up into 3 scripts

1. upload

2. create 300 image

3. create 140 image (from the 300 image)

 

maybe link them via header("Location: next.php);

 

see if that helps, as you are putting a lot of stress on server with 1 script..

Link to comment
Share on other sites

I tried that with your script where it just resizes 3 images -  i tried it with 1 image and it still failed.

 

i've been looking at the phpinfo for my server and it got-

max-execution-time 30

memory_limit 8m

post-max-size 8m

 

so does that mean it isn't reading my php.ini file? could that be the problem?

Link to comment
Share on other sites

You definitely need a lot of memory to do that.  I do something similar with 5000x5000 pixels (PNG), it takes 189 MB.  Even 1000x1000 pixels took 90 MB.

 

Try these three scenarios--for all three, do not use the temp folder--upload somewhere permanent.  This will help narrow down the issue.  You can also try them in reverse order.  Save the file before doing any resizing.

 

BTW, if the memory limits are the problem, you should have a fatal PHP error in a file.  Make sure log_errors is on so you can read that.  A 500 server error sounds like a non-php issue.

 

#1

1.) Upload a huge image using FTP

2.) With PHP, open it. Do not resize it.

3.) Save it in another location.

 

If this works, do this:

#2

1.) Same as before.

2.) With PHP, open it.  Resize it.

3.) Save it in another location.

 

If this works, do this:

1.) Upload the image using PHP.

2.) Save it in a non-temp location.

3.) Reopen it, resize it, and save it in another location.

Link to comment
Share on other sites

I tried the 1st scenario. Php wouldn't open a big image, just got the small red X  even when i put -

ini_set('memory_limit', '500M');

ini_set('max_execution_time', '300');

 

at the start of the script.

What does this point to?

Thanks

Link to comment
Share on other sites

Okay, let's see if we can get that narrowed down.  At this point, then, is it safe to say the issue actually might not be uploading but something else?

 

Put just the short code up here for scenario 1.  I will try that on my server and see what the issue is.  Also, set memory_limit and that max one.  But, do this after.

 

$myResult=ini_get('memory_limit');

var_dump($myResult);

 

Do the same with the other one.  If it dumps false, the change is not taking.  If it dumps your numbers, then you are good.  Do this and comment out your other code to see if the changes work.

 

For the time, use this one: set_time_limit (200);

 

Also, don't put the M.  Put the number:

 

ini_set('memory_limit', "250000000");
$testmem=ini_get('memory_limit');
print "memlimit is $testmem";

 

Jim

 

 

Link to comment
Share on other sites

Thanks,

I did that and it comes up with-

memlimit is 250000000string(9) "250000000"

 

still the large image doesn't open, only a small one,

This is the code i did

<?php

ini_set('max_execution_time', '300'); 
ini_set('memory_limit', "250000000");
$testmem=ini_get('memory_limit');
print "memlimit is $testmem";
$myResult=ini_get('memory_limit');
var_dump($myResult);

$pic = './uploads/img1.jpg';
print $pic;
$pic1 = './thumbnails/garydt88Picture 003.jpg';
print $pic1;

?>
<body>
<img src="<?php print $pic; ?>" border="0"  />
<img src="<?php print $pic1; ?>" border="0"  />

</body>
</html>

Link to comment
Share on other sites

I don't see where you are opening them.

 

It probably has nothing to do with this, but I'd avoid spaces in filenames just to be safe.

 

Put the code up here that you are using to do that.  Would you be willing to use other code as well?  I could post mine up here and see if that makes a difference.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.


×
×
  • Create New...

Important Information

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.