Go Back   Novahq.net Forum > Computers > Web design and Programming
FAQ Community Calendar Today's Posts Search

Web design and Programming Discuss website creation and other programming topics.

Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old 01-03-2009, 12:25 PM
atholon is offline atholon
"ath-hole"

Join Date: Jan 2003
Location: Failville.
Posts: 11,357

Send a message via MSN to atholon
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;
    }
  }
}

?>
__________________
Reply With Quote
  #2  
Old 01-03-2009, 12:56 PM
SilentTrigger is offline SilentTrigger
-1PARA-

Join Date: Sep 2002
Location: Sweden
Posts: 3,972

Actually around 30 MB is the server limit. I presume you have shared hosting?

You can try to add this to a .htaccess file in your root directory.

php_value memory_limit 40M

Note that this will only work if the server admin allows it.
__________________
-1PARA-AlexKall

My photography website



Reply With Quote
  #3  
Old 01-03-2009, 01:04 PM
SilentTrigger is offline SilentTrigger
-1PARA-

Join Date: Sep 2002
Location: Sweden
Posts: 3,972

Forgot one thing, try to gather how much memory is used before the script has done it's magic and after with memory_get_usage(). 30MB is ALOT for a script especially if it's handling a file of only 300 KB.

Don't have alot of experience with shared hosting but I do recall that it's not always that liked when you change the limit as more server memory will be used which they can use for another customer. So might be good to ask first if you can increase it but firstly look over your code so that you close all opened files and so on after your done with it.I'm sure scott or Ici can help you with the code, way to much for my dyslexic arse to read heh
__________________
-1PARA-AlexKall

My photography website




Last edited by SilentTrigger; 01-03-2009 at 01:10 PM.
Reply With Quote
  #4  
Old 01-03-2009, 01:10 PM
atholon is offline atholon
"ath-hole"

Join Date: Jan 2003
Location: Failville.
Posts: 11,357

Send a message via MSN to atholon
Ok the .htaccess worked! Cuz you rocketh ST.

Now... I will do the mem usage thing.

Any idea as to why I am getting that other warning?
I don't think I am using globals at all in that script.
__________________
Reply With Quote
  #5  
Old 01-03-2009, 01:13 PM
SilentTrigger is offline SilentTrigger
-1PARA-

Join Date: Sep 2002
Location: Sweden
Posts: 3,972

Actually not sure why you're getting that error, searched your script for obvious things but didn't see any declaration of any globals. But then as noted above I haven't read it though because thats just too much to read. I hate to read I'm afraid, need to be a small amount

The most I've read is 10 pages in a book and that was like klimbing mount everest mentaly. Well besides the code I write on the Robots but as it's me writing it there's not an issue hehe
__________________
-1PARA-AlexKall

My photography website



Reply With Quote
  #6  
Old 01-03-2009, 01:14 PM
atholon is offline atholon
"ath-hole"

Join Date: Jan 2003
Location: Failville.
Posts: 11,357

Send a message via MSN to atholon
OK well the memory usage:
Beginning of makeThumb:2793000
End of makeThumb:18579868
Beginning of makeOtherSize:18579868
End of makeOtherSize:18579976

My guess is that the page I am using is keeping the temp file in memory... something else in makeThumb isn't right either.
__________________
Reply With Quote
  #7  
Old 01-03-2009, 01:16 PM
SilentTrigger is offline SilentTrigger
-1PARA-

Join Date: Sep 2002
Location: Sweden
Posts: 3,972

memory_limit in php should be script based and not serverside. Looks like it isn't :/

Did you take a reading before you do anything? Shouldn't use much memory at all if you read it in the beginning of the script before you do anything
__________________
-1PARA-AlexKall

My photography website



Reply With Quote
  #8  
Old 01-03-2009, 01:24 PM
SilentTrigger is offline SilentTrigger
-1PARA-

Join Date: Sep 2002
Location: Sweden
Posts: 3,972

PHP Code:
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 ); 
If the type is gif you're not destroying tempImage
__________________
-1PARA-AlexKall

My photography website



Reply With Quote
  #9  
Old 01-03-2009, 01:29 PM
atholon is offline atholon
"ath-hole"

Join Date: Jan 2003
Location: Failville.
Posts: 11,357

Send a message via MSN to atholon
Just figured it out..with the memory deal....the other stuff with globals still confuses me.


imagedestroy( $this->theImage );
__________________
Reply With Quote
  #10  
Old 01-03-2009, 01:30 PM
SilentTrigger is offline SilentTrigger
-1PARA-

