Skip to content

Instantly share code, notes, and snippets.

@bachors
Last active November 29, 2016 03:00
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save bachors/2b5b3cdd682d932e03c5aba8467cd611 to your computer and use it in GitHub Desktop.
Save bachors/2b5b3cdd682d932e03c5aba8467cd611 to your computer and use it in GitHub Desktop.
Menggabungkan gambar menggunakan php
<?php
// menentukan frame, foto ,padding & file format (jpeg or png)
$frame = 'https://pixabay.com/static/uploads/photo/2016/02/02/01/31/background-1174639_960_720.png';
$foto = 'http://4.bp.blogspot.com/-MnbkNj0P6G4/Viq-PDdxdPI/AAAAAAAAAnw/kpSkfdXs3A0/s1600/yakuza-apocalypse-yayan-ruhian.jpg';
$padding = 150;
$format = 'png';
$frame_info = getImageSize($frame);
$foto_info = getImageSize($foto);
switch ($frame_info['mime']) {
case 'image/jpeg':
$frame_insert = imageCreateFromJPEG($frame);
break;
case 'image/png':
$frame_insert = imageCreateFromPNG($frame);
break;
default:
$frame_insert = '';
$frame_msg = $frame_info['mime'] . ' images are not supported<br />';
break;
}
switch ($foto_info['mime']) {
case 'image/jpeg':
$foto_insert = imageCreateFromJPEG($foto);
break;
case 'image/png':
$foto_insert = imageCreateFromPNG($foto);
break;
default:
$foto_insert = '';
$foto_msg = $foto_info['mime'] . ' images are not supported<br />';
break;
}
if(!empty($frame_insert) && !empty($foto_insert)){
$is = imagesx($foto_insert);
$iy = imagesy($foto_insert);
list($width, $height) = getimagesize($frame);
$foto_dst = imagecreatetruecolor($width, $height);
imagecolortransparent($foto_dst, imagecolorallocate($foto_dst, 0, 0, 0));
imagealphablending($foto_dst, false);
imagesavealpha($foto_dst, true);
imagecopyresized($foto_dst, $foto_insert, 0, 0, 0, 0, $width - ($padding*2), $height - ($padding*2), $is, $iy);
imagecopymerge($frame_insert, $foto_dst, $padding, $padding, 0, 0, $width - ($padding*2), $height - ($padding*2), 100);
// start buffering
ob_start();
// output jpeg or png format & kualitas
if($format == 'jpeg'){
imagejpeg($frame_insert, NULL, 85);
}else if($format == 'png'){
imagepng($frame_insert);
}
// mencetak output menjadi string
$gambar = ob_get_contents();
//selesai mencetak
ob_end_clean();
// membersihkan memory
imagedestroy($frame_insert);
// menampilkan output
header('Content-type: image/'.$format);
echo $gambar;
}else{
echo (empty($frame_insert) ? $frame_msg : $foto_msg);
}
?>
@maulanasatyaadi
Copy link

keren ini fungsinya bekerja..
ini kalo gambar lebih dari 2, misalkan 6 gt bisa ga?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment