<?php

function upload($files, $path, $webp=false, $lang='tr')
{
        $f_name = $_FILES[$files]['name'];
        $tmp = $_FILES[$files]['tmp_name'];
        $ext = strtolower(pathinfo($f_name, PATHINFO_EXTENSION));
        $dir = 'images';

            $upload_name = $f_name;
            $filename = $f_name;

            // Eğer aynı isimde bir dosya varsa, dosya adını değiştir
            $i = 1;
            if ($webp) {
                $filename = pathinfo($f_name, PATHINFO_FILENAME) .'.webp';
            }
            while (file_exists(UPLOAD_DIR.'/'.$filename)) {
                $upload_name = pathinfo($f_name, PATHINFO_FILENAME) . '_' . $i . '.' . $ext;
                $filename = pathinfo($f_name, PATHINFO_FILENAME) . '_' . $i . '.' . $ext;
                if ($webp) {
                    $filename = pathinfo($f_name, PATHINFO_FILENAME) . '_' . $i . '.webp';
                }
                $i++;
            }

            if (move_uploaded_file($tmp, UPLOAD_DIR.'/'.$upload_name)) {
                if ($webp) {
                    $upload_name = m_image_webp_convert(UPLOAD_DIR.'/'.$upload_name, $path);
                }
                
                // Watermark ekle (eğer etkinse)
                if (function_exists('add_watermark_to_image')) {
                    $watermarked_path = add_watermark_to_image(UPLOAD_DIR.'/'.$upload_name, $lang);
                    if ($watermarked_path != UPLOAD_DIR.'/'.$upload_name && file_exists($watermarked_path)) {
                        // Watermark eklendiyse, dosya adını güncelle
                        $upload_name = basename($watermarked_path);
                    }
                }
                
                $output[] = $upload_name;
            }

    return $output;
}



function m_image_webp_convert($file, $path)
{
    if(file_exists($file))
    {

        $info = pathinfo($file);
        $image_data = getimagesize($file);
        switch($image_data['mime'])
        {

            case 'image/png':
                $new_file = $info['filename'].'.webp';
                $pngimg = imagecreatefrompng($file);
                $w = imagesx($pngimg);
                $h = imagesy($pngimg);
                $im = imagecreatetruecolor ($w, $h);
                imageAlphaBlending($im, false);
                imageSaveAlpha($im, true);
                $trans = imagecolorallocatealpha($im, 0, 0, 0, 127);
                imagefilledrectangle($im, 0, 0, $w - 1, $h - 1, $trans);
                imagecopy($im, $pngimg, 0, 0, 0, 0, $w, $h);
                imagewebp($im, UPLOAD_DIR.'/'.$new_file,100);
                imagedestroy($im);
                unlink($file);
                return $new_file;
                break;
            case 'image/bmp':
                $new_file = $info['filename'].'.webp';
                $image = imagecreatefrombmp($file);
                imagewebp($image, UPLOAD_DIR.'/'.$new_file,100);
                imagedestroy($image);
                unlink($file);
                return $new_file;
                break;
            case 'image/jpeg':
                $new_file = $info['filename'].'.webp';
                $image = imagecreatefromjpeg($file);
                imagewebp($image, UPLOAD_DIR.'/'.$new_file,100);
                imagedestroy($image);
                unlink($file);
                return $new_file;
                break;
            default:
                return basename($file);

        }


    }
}

function m_image_extension_converter($file)
{

    $replace_types = array('image/gif','image/jpeg','image/x-citrix-jpeg','image/png','image/bmp','image/webp');
    $types['image/gif'] = 'gif';
    $types['image/jpeg'] = 'jpg';
    $types['image/x-citrix-jpeg'] = 'jpg';
    $types['image/png'] = 'png';
    $types['image/bmp'] = 'bmp';
    $types['image/webp'] = 'webp';
    $path = UPLOAD_DIR.'/images/'.$file;
    if($file!='' and file_exists($path))
    {
        $info = pathinfo($path);
        $image_data = getimagesize($path);
        if(in_array($image_data['mime'],$replace_types) and $types[$image_data['mime']]!=$info['extension'])
        {

            copy($path,UPLOAD_DIR.'/images/'.$info['filename'].'.'.$types[$image_data['mime']].'');
            unlink($path);
            return $info['filename'].'.'.$types[$image_data['mime']].'';
        }
        else
        {
            return $file;
        }

    }


}
function m_image_remover($file)
{
		if($file!='')
		{
			$path = UPLOAD_DIR.'/images/'.$file;
			$thumb_path = UPLOAD_DIR.'/images/thumb/'.$file;
			
			// Orijinal dosyayı sil
			if(file_exists($path))
			{
				unlink($path);
			}
			
			// Thumbnail'i sil
			if(file_exists($thumb_path))
			{
				unlink($thumb_path);
			}
			
			// Watermark'lı dosyayı da sil (eğer varsa)
			$watermarked_path = str_replace(['.jpg', '.jpeg', '.png', '.gif'], '_watermarked.jpg', $path);
			if(file_exists($watermarked_path))
			{
				unlink($watermarked_path);
			}
			
			// WebP versiyonunu da sil (eğer varsa)
			$webp_path = str_replace(['.jpg', '.jpeg', '.png', '.gif'], '.webp', $path);
			if(file_exists($webp_path))
			{
				unlink($webp_path);
			}
		}
}

function m_images_remover($list)
{
		preg_match_all('@\[(.*?)\]@si',$list,$files);

		foreach($files[1] as $file)
		{
			if($file!='')
			{
				$path = UPLOAD_DIR.'/images/'.$file;
				$thumb_path = UPLOAD_DIR.'/images/thumb/'.$file;
				
				// Orijinal dosyayı sil
				if(file_exists($path))
				{
					unlink($path);
				}
				
				// Thumbnail'i sil
				if(file_exists($thumb_path))
				{
					unlink($thumb_path);
				}
				
				// Watermark'lı dosyayı da sil (eğer varsa)
				$watermarked_path = str_replace(['.jpg', '.jpeg', '.png', '.gif'], '_watermarked.jpg', $path);
				if(file_exists($watermarked_path))
				{
					unlink($watermarked_path);
				}
				
				// WebP versiyonunu da sil (eğer varsa)
				$webp_path = str_replace(['.jpg', '.jpeg', '.png', '.gif'], '.webp', $path);
				if(file_exists($webp_path))
				{
					unlink($webp_path);
				}
			}
		}
}

function m_video_remover($file)
{
		if($file!='')
		{
			$path = UPLOAD_DIR.'/videos/'.$file;
			if(file_exists($path))
			{
				unlink($path);
			}
		}
}