how do I copy a lot of images onto a canvas in PHP?
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开发者_StackOverflow社区 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),
),
);
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!
精彩评论