PHP使用GD创建动态图像

i wrote a function for create & crop images with PHP. i load a list of Images (over 500 images) dynamically create and crop these images in a foreach loop.

i tried this with the new crop function in PHP >= 5.5:

resource imagecrop ( resource $image , array $rect )

this function is buggy, since it adds a black line at the Bottom of the created Image. but i can create so many images as needed in a foreach loop using this function.

https://bugs.php.net/bug.php?id=67447&edit=3

my Function using imagecrop :

function createAndSaveImage($imagePath, $targetName){
    $imagesize = getimagesize($imagePath);
    $imagewidth = $imagesize[0];
    $imageheight = $imagesize[1];
    $imagetype = $imagesize[2];
    switch ($imagetype){
        case 1: // GIF 
            $image = imagecreatefromgif($imagePath);
            break;
        case 2: // JPEG 
            $image = imagecreatefromjpeg($imagePath);
            break;
        case 3: // PNG 
            $image = imagecreatefrompng($imagePath);
            break;
        default:
            return false;
    }
    $rect = array();
    $rect["x"] = 230;
    $rect["y"] = 140;
    $rect["width"] = 40;
    $rect["height"] = 30;
    $thumb = imagecrop( $image , $rect );
    if( $imagetype == IMAGETYPE_PNG ){
        imagepng($thumb, $targetName,9);
    }else{
        imagejpeg($thumb, $targetName,100);
    }    
    return true;
}

now instead of imagecrop i tried the same using imagecopy OR imagecopyresampled.

i call my function in a foreach loop. it creates goog croped images(no black lines at bottom), BUT it breaks allways after 300 items (sometime more).

my Function using imagecopy OR imagecopyresampled:

function createAndSaveImage($imagePath, $targetName){
    $imagesize   = getimagesize($imagePath);
    $imagewidth  = $imagesize[0];
    $imageheight = $imagesize[1];
    $imagetype   = $imagesize[2];
    switch ($imagetype){
        case 1: // GIF 
            $image = imagecreatefromgif($imagePath);
            break;
        case 2: // JPEG 
            $image = imagecreatefromjpeg($imagePath);
            break;
        case 3: // PNG 
            $image = imagecreatefrompng($imagePath);
            break;
        default:
            return false;
    }
    $thumbwidth = 40;
    $thumbheight = 30;
    $thumb = imagecreatetruecolor($thumbwidth, $thumbheight);
    imagecopy($thumb, $image, 0, 0, 230, 140, $imagewidth, $imageheight);
    //imagecopyresampled( $thumb, $image, 0, 0, 230, 140, $imagewidth, $imageheight, $imagewidth, $imageheight );
    if( $imagetype == IMAGETYPE_PNG ){
        imagepng($thumb, $targetName,9);
    }else{
        imagejpeg($thumb, $targetName,100);
    }
    imagedestroy($thumb);
    return true;
}

any idea why? is it a RAM / Cache issue?

UPDATE [phpInfo]:

max_execution_time         0    30
max_file_uploads          20    20
max_input_nesting_level   64    64
max_input_time            -1    -1
max_input_vars          1000    1000
memory_limit            128M    128M

first Value : Local Value

Second Value: Master Value

many thanks

This sounds like a max-execution-time problem. Try adding set_time_limit(0); to the top of your script, or change the value of max_execution_time in php.ini.