I am trying batch resize a bunch of images (~220). I am getting: (everytime)
Fatal error: Allowed memory size of 33554432 bytes exhausted (tried to allocate 19520 bytes) in C:\xampp\htdocs\jason\inc\admin\image.resize.functions.php on line 31
I get the error at the same point EVERY TIME, regardless of how many times I call $n->resizeToHeight($c)
. It will cycle through about 100 photos then crash as soon as I try to resize ONE. Or I can resize 100 photos and it will crash on the 101st. I can even go through all cases of switch($k)
(cause it resize 300 photos), and it will still crash after the 101st instance of the class Image
.
Does my explanation of the error make sense? I am throwing around "100", it may not necessarily be 100, could be 75, or 125 (don't know the exact number). It just consistently crashes at the same spot.
Should be noted: I am testing with XAMPP. I haven't tried it in a real environment (because I can't yet).
resize-images.php
(what is necessary)
foreach($imgs as $img){
$i = new Image($img['cid']);
foreach($go as $k=>$c){
if($i->$k > $c+IMGTOL || $i->$k < $c-IMGTOL) {
$n = new SimpleImage();
$n->load(ORIGABSLOC."/".$i->filename);
$n->resizeToHeight($c);
switch($k){
case "image_height":
$loc = IMGABSLOC;
break;
case "thumb_height":
$loc = THUMBABSLOC;
break;
case "mobile_height":
$loc = MOBILEABSLOC;
break;
}
$n->save($loc."/".$i->filename);
$n->destroy();
unset($n);
}
echo "<style>#$k-$count{background-color:blue;}</style>";
flush_();
}
$count++;
unset($i);
}
I echo the <style>
and flush_()
it so I get a loading bar effect. Doesn't matter if I flush or not, still crashes at the same spot.
Image class
(what is necessary)
class Image{
function Image($id){
$img = selectImages(array('type'=>4,'id'=>$id));
if(sizeof($img) < 1) {
return NULL;
}
if(sizeof($img)>0){
foreach($img[0] as $k=>$c){
if(!is_numeric($k)) {
$this->$k = $c;
}
}
$this->type = $this->display_type;
$size = @getimagesize(IMGABSLOC."/".$this->filename);
$this->height = $size[1];
$this->width = $size[0];
$this->image_height = $size[1];
$this->image_width = $size[0];
$size = @getimagesize(ORIGABSIMGLOC."/".$this->filename);
$this->original_height = $size[1];
$this->original_width = $size[0];
$size = @getimagesize(THUMBABSLOC."/".$this->filename);
$this->thumb_height = $size[1];
$this->thumb_width = $size[0];
$size = @getimagesize(MOBILEABSLOC."/".$this->filename);
$this->mobile_height = $size[1];
$this->mobile_width = $size[0];
$this->anchor = $this->album;
}
else return false;
}
SimpleImage Class
(what is necessary)
class SimpleImage {
var $image;
var $image_type;
function load($filename) {
$image_info = getimagesize($filename);
$this->image_type = $image_info[2];
if( $this->image_type == IMAGETYPE_JPEG ) {
$this->image = imagecreatefromjpeg($filename);
} elseif( $this->image_type == IMAGETYPE_GIF ) {
$this->image = imagecreatefromgif($filename);
} elseif( $this->image_type == IMAGETYPE_PNG ) {
$this->image = imagecreatefrompng($filename);
}
}
function save($filename, $image_type=IMAGETYPE_JPEG, $compression=75, $permissions=null) {
if( $image_type == IMAGETYPE_JPEG ) {
imagejpeg($this->image,$filename,$compression);
} elseif( $image_type == IMAGETYPE_GIF ) {
imagegif($this->image,$filename);
} elseif( $image_type == IMAGETYPE_PNG ) {
imagepng($this->image,$filename);
}
if( $permissions != null) {
chmod($filename,$permissions);
}
}
function output($image_type=IMAGETYPE_JPEG) {
if( $image_type == IMAGETYPE_JPEG ) {
imagejpeg($this->image);
} elseif( $image_type == IMAGETYPE_GIF ) {
imagegif($this->image);
} elseif( $image_type == IMAGETYPE_PNG ) {
imagepng($this->image);
}
}
function resizeToHeight($height) {
$ratio = $height / $this->getHeight();
$width = $this->getWidth() * $ratio;
$this->resize($width,$height);
}
function resize($width,$height) {
$new_image = imagecreatetruecolor($width, $height);
imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
imagedestroy($this->image);
$this->image = $new_image;
}
function destroy(){
imagedestroy($this->image);
}
}