Merging two images with PHP
I got it working from one I made.
<?php
$dest = imagecreatefrompng('vinyl.png');
$src = imagecreatefromjpeg('cover2.jpg');
imagealphablending($dest, false);
imagesavealpha($dest, true);
imagecopymerge($dest, $src, 10, 9, 0, 0, 181, 180, 100); //have to play with these numbers for it to work for you, etc.
header('Content-Type: image/png');
imagepng($dest);
imagedestroy($dest);
imagedestroy($src);
?>
Merge two images in php
The best approach for this situation may be to create a new image in memory with the combined dimensions you desire, then copy or resample the existing images to the new image, and then save the new image to disk.
For example:
function merge($filename_x, $filename_y, $filename_result) {
// Get dimensions for specified images
list($width_x, $height_x) = getimagesize($filename_x);
list($width_y, $height_y) = getimagesize($filename_y);
// Create new image with desired dimensions
$image = imagecreatetruecolor($width_x + $width_y, $height_x);
// Load images and then copy to destination image
$image_x = imagecreatefromjpeg($filename_x);
$image_y = imagecreatefromgif($filename_y);
imagecopy($image, $image_x, 0, 0, 0, 0, $width_x, $height_x);
imagecopy($image, $image_y, $width_x, 0, 0, 0, $width_y, $height_y);
// Save the resulting image to disk (as JPEG)
imagejpeg($image, $filename_result);
// Clean up
imagedestroy($image);
imagedestroy($image_x);
imagedestroy($image_y);
}
Example:
merge('images/myimg.jpg', 'images/second.gif', 'images/merged.jpg');
How to merge multiple images and text into single image?
And i achieved it this way,
Used these 3 images, img1.png,img2.png,img3.png
And create-image.php
file
<?php
createimageinstantly();
//$targetFolder = '/gw/media/uploads/processed/';
//$targetPath = $_SERVER['DOCUMENT_ROOT'] . $targetFolder;
//$img3 = $targetPath.'img3.png';
//print_r(getimagesize('http://www.vapor-rage.com/wp-content/uploads/2014/05/sample.jpg'));
function createimageinstantly($img1='',$img2='',$img3=''){
$x=$y=600;
header('Content-Type: image/png');
$targetFolder = '/gw/media/uploads/processed/';
$targetPath = $_SERVER['DOCUMENT_ROOT'] . $targetFolder;
$img1 = $targetPath.'img1.png';
$img2 = $targetPath.'img2.png';
$img3 = $targetPath.'img3.png';
$outputImage = imagecreatetruecolor(600, 600);
// set background to white
$white = imagecolorallocate($outputImage, 255, 255, 255);
imagefill($outputImage, 0, 0, $white);
$first = imagecreatefrompng($img1);
$second = imagecreatefrompng($img2);
$third = imagecreatefrompng($img3);
//imagecopyresized ( resource $dst_image , resource $src_image , int $dst_x , int $dst_y , int $src_x , int $src_y , int $dst_w , int $dst_h , int $src_w , int $src_h )
imagecopyresized($outputImage,$first,0,0,0,0, $x, $y,$x,$y);
imagecopyresized($outputImage,$second,0,0,0,0, $x, $y,$x,$y);
imagecopyresized($outputImage,$third,200,200,0,0, 100, 100, 204, 148);
// Add the text
//imagettftext ( resource $image , float $size , float $angle , int $x , int $y , int $color , string $fontfile , string $text )
//$white = imagecolorallocate($im, 255, 255, 255);
$text = 'School Name Here';
$font = 'OldeEnglish.ttf';
imagettftext($outputImage, 32, 0, 150, 150, $white, $font, $text);
$filename =$targetPath .round(microtime(true)).'.png';
imagepng($outputImage, $filename);
imagedestroy($outputImage);
}
?>
And the result image is
Reference : imagecopyresized & imagettftext
thank you for the suggestions made via comments/answers.
And also i blogged this in detail http://sumankc.com/2016/01/30/merge-multiple-images-and-text-to-create-single-image-php-gd-library/
Good day !!
PHP - Merge two images vertically using imagecopy function
When I consider the function
imagecopy ( resource $dst_im , resource$src_im , int $dst_x , int $dst_y , int $src_x , int$src_y , int $src_w , int $src_h )
It says that the dst_x
and dst_y
should be the x-coordinate of destination point and y-coordinate of destination point respectively.
So to merge two images vertically, it should be something like this.
imagecopy($merged_image, $img2, 0,$img1_height , 0, 0, $img2_width, $img2_height);
You should also change the $merged_width
and $merged_height
variable values as needed for the final result. Change the following two lines,
$merged_width = $img1_width + $img2_width; //get highest
$merged_height = $img1_height > $img2_height ? $img1_height : $img2_height;
As follows,
$merged_width = $img1_width > $img2_width ? $img1_width : $img2_width; //get highest width as result image width
$merged_height = $img1_height + $img2_height;
Final Result:
$img1_path = 'images/1.png';
$img2_path = 'images/2.png';
list($img1_width, $img1_height) = getimagesize($img1_path);
list($img2_width, $img2_height) = getimagesize($img2_path);
$merged_width = $img1_width > $img2_width ? $img1_width : $img2_width; //get highest width as result image width
$merged_height = $img1_height + $img2_height;
$merged_image = imagecreatetruecolor($merged_width, $merged_height);
imagealphablending($merged_image, false);
imagesavealpha($merged_image, true);
$img1 = imagecreatefrompng($img1_path);
$img2 = imagecreatefrompng($img2_path);
imagecopy($merged_image, $img1, 0, 0, 0, 0, $img1_width, $img1_height);
//place at right side of $img1
imagecopy($merged_image, $img2, 0,$img1_height , 0, 0, $img2_width, $img2_height);
//save file or output to broswer
$SAVE_AS_FILE = TRUE;
if( $SAVE_AS_FILE ){
$save_path = "images/Sample_Output.png";
imagepng($merged_image,$save_path);
}else{
header('Content-Type: image/png');
imagepng($merged_image);
}
//release memory
imagedestroy($merged_image);
Merging multiple images next to each other in PHP
Put imagepng
outside of loop:
<?php
header('Content-Type: image/png');
$numbers = array(1, 2, 3, 4, 5, 8, 9);
shuffle($numbers);
$newid=array_slice($numbers,0,6);
$count = 0;
$dest=imagecreatetruecolor(35*6,75);
foreach($newid as $imageSrc) {
$image = imagecreatefrompng("numbers/" . $imageSrc . ".png");
imagecopymerge($dest, $image, (35*$count), 0, 0, 0, imagesx($image), imagesy($image), 100);
$count++;
}
imagepng($dest);
?>
Related Topics
Can HTML Be Embedded Inside PHP "If" Statement
How to Loop Through a MySQL Result Set More Than Once Using the MySQL_* Functions
Check If a User/Guest Has Purchased Specific Products in Woocommerce
How to Find Out Where a Function Is Defined
Redefine Class Methods or Class
Converting an Integer to a String in PHP
Convert String to Binary Then Back Again Using PHP
Using Nested Ternary Operators
How to Get Video Duration, Dimension and Size in PHP
PHP Domdocument Failing to Handle Utf-8 Characters (☆)
List All Files in One Directory PHP
Disable Warnings When Loading Non-well-formed HTML by Domdocument (PHP)
Fastest Way to Serve a File Using PHP
Does File_Get_Contents() Have a Timeout Setting
Difference Between a Language Construct and a "Built-In" Function in PHP
Error Message Strict Standards: Non-Static Method Should Not Be Called Statically in PHP