This is how I handle memory increase wherever I need to do so:
// INCREASE MEMORY IMMEDIATELY BEFORE PERFORMING ANY IMAGE-RELATED FUNCTIONS IF $willIncreaseMemory IS TRUE (**NOT RECOMMENDED**)
$mem = (int)memory_get_usage();
// MULTIPLY YOUR MEMORY TEMPORARILY (10XFILESIZE LARGER SHOULD *DEFINITELY* BE ENOUGH UNTIL END!)
if ($willIncreaseMemory && (int)ini_get('memory_limit') * 10 * @filesize(actual_path("$locationPath/$this->fileName")) > $mem) /* INCREASE ONLY IF > $mem */ {
@ini_set('memory_limit', ceil((int)ini_get('memory_limit') * 10 * filesize(actual_path("$locationPath/$this->fileName")) / 100000) . 'M');
} elseif ($willIncreaseMemory && (int)$origWidth > 0 && (int)$origHeight > 0 && ceil((int)$origWidth * (int)$origHeight * 5) > $mem) {
@ini_set('memory_limit', ceil((int)$origWidth * (int)$origHeight * 5 / 10000) . 'M');
}
The steps for resizing an image will be as follows:
1) Use NokiaImageCleaner to "clean up" the image if it happens to have been spawned by a Nokia Camera Phone:
/**
* This class will clean up Nokia-generated images
*
* @author Paul Visco (modified by Phil Powell)
* @version 1.2.0
* @since Version 1.2.0
* @package IMAGE_CATALOG::IMAGE
* @link http://us2.php.net/manual/en/function.imagecreatefromstring.php#55114
*/
class NokiaImageCleaner extends ImageResizer {
/**
* @access private
* @var mixed $image_name
*/
var $image_name;
/**
* @access private
* @var char $debug (default 'n')
*/
var $debug = 'n';
/**
* Constructor
*
* @access public
* @param mixed $image_name
* @param char $debug (default 'n')
*/
function NokiaImageCleaner($image_name, $debug = 'n') { // CONSTRUCTOR
parent::ImageResizer($image_name); // "super()"
$this->image_name = $image_name;
$this->debug = $debug;
}
/**
* This will "eat" the corrupted data from the file
*
* @access private
* @param mixed $imagestring (reference)
* @return boolean
* @see checkFixPPC6700
*/
function &eat(&$imagestring) { // STATIC BOOLEAN METHOD
// IF NO IMAGE RESOURCE OBJECT IS CREATED THEN YOU MUST REMOVE ALL CORRUPTED DATA UNTIL THE OBJECT CAN BE CREATED
while (strlen($imagestring) > 0 &&
($imageObj = @imagecreatefromstring(checkFixPPC6700($imagestring, 'isData'))) === false ||
($imageObj = @imagecreatefromstring(checkFixPPC6700($imagestring, 'isData'))) === null
) $imagestring = substr_replace($imagestring, '', -3, -2); // CUT OUT THE BAD BYTES ONE BY ONE
if (strcmp(strtolower($this->debug), 'y') == 0) echo "\n<br />Nasty bits eaten!! " . $this->image_name . ' is fixed now thanks to p:labs!';
return true;
}
/**
* Fix the file
*
* *NOTE* This class method will only perform the fix upon images that are spawned by Nokia™ camera phones, which will for now only create JPEG images. All other image types
* will pass through this method as returning true by default as they cannot be checked by this method.
*
* @access public
* @param mixed $album (optional)
* @return boolean
* @see file_get_contents
* @see file_put_contents
* @see actual_path
* @see checkFixPPC6700
* @see memory_get_usage
* @todo Future implementation of handling non-JPEG images if Nokia™ produces non-JPEG camera phone images
*/
function &fix($album = '') { // STATIC BOOLEAN METHOD
global $section, ${$section . 'LocationPath'}, $willIncreaseMemory;
if ($album) $_REQUEST['album'] = $album;
$locationPath = ($_REQUEST['album']) ? "${$section . 'LocationPath'}/" . $_REQUEST['album'] : ${$section . 'LocationPath'};
list($width, $height) = @getimagesize(actual_path(realpath("$locationPath/" . $this->{$section . '_name'})));
$mem = (int)memory_get_usage();
// MULTIPLY YOUR MEMORY TEMPORARILY (10XFILESIZE LARGER SHOULD *DEFINITELY* BE ENOUGH UNTIL END!)
if ($willIncreaseMemory && (int)ini_get('memory_limit') * 10 * filesize(actual_path("$locationPath/" . $this->{$section . '_name'})) > $mem) {
@ini_set('memory_limit', ceil((int)ini_get('memory_limit') * 10 * filesize(actual_path("$locationPath/" . $this->{$section . '_name'})) / 100000) . 'M');
} elseif ($willIncreaseMemory && (int)$width > 0 && (int)$height > 0 && ceil((int)$width * (int)$height * 5) > $mem) {
@ini_set('memory_limit', ceil((int)$width * (int)$height * 5 / 10000) . 'M');
} elseif ((int)$width * (int)$height * 5 > $mem) {
$this->isSuccessful = false;
$this->setErrorArray(array('action' => "Unable to do required fix for $section \"" . $this->{$section . '_name'} . "\" due to exceeding memory limit of \"$mem\" KB"));
}
$imagestring = @file_get_contents(actual_path(realpath("$locationPath/" . $this->{$section . '_name'})));
// TEST TO SEE IF IT IS A NOKIA IMAGE OR IF IT HAS BEEN PREVIOUSLY CORRUPTED
if (substr_count($imagestring, chr(0x28) . chr(0x36) . chr(0x28) . chr(0x2B)) == 0 || @imagecreatefromstring(checkFixPPC6700($imagestring, 'isData'))) {
if (strcmp(strtolower($this->debug), 'y') == 0) echo "\n<br />" . $this->image_name . ' is not a corrupted Nokia pic';
return true;
}
$this->eat($imagestring); // STRIP OUT THE "FUNK E CRAP" FROM THE FILE
@file_put_contents(actual_path("$locationPath/$this->image_name"), $imagestring); // WRITE THE FILE BACK TO ITSELF
return true; //RETURN TRUE FOR FIXED
}
}
2) resize it using imagereatefrom[jpeg|gif|png]
$image = @imagecreatefromjpeg('/path/to/your/jpeg');
Phil