Page 1 of 1

Use multiple images make one image

Posted: Sun Sep 05, 2021 12:57 pm
by andrewvideouk
Hi

Has anyone made a module / plugin to Use multiple images make one image in a tiles form.

I been looking at PHP scripts and they seem to be out of date and don't work on the Lastest PHP.

I was hoping to use this script and turn it a plugin. The script dont work anyhow.

Thank you guys.

Code: Select all

    <?php
      $im = mergeImages(array( '1.jpg', '2.jpg', '3.jpg', '4.jpg' ));
    header('Content-type: image/jpg');
     
    imagejpeg($im);

    function mergeImages($images) {
    	$imageData = array();
    	$len = count($images);
    	$wc = ceil(sqrt($len));
    	$hc = floor(sqrt($len/2));
    	$maxW = array();
    	$maxH = array();
    	for($i = 0; $i < $len; $i++) {
    		$imageData[$i] = getimagesize($images[$i]);
    		$found = false;
    		for($j = 0; $j < $i; $j++) {
    			if ( $imageData[$maxW[$j]][0] < $imageData[$i][0] ) {
    				$farr = $j > 0 ? array_slice($maxW, $j-1, $i) : array();
    				$maxW = array_merge($farr, array($i), array_slice($maxW, $j));
    				$found = true;
    				break;
    			}
    		}
    		if ( !$found ) {
    			$maxW[$i] = $i;
    		}
    		$found = false;
    		for($j = 0; $j < $i; $j++) {
    			if ( $imageData[$maxH[$j]][1] < $imageData[$i][1] ) {
    				$farr = $j > 0 ? array_slice($maxH, $j-1, $i) : array();
    				$maxH = array_merge($farr, array($i), array_slice($maxH, $j));
    				$found = true;
    				break;
    			}
    		}
    		if ( !$found ) {
    			$maxH[$i] = $i;
    		}
    	}
     
    	$width = 0;
    	for($i = 0; $i < $wc; $i++) {
    		$width += $imageData[$maxW[$i]][0];
    	}
     
    	$height = 0;
    	for($i = 0; $i < $hc; $i++) {
    		$height += $imageData[$maxH[$i]][1];
    	}
     
    	$im = imagecreatetruecolor($width, $height);
     
    	$wCnt = 0;
    	$startWFrom = 0;
    	$startHFrom = 0;
    	for( $i = 0; $i < $len; $i++ ) {
    		$tmp = imagecreatefromjpeg($images[$i]);
    		imagecopyresampled($im, $tmp, $startWFrom, $startHFrom, 0, 0, $imageData[$i][0], $imageData[$i][1], $imageData[$i][0], $imageData[$i][1]);
    		$wCnt++;
    		if ( $wCnt == $wc ) {
    			$startWFrom = 0;
    			$startHFrom += $imageData[$maxH[0]][1];
    			$wCnt = 0;
    		} else {
    			$startWFrom += $imageData[$i][0];
    		}
    	}
     
     
    	return $im;
    }     
    ?>


Re: Use multiple images make one image

Posted: Sun Sep 05, 2021 2:19 pm
by DIGI3
There's lots of javascript options out there, most modern ones will add them to a canvas element.

Re: Use multiple images make one image

Posted: Sun Sep 05, 2021 5:10 pm
by andrewvideouk
javascript would not work. I want to use it for Social media when sharing a website. :)