Add watermark images for new upload image

This code will add watermark image when any new images will upload
/*     Images watermark effect     */
add_image_size('gallery-thumb', 130, 80, true);

function filter_wp_generate_attachment_metadata($metadata, $attachment_id) {
global $wpdb;

$thumbfile = $metadata['sizes']['gallery-list']['file'];

$filepath = wp_get_attachment_url($attachment_id);
$fullsize_path = explode('/', get_attached_file($attachment_id));

$dst_img = '';
$dst_img1 = '';
$org_img1 = '';
$last = count($fullsize_path) - 1;
for ($i = 0; $i < count($fullsize_path); $i++) {
if ($i == $last) {
$dst_img .= 'watermark_' . $fullsize_path[$i];
$dst_img1 .= 'watermark_' . $thumbfile;
$org_img1 .= $thumbfile;
} else {
$dst_img .= $fullsize_path[$i] . '/';
$dst_img1 .= $fullsize_path[$i] . '/';
$org_img1 .= $fullsize_path[$i] . '/';
}
}
$org_img = $filepath;

$ext = pathinfo($filepath, PATHINFO_EXTENSION);

$stamp = imagecreatefrompng(get_template_directory_uri() . '/images/watermark.png'); // add image logo to show on all image
if ($ext == 'jpg' || $ext == 'jpeg' || $ext == 'JPG' || $ext == 'JPEG') {
$im = imagecreatefromjpeg($org_img);
$im1 = imagecreatefromjpeg($org_img1);
}
if ($ext == 'png' || $ext == 'PNG') {
$im = imagecreatefrompng($org_img);
$im1 = imagecreatefrompng($org_img1);
}
if ($ext == 'gif' || $ext == 'GIF') {
$im = imagecreatefromgif($org_img);
$im1 = imagecreatefromgif($org_img1);
}
$sx = imagesx($stamp);
$sy = imagesy($stamp);
$ratio = $sx / $sy;

imagecopyresized($im, $stamp, 0, 0, 0, 0, imagesx($im) / 3, imagesx($im) / ($ratio * 3), imagesx($stamp), imagesy($stamp));
imagecopyresized($im1, $stamp, 0, 0, 0, 0, imagesx($im1) / 3, imagesx($im1) / ($ratio * 3), imagesx($stamp), imagesy($stamp));

if ($ext == 'jpg' || $ext == 'jpeg' || $ext == 'JPG' || $ext == 'JPEG') {
imagejpeg($im, $dst_img, 100);
imagejpeg($im1, $dst_img1, 100);
}
if ($ext == 'png' || $ext == 'PNG') {
imagepng($im, $dst_img, 100);
imagepng($im1, $dst_img1, 100);
}
if ($ext == 'gif' || $ext == 'GIF') {
imagegif($im, $dst_img, 100);
imagegif($im1, $dst_img1, 100);
}
imagedestroy($im);
imagedestroy($im1);

if ($ext == 'jpg' || $ext == 'jpeg' || $ext == 'JPG' || $ext == 'JPEG') {
$im = imagecreatefromjpeg($dst_img);
$im1 = imagecreatefromjpeg($dst_img1);
}
if ($ext == 'png' || $ext == 'PNG') {
$im = imagecreatefrompng($dst_img);
$im1 = imagecreatefrompng($dst_img1);
}
if ($ext == 'gif' || $ext == 'GIF') {
$im = imagecreatefromgif($dst_img);
$im1 = imagecreatefromgif($dst_img1);
}

imagecopyresized($im, $stamp, imagesx($im) - (imagesx($im) / 3), 0, 0, 0, imagesx($im) / 3, imagesx($im) / ($ratio * 3), imagesx($stamp), imagesy($stamp));
imagecopyresized($im1, $stamp, imagesx($im1) - (imagesx($im1) / 3), 0, 0, 0, imagesx($im1) / 3, imagesx($im1) / ($ratio * 3), imagesx($stamp), imagesy($stamp));
if ($ext == 'jpg' || $ext == 'jpeg' || $ext == 'JPG' || $ext == 'JPEG') {
imagejpeg($im, $dst_img, 100);
imagejpeg($im1, $dst_img1, 100);
}
if ($ext == 'png' || $ext == 'PNG') {
imagepng($im, $dst_img, 100);
imagepng($im1, $dst_img1, 100);
}
if ($ext == 'gif' || $ext == 'GIF') {
imagegif($im, $dst_img, 100);
imagegif($im1, $dst_img1, 100);
}
imagedestroy($im);
imagedestroy($im1);

if ($ext == 'jpg' || $ext == 'jpeg' || $ext == 'JPG' || $ext == 'JPEG') {
$im = imagecreatefromjpeg($dst_img);
$im1 = imagecreatefromjpeg($dst_img1);
}
if ($ext == 'png' || $ext == 'PNG') {
$im = imagecreatefrompng($dst_img);
$im1 = imagecreatefrompng($dst_img1);
}
if ($ext == 'gif' || $ext == 'GIF') {
$im = imagecreatefromgif($dst_img);
$im1 = imagecreatefromgif($dst_img1);
}

imagecopyresized($im, $stamp, 0, imagesy($im) - (imagesx($im) / ($ratio * 3)), 0, 0, imagesx($im) / 3, imagesx($im) / ($ratio * 3), imagesx($stamp), imagesy($stamp));
imagecopyresized($im1, $stamp, 0, imagesy($im1) - (imagesx($im1) / ($ratio * 3)), 0, 0, imagesx($im1) / 3, imagesx($im1) / ($ratio * 3), imagesx($stamp), imagesy($stamp));
if ($ext == 'jpg' || $ext == 'jpeg' || $ext == 'JPG' || $ext == 'JPEG') {
imagejpeg($im, $dst_img, 100);
imagejpeg($im1, $dst_img1, 100);
}
if ($ext == 'png' || $ext == 'PNG') {
imagepng($im, $dst_img, 100);
imagepng($im1, $dst_img1, 100);
}
if ($ext == 'gif' || $ext == 'GIF') {
imagegif($im, $dst_img, 100);
imagegif($im1, $dst_img1, 100);
}
imagedestroy($im);
imagedestroy($im1);

if ($ext == 'jpg' || $ext == 'jpeg' || $ext == 'JPG' || $ext == 'JPEG') {
$im = imagecreatefromjpeg($dst_img);
$im1 = imagecreatefromjpeg($dst_img1);
}
if ($ext == 'png' || $ext == 'PNG') {
$im = imagecreatefrompng($dst_img);
$im1 = imagecreatefrompng($dst_img1);
}
if ($ext == 'gif' || $ext == 'GIF') {
$im = imagecreatefromgif($dst_img);
$im1 = imagecreatefromgif($dst_img1);
}

imagecopyresized($im, $stamp, imagesx($im) - (imagesx($im) / 3), imagesy($im) - (imagesx($im) / ($ratio * 3)), 0, 0, imagesx($im) / 3, imagesx($im) / ($ratio * 3), imagesx($stamp), imagesy($stamp));
imagecopyresized($im1, $stamp, imagesx($im1) - (imagesx($im1) / 3), imagesy($im1) - (imagesx($im1) / ($ratio * 3)), 0, 0, imagesx($im1) / 3, imagesx($im1) / ($ratio * 3), imagesx($stamp), imagesy($stamp));
if ($ext == 'jpg' || $ext == 'jpeg' || $ext == 'JPG' || $ext == 'JPEG') {
imagejpeg($im, $dst_img, 100);
imagejpeg($im1, $dst_img1, 100);
}
if ($ext == 'png' || $ext == 'PNG') {
imagepng($im, $dst_img, 100);
imagepng($im1, $dst_img1, 100);
}
if ($ext == 'gif' || $ext == 'GIF') {
imagegif($im, $dst_img, 100);
imagegif($im1, $dst_img1, 100);
}
imagedestroy($im);
imagedestroy($im1);

if ($ext == 'jpg' || $ext == 'jpeg' || $ext == 'JPG' || $ext == 'JPEG') {
$im = imagecreatefromjpeg($dst_img);
$im1 = imagecreatefromjpeg($dst_img1);
}
if ($ext == 'png' || $ext == 'PNG') {
$im = imagecreatefrompng($dst_img);
$im1 = imagecreatefrompng($dst_img1);
}
if ($ext == 'gif' || $ext == 'GIF') {
$im = imagecreatefromgif($dst_img);
$im1 = imagecreatefromgif($dst_img1);
}

imagecopyresized($im, $stamp, imagesx($im) / 3, imagesy($im) / 3, 0, 0, imagesx($im) / 3, imagesx($im) / ($ratio * 3), imagesx($stamp), imagesy($stamp));
imagecopyresized($im1, $stamp, imagesx($im1) / 3, imagesy($im1) / 3, 0, 0, imagesx($im1) / 3, imagesx($im1) / ($ratio * 3), imagesx($stamp), imagesy($stamp));
if ($ext == 'jpg' || $ext == 'jpeg' || $ext == 'JPG' || $ext == 'JPEG') {
imagejpeg($im, $dst_img, 100);
imagejpeg($im1, $dst_img1, 100);
}
if ($ext == 'png' || $ext == 'PNG') {
imagepng($im, $dst_img, 100);
imagepng($im1, $dst_img1, 100);
}
if ($ext == 'gif' || $ext == 'GIF') {
imagegif($im, $dst_img, 100);
imagegif($im1, $dst_img1, 100);
}
imagedestroy($im);
imagedestroy($im1);

return $metadata;
}

add_filter('wp_generate_attachment_metadata','filter_wp_generate_attachment_metadata', 10, 2);