Ad

Create Big Image From Lot Of Small

- 1 answer

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.

Ad

Answer

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);
?>
Ad
source: stackoverflow.com
Ad