天天看点

php 调整图片内存,(图像大小调整)每次都耗尽内存,即使没有创建图像实例

我正在尝试批量调整一组图像的大小(~220)。我得到:(每次)

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

$n->resizeToHeight($c)

. 它将循环通过大约100张照片,然后崩溃,只要我试图调整一个。或者我可以调整100张照片的大小,它会在101街崩溃

switch($k)

(因为它调整了300张照片的大小),并且在类的第101个实例之后仍然会崩溃

Image

我对错误的解释有意义吗?我在投“100”,可能不一定是100,可能是75,或125(不知道确切的数字)。它总是在同一地点坠毁。

需要注意的是:我正在使用XAMPP进行测试。我还没有在真实的环境中尝试过(因为我还不能)。

resize-images.php

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 "";

flush_();

}

$count++;

unset($i);

}

我回应

flush_()

Image class

(什么是必要的)

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

(什么是必要的)

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);

}

}