I have web site in which on front-end exist lot of different images. ~200 x ~50kb each. Now all these images are download on different requests. (~200 requests) my goal is to create one big image and download it using only one request, also I want to store coordinates on my MySQL database about image positions, because it I will use on front-end in canvas. Also I want to re-size images before inserting in big one, because 50kb is to big. Also it will be doing auto after new image is uploaded in specific directory. All images is in one directory. I using laravel PHP framework on back-end.
Asked
Active
Viewed 51 times
-2
-
1Hi you need to provide more information with your question. Also what have you tried? One place to start might be to look at merging two images ( http://stackoverflow.com/questions/3876299/merging-two-images-with-php ) and then go from there. – Mark Davidson Jan 20 '16 at 10:44
1 Answers
1
As a starting point you might be able to make use of this code which scans a directory and adds each image found to a new image. It's rough and would require tweaking but is more or less what you were after.
<?php
$dir=realpath( 'c:/wwwroot/images/tmp/' );/* change to suit your environment */
$col=glob( $dir . '*.*' );/* get all files ( presuming images ) */
$length=count( $col );/* you could use this to generate new image dimensions dynamically */
$sizes=array();
foreach( $col as $file ){
list( $width, $height, $type, $attr ) = getimagesize( $file );
$sizes[ realpath( $file ) ]=array( 'w'=>$width, 'h'=>$height, 't'=>$type );
}
/* create the new image */
$img=imagecreatetruecolor(600,600);
imagecolorallocate( $img, 0,0,0 );
$x = $y = 0;
/* add each image to new image */
foreach( $sizes as $imgpath => $data ){
$x+=$data['w'];
$y+=$data['h'];
$tgt=imagecreatefromjpeg( $imgpath );
imagecopymerge( $img, $tgt, $x, $y, 0, 0, $data['w'], $data['h'], 100 );
imagedestroy( $tgt );
}
header('Content-Type: image/jpeg');
imagejpeg( $img );
imagedestroy($img);
?>

Professor Abronsius
- 33,063
- 5
- 32
- 46