"ath-hole"
Join Date: Jan 2003
Location: Failville.
Posts: 11,357
|
|
Another question
Hi there,
I wrote a PHP class to crop images using the gd library...works fine on my local server but it appears my host has a smaller memory limit...any ideas of how to work around it? It seems I cannot upload any images larger than 300k with this cropping class.
This is the error I get:
Quote:
Fatal error: Allowed memory size of 33554432 bytes exhausted (tried to allocate 2048 bytes) in /home/digitalh/public_html/includes/ImageCropper.php on line 124
Warning: Unknown: Your script possibly relies on a session side-effect which existed until PHP 4.2.3. Please be advised that the session extension does not consider global variables as a source of data, unless register_globals is enabled. You can disable this functionality and this warning by setting session.bug_compat_42 or session.bug_compat_warn to off, respectively. in Unknown on line 0
|
The class is sort of long:
Code:
<?php
class ImageCropper
{
var $mediumWidth, $mediumHeight, $resizeWidth, $resizeHeight, $theImage, $width, $height, $newWidth, $newHeight, $imageType, $thumbSize, $error;
function ImageCropper()
{
$this->mediumWidth = 275;
$this->mediumHeight = 200;
$this->resizeWidth = 325;
$this->resizeHeight = 250;
$this->error = null;
} // Constructor
function makeThumb($image, $iName, $iSize=110) // Thumbnails are square so there are only three params
{
$this->thumbSize = $iSize;
list($this->width,$this->height, $this->imageType) = getimagesize($image);
if($this->generateImageFromType($image))
{
if ($this->width > $this->height)
{
$this->resizeWidth = $this->thumbSize + 100;
$this->makeLandscape();
}
else if ($this->height > $this->width)
{
$this->makePortrait();
$this->resizeHeight = $this->thumbSize + 100;
}
else
{
$this->resizeWidth = $this->thumbSize + 100;
$this->makeLandscape();
}
if ($this->width > $this->resizeWidth || $this->height > $this->resizeHeight)
{
if($this->imageType != IMAGETYPE_GIF)
{
$tempImage = imagecreatetruecolor( $this->newWidth, $this->newHeight );
$temp = imagecreatetruecolor( $this->thumbSize, $this->thumbSize);
}
else
{
$tempImage = imagecreate( $this->newWidth, $this->newHeight );
$temp = imagecreate( $this->thumbSize, $this->thumbSize);
}
imagecopyresampled( $tempImage, $this->theImage, 0, 0, 0, 0, $this->newWidth, $this->newHeight, $this->width, $this->height );
imagecopy( $temp, $tempImage, 0, 0,($this->newWidth/2)-($this->thumbSize/2),($this->newHeight/2)-($this->thumbSize/2), $this->newWidth, $this->newHeight );
$this->renderImage($temp, $iName);
imagedestroy( $temp );
imagedestroy( $tempImage );
}
else
{
if($this->imageType != IMAGETYPE_GIF)
{
$temp = imagecreatetruecolor( $this->thumbSize, $this->thumbSize);
}
else
{
$tempImage = imagecreate( $this->newWidth, $this->newHeight );
$temp = imagecreate( $this->thumbSize, $this->thumbSize);
}
imagecopy( $temp, $this->theImage, ($this->thumbSize/2)-($this->width/2), ($this->thumbSize/2)-($this->height/2),0,0, $this->width, $this->height ); // resize to width
$this->renderImage($temp, $iName);
imagedestroy( $temp );
}
}
else
echo $this->error;
} // End Make Thumb
function makeOtherSize($image, $iName, $iWidth=275, $iHeight=200)
{
$this->mediumWidth = $iWidth;
$this->mediumHeight = $iHeight;
$this->resizeWidth = $iWidth + 50;
$this->resizeHeight = $iHeight + 50;
list($this->width,$this->height, $this->imageType) = getimagesize($image);
if($this->generateImageFromType($image))
{
if ($this->width > $this->height)
{
$this->makeLandscape();
}
else if ($this->height > $this->width)
{
$this->makePortrait();
}
else
{
$this->makeLandscape();
}
if ($this->width > $this->resizeWidth || $this->height > $this->resizeHeight)
{
if($this->imageType != IMAGETYPE_GIF)
{
$tempImage = imagecreatetruecolor( $this->newWidth, $this->newHeight );
$temp = imagecreatetruecolor( $this->mediumWidth, $this->mediumHeight);
}
else
{
$tempImage = imagecreate( $this->newWidth, $this->newHeight );
$temp = imagecreate( $this->mediumWidth, $this->mediumHeight);
}
imagecopyresampled( $tempImage, $this->theImage, 0, 0, 0, 0, $this->newWidth, $this->newHeight, $this->width, $this->height );
imagecopy( $temp, $tempImage, 0, 0,($this->newWidth/2)-($this->mediumWidth/2),($this->newHeight/2)-($this->mediumHeight/2), $this->newWidth, $this->newHeight );
$this->renderImage($temp, $iName);
imagedestroy( $temp );
imagedestroy( $tempImage );
}
else
{
if($this->imageType != IMAGETYPE_GIF)
{
$temp = imagecreatetruecolor( $this->mediumWidth, $this->mediumHeight);
}
else
{
$temp = imagecreate( $this->mediumWidth, $this->mediumHeight);
}
imagecopy( $temp, $this->theImage, ($this->mediumWidth/2)-($this->width/2), ($this->mediumHeight/2)-($this->height/2),0,0, $this->width, $this->height ); // resize to width
$this->renderImage($temp, $iName);
imagedestroy( $temp );
imagedestroy( $tempImage );
}
}
else
{
echo $this->error;
}
}
function generateImageFromType($toBeMade)
{
switch($this->imageType)
{
case IMAGETYPE_GIF:
$this->theImage = imagecreatefromgif($toBeMade) or die("Error: Cannot find image!");
return true;
break;
case IMAGETYPE_JPEG:
$this->theImage = imagecreatefromjpeg($toBeMade) or die("Error: Cannot find image!");
return true;
break;
case IMAGETYPE_PNG:
$this->theImage = imagecreatefrompng($toBeMade) or die("Error: Cannot find image!");
return true;
break;
default:
$this->error("This image type is not allowed:".$this->imageType);
return false;
break;
}
}
function renderImage($theImage,$iName)
{
switch ($this->imageType)
{
case IMAGETYPE_JPEG:
imagejpeg($theImage, $iName);
break;
case IMAGETYPE_GIF:
imagegif($theImage, $iName);
break;
case IMAGETYPE_PNG:
imagepng($theImage, $iName);
break;
default:
break;
}
}
function makeLandscape()
{
if ($this->width > $this->resizeWidth)
{
$ratio = $this->exceededAspectRatio();
if ($ratio == true)
{
$this->newHeight = $this->resizeHeight;
$this->newWidth = ceil( $this->newHeight*($this->width/$this->height));
echo("got here");
}
else
{
$this->newWidth = $this->resizeWidth;
$this->newHeight = ceil( $this->newWidth*($this->height/$this->width));
}
}
else
{
$this->newWidth = $this->width;
$this->newHeight = $this->height;
}
} // End Make LandScape
function makePortrait()
{
if ($this->height > $this->resizeHeight)
{
$ratio = $this->exceededAspectRatio();
if ($ratio == true)
{
$this->newHeight = $this->resizeHeight;
$this->newWidth = ceil( $this->newHeight*($this->width/$this->height));
echo("got here");
}
else
{
$this->newHeight = $this->resizeHeight;
$this->newWidth = ceil( $this->newHeight*($this->width/$this->height));
}
}
else
{
$this->newWidth = $this->width;
$this->newHeight = $this->height;
}
}
function exceededAspectRatio()
{
if ($this->width > $this->height)
{
if ($this->width / $this->height > 2)
return true;
}
else
{
return false;
}
if ($this->height > $this->width)
{
if ($this->height / $this->width > 2)
return true;
}
else
{
return false;
}
}
}
?>
|