尝试在php中调整图像大小时如何处理此内存泄漏?

I try to resize all images in one directory. My code works find until I try to resize a directory with 70 pictures. I already use imagedestroy and unset. But i still fail with "Fatal error: Allowed memory size of 67108864 bytes exhausted (tried to allocate 17232 bytes) in".(My php memory is set to 64M and can not be modified)

$file = scandir ( $dir );
    foreach ( $file as $key => $filename ) {
    $src = $dir . '/' . $filename;
    $dst = $dir . '/' . $filename;
    $width = 660; // max width
    $height = 2000; // max height
    $message = image_resize ( $src, $dst, $width, $height, $crop = 0 );
    if ($message == 1)
        echo "$filename resize success!</br>";
    else
        echo "$filename $message</br>";
}

function image_resize($src, $dst, $width, $height, $crop = 0) {
    if (! list ( $w, $h ) = getimagesize ( $src ))
        return "Unsupported picture type!";

    $type = strtolower ( substr ( strrchr ( $src, "." ), 1 ) );
    if ($type == 'jpeg')
        $type = 'jpg';
    switch ($type) {
        case 'bmp' :
            $img = imagecreatefromwbmp ( $src );
            break;
        case 'gif' :
            $img = imagecreatefromgif ( $src );
            break;
        case 'jpg' :
            $img = imagecreatefromjpeg ( $src );
            break;
        case 'png' :
            $img = imagecreatefrompng ( $src );
            break;
        default :
            return "Unsupported picture type!";
    }

    // resize
    if ($crop) {
        if ($w < $width or $h < $height) {
            imagedestroy ( $img );
            unset ( $img );
            return "Picture is too small!";
        }
        $ratio = max ( $width / $w, $height / $h );
        $h = $height / $ratio;
        $x = ($w - $width / $ratio) / 2;
        $w = $width / $ratio;
    } else {
        if ($w < $width and $h < $height) {
            imagedestroy ( $img );
            unset ( $img );
            return "Picture is too small!";
        }
        $ratio = min ( $width / $w, $height / $h );
        $width = $w * $ratio;
        $height = $h * $ratio;
        $x = 0;
    }

    $new = imagecreatetruecolor ( $width, $height );

    // preserve transparency
    if ($type == "gif" or $type == "png") {
        imagecolortransparent ( $new, imagecolorallocatealpha ( $new, 0, 0, 0, 127 ) );
        imagealphablending ( $new, false );
        imagesavealpha ( $new, true );
    }

    imagecopyresampled ( $new, $img, 0, 0, $x, 0, $width, $height, $w, $h );

    switch ($type) {
        case 'bmp' :
            imagewbmp ( $new, $dst );
            break;
        case 'gif' :
            imagegif ( $new, $dst );
            break;
        case 'jpg' :
            imagejpeg ( $new, $dst );
            break;
        case 'png' :
            imagepng ( $new, $dst );
            break;
    }
    imagedestroy ( $img );
    imagedestroy ( $new );
    unset ( $img );
    unset ( $new );
    return true;
}

Well you have that problem because you have to change in php.ini max_execution_time and memory_limit you can use which one you want for memory limit either change in php.ini or use ini_set

in php.ini

memory_limit = 128mb;

in your php script

ini_set('memory_limit','16M');

in .htaccess

php_value memory_limit 8M;

Note: This method will only work if PHP is executed as an apache module.