Jump to content

[SOLVED] Memory problems


tjodolv

Recommended Posts

It's me, again... :P

 

This problem may be a server problem, but I'll start by asking here. I have written a script for uploading images. When i attempt to upload images with a certain filesize, i get a fatal error. I tried with different filesizes, and narrowed down the approximate borderline to between 30k and 40k.

 

A 76K .jpeg file works fine.

 

A 1.7M .jpeg gives the error:

Fatal error: Allowed memory size of 8388608 bytes exhausted (tried to allocate 2608 bytes) in /Users/test/Sites/cms/lib/functions.inc.php on line 1050

 

A 20K .png file works fine.

 

A 40K .png file gives the error:

Fatal error: Allowed memory size of 8388608 bytes exhausted (tried to allocate 3686400 bytes) in /Users/test/Sites/cms/lib/functions.inc.php on line 1056

 

The line in question is part of a function i found on the web:

<?php
// this function originally found at http://www.findmotive.com - a wee bit modified
function cropImage($nw, $nh, $source, $stype, $dest) {
$size = getimagesize($source);
$w = $size[0];
$h = $size[1];
switch($stype) {
	case 'image/gif':
	$simg = imagecreatefromgif($source);
	break;
	case 'image/jpeg':
	$simg = imagecreatefromjpeg($source); // this is line 1050
	break;
	case 'image/pjpeg':
	$simg = imagecreatefromjpeg($source);
	break;
	case 'image/png':
	$simg = imagecreatefrompng($source); // this is line 1056
	break;
}
$dimg = imagecreatetruecolor($nw, $nh);
$wm = $w/$nw;
$hm = $h/$nh;
$h_height = $nh/2;
$w_height = $nw/2;
if($wm> $hm) {
	$adjusted_width = $w / $hm;
	$half_width = $adjusted_width / 2;
	$int_width = $half_width - $w_height;
	imagecopyresampled($dimg,$simg,-$int_width,0,0,0,$adjusted_width,$nh,$w,$h);
}

elseif(($wm <$hm) || ($wm == $hm)) {
	$adjusted_height = $h / $wm;
	$half_height = $adjusted_height / 2;
	$int_height = $half_height - $h_height;
	imagecopyresampled($dimg,$simg,0,-$int_height,0,0,$nw,$adjusted_height,$w,$h);
}
else {
	imagecopyresampled($dimg,$simg,0,0,0,0,$nw,$nh,$w,$h);
}
imagejpeg($dimg,$dest,100);
return true;
} // end cropImage
?>

 

Settings read from phpinfo():

file_uploads On

memory_limit 8M

post_max_size 32M

upload_max_filesize 32M

 

SERVER_SOFTWARE Apache/2.0.59 (Unix) PHP/5.2.3 DAV/2

 

GD Support enabled

GD Version bundled (2.0.34 compatible)

 

What do I need to fix?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.