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

发布于 2024-10-13 20:04:11 字数 2869 浏览 3 评论 0原文

我编写了一个函数,可以将一些图像复制到画布上并将其保存到文件中。我的代码位于帖子底部。

当我尝试将 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),
                    ),
                );

I have witten a function that copies some images onto a canvas and saves it to a file. My code is at the bottom of the post.

The code works fine when i try to copy 15 image onto the canvas, but when i try to copy 30 it just stops. No errors or exceptions...

I hope one of you can help me out :)

    $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 is an array. Here is an example which will copy 16 images onto the canvas

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

埖埖迣鎅 2024-10-20 20:04:11

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

I'm going to guess that you are using too much RAM with this job. ImageCopyResampled has to write a lot to RAM with this job you have, and images can take up a lot of memory. Check memory_limit in your php.ini file, try increasing it and see if you can get through more/all of your images being written to the canvas. Good luck!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文