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');
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);
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 !!
Merge two images and round corner in PHP
By cannibalising a previous answer of mine I've written the following:
<?php
$srcImg = '1.jpg';
$destImg = '2.jpg';
$dstX = 300;
$dstY = 40;
$srcX = 0;
$srcY = 0;
//$srcW = 200; // replaced with $src_width
//$srcH = 226; // replaced with $src_height
$pct = 100;
// create destination image resource.
$dest = imagecreatefromjpeg($destImg);
$dest_width = imagesx($dest);
$dest_height = imagesy($dest);
// create source image resource and define transparent colour.
$src = imagecreatefromjpeg($srcImg);
$src_width = imagesx($src);
$src_height = imagesy($src);
imagecolortransparent($src, imagecolorallocate($src, 255, 0, 255));
// create a circular mask and use it to crop the source image.
$mask = imagecreatetruecolor($src_width, $src_height);
$black = imagecolorallocate($mask, 0, 0, 0);
$magenta = imagecolorallocate($mask, 255, 0, 255);
imagefill($mask, 0, 0, $magenta);
$r = min($src_width, $src_height);
imagefilledellipse($mask, ($src_width / 2), ($src_height / 2), $r, $r, $black);
imagecolortransparent($mask, $black);
imagecopymerge($src, $mask, 0, 0, 0, 0, $src_width, $src_height, 100);
imagedestroy($mask);
// merge the two images to create the result.
imagecopymerge($dest, $src, $dstX, $dstY, $srcX, 0, $src_width, $src_height, $pct);
// output.
header('Content-type: image/jpeg');
imagejpeg($dest);
imagedestroy($src);
imagedestroy($dest);
This correctly crops the source image to a circle and merges it with the destination. You'll need to adjust the positioning to your preference.
How to merge two image in Laravel
use default php functions (imagecopymerge) ( GD library ):
<?php
list($new_width, $new_height, $new_type, $new_attr) = getimagesize("newimage.png");
switch(image_type_to_mime_type($new_type)){
case IMAGETYPE_GIF:
$new = imagecreatefrompng('newimage.png');
break;
case IMAGETYPE_JPEG:
$new = imagecreatefromjpeg('newimage.png');
break;
case IMAGETYPE_PNG:
$new = imagecreatefromgif('newimage.png');
break;
}
$master = imagecreatefrompng('master.png');
imagealphablending($master, false);
imagesavealpha($demasterst, true);
imagecopymerge($master, $new, $box_x, $box_y, 0, 0, $box_w, $box_h, 100);
// imagecopymerge ( resource $dst_im , resource $src_im , int $dst_x , int $dst_y , int $src_x , int $src_y , int $src_w , int $src_h , int $pct )
//save image
imagepng($master, "file.png");
imagedestroy($master);
imagedestroy($new);
for jpeg format use blow function:
- http://php.net/manual/en/function.imagejpeg.php
- http://php.net/manual/en/function.imagecreatefromjpeg.php
If you do not want to store it and just want to send it to the user:
...
...
header('Content-Type: image/jpeg');
// Output the image
imagejpeg($master);
// Free up memory
imagedestroy($master);
imagedestroy($new);
Related Topics
Show Hide Div If, If Statement Is True
Xml Parser Error: Entity Not Defined
What's the Prefix for Binary in PHP
Fetching One Row Only with MySQLi
Post Data and Retrieve the Response Using PHP Curl
MySQL Binary Storage Using Blob VS Os File System: Large Files, Large Quantities, Large Problems
Using the Browser Prompt to Download a File
Replacing .PHP Ext with .HTML Through .Htaccess
How to Execute PHP Code in a Sandbox from Within PHP
Get Key of Multidimensional Array
File_Exists() Returns False, But the File Does Exist
Format Output of $Simplexml->Asxml();