upload

mail@pastecode.io avatar
unknown
php
3 years ago
4.7 kB
4
Indexable
<?php
if(isset($_FILES['image'])){
    $errors= array();
    foreach($_FILES['image']['tmp_name'] as $key => $tmp_name ){
        $file_name =$_FILES['image']['name'][$key];
        $file_size =$_FILES['image']['size'][$key];
        $file_tmp =$_FILES['image']['tmp_name'][$key];
        $file_type=$_FILES['image']['type'][$key];      

            // Remove encoding problem
            $file_name = Normalizer::normalize($file_name); 
            setlocale(LC_ALL,'bs_BA.UTF-8');        

            // get file extension
            $fileType = strtolower(pathinfo($file_name, PATHINFO_EXTENSION)); 

            $temp = pathinfo($file_name, PATHINFO_EXTENSION);
            $name = str_replace($temp, '', $file_name);

            // get filename without extension
            $fileNewName = pathinfo($name, PATHINFO_FILENAME);
            $watermarkImagePath = "https://bullerhulen.dk/img/logo.png"; 
            $folderPath = "fototest/";
            $sourceProperties = getimagesize($file_tmp);
            $imageType = $sourceProperties[2];

            // Resize code
            switch ($imageType) {
            case IMAGETYPE_PNG:
                $imageResourceId = imagecreatefrompng($file_tmp); 
                $targetLayer = imageResize($imageResourceId,$sourceProperties[0],$sourceProperties[1]);
                imagepng($targetLayer,$folderPath. $fileNewName. ".jpg");
                break;
            case IMAGETYPE_GIF:
                $imageResourceId = imagecreatefromgif($file_tmp); 
                $targetLayer = imageResize($imageResourceId,$sourceProperties[0],$sourceProperties[1]);
                imagegif($targetLayer,$folderPath. $fileNewName. ".jpg");
                break;
            case IMAGETYPE_JPEG:
                $imageResourceId = imagecreatefromjpeg($file_tmp); 
                $targetLayer = imageResize($imageResourceId,$sourceProperties[0],$sourceProperties[1]);
                imagejpeg($targetLayer,$folderPath. $fileNewName. ".jpg");

                break;
            default:
                echo "Invalid Image type.";
                exit;
                break;
            }               

        // Image Orientation correction                         

        $targetFilePath = $folderPath . $file_name;                 
        $exif = exif_read_data($file_tmp);
        if ($exif['Orientation']==3 OR $exif['Orientation']==6 OR $exif['Orientation']==8) {
            $imageResource = imagecreatefromjpeg($targetFilePath); 
            switch ($exif['Orientation']) { 
            case 3:
            $image = imagerotate($imageResource, 180, 0);
            break;
            case 6:
            $image = imagerotate($imageResource, -90, 0);
            break;
            case 8:
            $image = imagerotate($imageResource, 90, 0);
            break;
        } 
        imagejpeg($image, $targetFilePath);
        imagedestroy($imageResource);
        imagedestroy($image);
        }

        // watermark code

                $watermarkImg = imagecreatefrompng($watermarkImagePath); 

                if(preg_match('/[.](jpg)$/i', $file_name)) {  
                $im = imagecreatefromjpeg($targetFilePath);  
              } else if (preg_match('/[.](jpeg)$/i', $file_name)) {  
                $im = imagecreatefromjpeg($targetFilePath);  
              } else if (preg_match('/[.](png)$/i', $file_name)) {  
                $im = imagecreatefrompng($targetFilePath);  
              }  else if (preg_match('/[.](gif)$/i', $file_name)) {  
                $im = imagecreatefromgif($targetFilePath);  
              }  

                $marge_right = 1; 
                $marge_bottom = 1; 

                $sx = imagesx($watermarkImg); 
                $sy = imagesy($watermarkImg); 

                imagecopy($im, $watermarkImg, imagesx($im) - $sx - $marge_right, imagesy($im) - $sy - $marge_bottom, 0, 0, imagesx($watermarkImg), imagesy($watermarkImg)); 


                imagejpeg($im, $targetFilePath,70); 
                imagedestroy($im);     

    }
     echo ' Successful upload';
}
function imageResize($imageResourceId,$width,$height) {
if($width > $height){
    $targetWidth=250;
    $targetHeight=($height/$width)*$targetWidth;
} else {
    $targetHeight=250;
$targetWidth=($width/$height)*$targetHeight;}
    $targetLayer=imagecreatetruecolor($targetWidth,$targetHeight);
    imagecopyresampled($targetLayer,$imageResourceId,0,0,0,0,$targetWidth,$targetHeight, $width,$height);
    return $targetLayer;
}

?>
<div class="sender">
<form action="/kontakt-os/" method="POST" enctype="multipart/form-data">
    <input type="file" name="image[]" multiple/>
    <input type="submit" value="Send"/>
</form></div>