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!
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:
You can get rid of the whole imagecreatefrom_autodetect() thing and just use imagecreatefromstring().