Use multiple images make one image

Have a question or a suggestion about a 3rd party addon module or plugin?
Let us know here.
Post Reply
andrewvideouk
Forum Members
Forum Members
Posts: 155
Joined: Thu Aug 20, 2020 10:49 pm

Use multiple images make one image

Post 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;
    }     
    ?>

User avatar
DIGI3
Dev Team Member
Dev Team Member
Posts: 1609
Joined: Wed Feb 25, 2009 4:25 am
Location: Victoria, BC

Re: Use multiple images make one image

Post by DIGI3 »

There's lots of javascript options out there, most modern ones will add them to a canvas element.
Not getting the answer you need? CMSMS support options
andrewvideouk
Forum Members
Forum Members
Posts: 155
Joined: Thu Aug 20, 2020 10:49 pm

Re: Use multiple images make one image

Post by andrewvideouk »

javascript would not work. I want to use it for Social media when sharing a website. :)
Post Reply

Return to “Modules/Add-Ons”