如何在PHP中将大量图像复制到画布上?

时间:2011-01-25 21:41:06

标签: php image gd

我已经找到了一个功能,可以将一些图像复制到画布上并将其保存到文件中。我的代码位于帖子的底部。

当我尝试将15张图像复制到画布上时,代码工作正常,但是当我尝试复制30时,它就停止了。没有错误或例外......

我希望你们中的一个可以帮助我:)

    $img        = imagecreatefromjpeg( $image );
    $imgWidth   = imagesx($img);
    $imgHeight  = imagesy($img);

    // CREATE CANVAS AND FILL WITH WHITE
    $canvas     = imagecreatetruecolor( $guidelines['canvasW'] * $dpi, $guidelines['canvasH'] * $dpi );
    $color      = imagecolorallocate( $canvas, 255, 255, 255 );
    imagefill( $canvas, 0, 0, $color );

    // COPY THE IMAGES ONTO THE CANVAS
    foreach( $guidelines['imageGuide'] as $guide ):

        $bestFit    = bestFit( $imgWidth, $imgHeight, $guide['W'] * $dpi, $guide['H'] * $dpi );
        if( $bestFit['rotate'] ) {
            $output = imagerotate($img, 90, 0);
        } else {
            $output = imagerotate($img, 0, 0);
        }
        imagecopyresampled($canvas, $output, $guide['X'] * $dpi, $guide['Y'] * $dpi, 0, 0, $bestFit['x'], $bestFit['y'], imagesx($output), imagesy($output));

        imagedestroy($output);
    endforeach;

$ guidelines是一个数组。这是一个将16个图像复制到画布上的例子

    $guidelines = array(    'canvasW' => 20,
            'canvasH' => 30,
            'imageGuide' => array(
                        array('W' => 18,    'H' => 13,  'X' => 1,   'Y' => 1.5),

                        array('W' => 3.5,   'H' => 4.5, 'X' => 1.25,    'Y' => 15),
                        array('W' => 3.5,   'H' => 4.5, 'X' => 4.75,    'Y' => 15),
                        array('W' => 3.5,   'H' => 4.5, 'X' => 8.25,    'Y' => 15),
                        array('W' => 3.5,   'H' => 4.5, 'X' => 11.75,   'Y' => 15),
                        array('W' => 3.5,   'H' => 4.5, 'X' => 15.25,   'Y' => 15),

                        array('W' => 3.5,   'H' => 4.5, 'X' => 1.25,    'Y' => 19.5),
                        array('W' => 3.5,   'H' => 4.5, 'X' => 4.75,    'Y' => 19.5),
                        array('W' => 3.5,   'H' => 4.5, 'X' => 8.25,    'Y' => 19.5),
                        array('W' => 3.5,   'H' => 4.5, 'X' => 11.75,   'Y' => 19.5),
                        array('W' => 3.5,   'H' => 4.5, 'X' => 15.25,   'Y' => 19.5),

                        array('W' => 3.5,   'H' => 4.5, 'X' => 1.25,    'Y' => 24),
                        array('W' => 3.5,   'H' => 4.5, 'X' => 4.75,    'Y' => 24),
                        array('W' => 3.5,   'H' => 4.5, 'X' => 8.25,    'Y' => 24),
                        array('W' => 3.5,   'H' => 4.5, 'X' => 11.75,   'Y' => 24),
                        array('W' => 3.5,   'H' => 4.5, 'X' => 15.25,   'Y' => 24),
                    ),
                );

1 个答案:

答案 0 :(得分:0)

我猜你在这份工作中使用了太多内存。 ImageCopyResampled必须通过这项工作为RAM写很多东西,而且图像会占用大量内存。检查php.ini文件中的memory_limit,尝试增加它,看看是否可以通过写入画布的更多/所有图像。祝你好运!