Join Date: Sep 2002
Location: Sweden
Posts: 3,972

So what was it then?
__________________
-1PARA-AlexKall

My photography website



Reply With Quote
  #11  
Old 01-03-2009, 01:31 PM
atholon is offline atholon
"ath-hole"

Join Date: Jan 2003
Location: Failville.
Posts: 11,357

Send a message via MSN to atholon
imagedestroy( $this->theImage );

You were right too, I had just fixed that as well. I was forgetting to get rid of the first image when it was created.
__________________
Reply With Quote
  #12  
Old 01-03-2009, 01:33 PM
SilentTrigger is offline SilentTrigger
-1PARA-

Join Date: Sep 2002
Location: Sweden
Posts: 3,972

Ah that explain it, no wonder I didn't find that when searching for imagedestory when it's not there hehe
__________________
-1PARA-AlexKall

My photography website



Reply With Quote
  #13  
Old 01-03-2009, 01:34 PM
atholon is offline atholon
"ath-hole"

Join Date: Jan 2003
Location: Failville.
Posts: 11,357

Send a message via MSN to atholon
Beginning of make thumb:2802320
End of make thumb:2816216
Beginning of make other size:2816216
End of make other size:2816328

Now I just need to figure out how to kill the temp file that PHP stores for the image in the files array.
__________________
Reply With Quote
  #14  
Old 01-03-2009, 01:36 PM
SilentTrigger is offline SilentTrigger
-1PARA-

Join Date: Sep 2002
Location: Sweden
Posts: 3,972

Not that the other one was either but there was an imagedestroy above it.

Sorry for not actually reading the code, bit pathetic, but it's a mental block, as I had trubles reading as a kid I developed a hate for it and it continues still today hehe

How do you gather the image that you upload? POST? Create a temporary file? Have you closed the temporary file?

Guess this is not the way you do it.
__________________
-1PARA-AlexKall

My photography website



Reply With Quote
  #15  
Old 01-03-2009, 01:47 PM
atholon is offline atholon
"ath-hole"

Join Date: Jan 2003
Location: Failville.
Posts: 11,357

Send a message via MSN to atholon
I uploaded the file and saved it to the server then I used the server file in this script....thing is that the upload.php file is what instantiates the imagecropper class....so technically memory is still used for that temp file.
__________________
Reply With Quote
  #16  
Old 01-03-2009, 01:49 PM
SilentTrigger is offline SilentTrigger
-1PARA-

Join Date: Sep 2002
Location: Sweden
Posts: 3,972

And you let the upload.php file close the tmp file when it's done with it right?
__________________
-1PARA-AlexKall

My photography website



Reply With Quote
  #17  
Old 01-03-2009, 02:01 PM
SilentTrigger is offline SilentTrigger
-1PARA-

Join Date: Sep 2002
Location: Sweden
Posts: 3,972

Is the image by any chance around 5-6 mb in size?
__________________
-1PARA-AlexKall

My photography website



Reply With Quote
  #18  
Old 01-03-2009, 02:12 PM
atholon is offline atholon
"ath-hole"

Join Date: Jan 2003
Location: Failville.
Posts: 11,357

Send a message via MSN to atholon
I am not sure how to do it. From my understanding PHP is supposed to kill any post data when the browser changes pages. The file I uploaded was 1.3 mb
__________________
Reply With Quote
  #19  
Old 01-03-2009, 02:14 PM
atholon is offline atholon
"ath-hole"

Join Date: Jan 2003
Location: Failville.
Posts: 11,357

Send a message via MSN to atholon
Silent Trigger gets the king and winner award of the month
__________________
Reply With Quote
  #20  
Old 01-03-2009, 02:22 PM
SilentTrigger is offline SilentTrigger
-1PARA-

Join Date: Sep 2002
Location: Sweden
Posts: 3,972

Never heared that (defenetly doesn't mean it's false) but have you tried to close the file/flush the temp file?

Heh, why? Haven't done anything lol
__________________
-1PARA-AlexKall

My photography website



Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Question ƒalcon Joint Operations 3 10-12-2006 04:45 PM
just a question Lucky Feedback / Novahq.net Support 3 07-29-2005 11:10 AM
Question SMITIE Sigs and Graphics 8 07-18-2005 07:20 PM
Question :D V99 Delta Force 7 09-10-2003 12:23 AM
i have a question †W¡§†ëÐ «M§§» Hardware and Software 5 08-23-2003 09:39 AM


All times are GMT -5. The time now is 02:18 PM.




Powered by vBulletin®