Image Question

Discussion in 'PHP' started by Nerve, Nov 13, 2007.

  1. #1
    How do I put two images side by side in php?

    I know I can use <img src=""> but I want to make two images into one.

    If anyone could help that would be great!
     
    Nerve, Nov 13, 2007 IP
  2. tonybogs

    tonybogs Peon

    Messages:
    462
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #2
    tonybogs, Nov 13, 2007 IP
  3. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #3
    Because I felt like doing it...


    This merges images horizontally. There might be a code on php.net for this (I didn't check), but I felt like writing my own. :)
    
    function imagemergefiles(array $image_sources)
    {
    	$widths = array();
    	$heights = array();
    	
    	// Get image sizes
    	foreach ($image_sources AS $source)
    	{
    		if (!list($widths[], $heights[]) = @getimagesize($source))
    		{
    			trigger_error(__FUNCTION__ . "(): Unable to create image from {$source}", E_USER_WARNING);
    			return false;
    		}
    	}
    	
    	if (!$widths OR !$heights)
    	{
    		trigger_error(__FUNCTION__ . '(): No valid images found', E_USER_WARNING);
    		return false;
    	}
    	
    	// Create blank image with the dimensions we need
    	$img = imagecreatetruecolor(array_sum($widths), max($heights));
    	$posx = 0;
    	
    	// Walk through all images and merge them horizontally
    	foreach ($image_sources AS $key => $image)
    	{
    		if (!$tmp_image = imagecreatefrom_autodetect($image))
    		{
    			trigger_error(__FUNCTION__ . "(): Unable to create image from {$image}", E_USER_WARNING);
    			return false;
    		}
    
    		imagecopymerge($img, $tmp_image, $posx, 0, 0, 0, $widths[$key], $heights[$key], 100);
    		$posx += $widths[$key];
    		unset($tmp_image); // Save some memory
    	}
    	
    	return $img;
    }
    
    
    function imagecreatefrom_autodetect($source)
    {
    	if (($type = strtolower(end(explode('.', $source)))) == 'jpg')
    	{
    		$type = 'jpeg';
    	}
    	
    	if (!function_exists($function = "imagecreatefrom{$type}"))
    	{
    		trigger_error(__FUNCTION__ . "(): Unsupported image type {$type}", E_USER_WARNING);
    		return false;
    	}
    	
    	return $function($source);
    }
    
    
    PHP:
    Usage example:
    
    <?php
    
    $images = array(
        'image1.jpg',
        'image2.jpg',
        'image3.jpg'
    );
    
    $image = imagemergefiles($images);
    
    header('Content-Type: image/jpeg');
    imagejpeg($image);
    
    ?>
    
    PHP:
     
    nico_swd, Nov 13, 2007 IP
  4. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #4
    You're welcome. :rolleyes:
     
    nico_swd, Nov 18, 2007 IP
  5. brendandonhue

    brendandonhue Peon

    Messages:
    45
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    You can get rid of the whole imagecreatefrom_autodetect() thing and just use imagecreatefromstring().
     
    brendandonhue, Nov 18, 2007 IP