Answer the question
In order to leave comments, you need to log in
How to overlay one image on another in php?
The essence is this:
I need to overlay one image (.jpg) on another (.png).
The output should be .png, since .jpg will be inside.
I do this, the output is .jpg:
<?php
$file_dir = $_POST['file_dir'];
$file_width = $_POST['file_width'];
$file_height = $_POST['file_height'];
$file_posX = $_POST['file_posX'];
$file_posY = $_POST['file_posY'];
$file_bckg = $_POST['file_bckg'];
$path = $_SERVER['DOCUMENT_ROOT'];
$imgBig = '/ajax/dist/square.png';
if($file_bckg == 'circle'):
$imgBig = '/ajax/dist/circle.png';
endif;
$imgSmall = $file_dir;
/* ------------------------ */
list($width, $height) = getimagesize($path.$file_dir);
$thumb = imagecreatetruecolor($file_width, $file_height);
$source = imagecreatefromjpeg($path.$file_dir);
imagecopyresized($thumb, $source, 0, 0, 0, 0, $file_width, $file_height, $width, $height);
$target = '/ajax/resize_img/'.date("d_m_Y_H_i_s")."_".rand(0,1000).".jpg";
imagejpeg($thumb, $path.$target, 100);
$img_resize = $target;
unset($target);
/* ------------------------ */
$img1 = imagecreatefrompng($path . $imgBig);
$img2 = imagecreatefromjpeg($path . $img_resize);
if($img1 and $img2) {
$x2 = imagesx($img2);
$y2 = imagesy($img2);
imagecopyresampled($img1,$img2,$file_posX,$file_posY,0,0,$x2,$y2,$x2,$y2);
$target = '/ajax/save_img/'.date("d_m_Y_H_i_s")."_".rand(0,1000).".jpg";
imagejpeg($img1, $path.$target, 100);
echo $target;
} else {
header('HTTP/1.1 404 Not Found');
}
?>
Answer the question
In order to leave comments, you need to log in
Try the image.intervention.io library for image manipulation, it will be easier and clearer.
It can be assumed that the lost part on the png image had transparency and after merging it got a black background. Check this section of the original in a graphical editor.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question