PHP Picture make thumbnail and compress it and upload it to database


PHP Picture make thumbnail and compress it and upload it to database


<?php
if ($_SERVER['REQUEST_METHOD'] == "POST") {
    require("../../config.php");

function compress_image($source_url, $destination_url, $quality)
{
    
    $info = getimagesize($source_url);
    
    if ($info['mime'] == 'image/jpeg')
        $image = imagecreatefromjpeg($source_url);
    
    elseif ($info['mime'] == 'image/gif')
        $image = imagecreatefromgif($source_url);
    elseif ($info['mime'] == 'image/png')
        $image = imagecreatefrompng($source_url);
    
    imagejpeg($image$destination_url$quality);
    return $destination_url;
}

function resize($tmp, $name, $type, $origwidth, $origheight, $newwidth, $newheight, $path)
{
    
    /* calculate new image size with ratio */
    
    $ratio = max($newwidth / $origwidth$newheight / $origheight);
    
    
    
    $h = ceil($newheight / $ratio);
    
    $x = ($origwidth - $newwidth / $ratio) / 2;
    
    $w = ceil($newwidth / $ratio);
    
    
    
    /* new file name */
    
    
    
    
    
    
    
    /* read binary data from image file */
    
    $imgString = file_get_contents($tmp);
    
    
    
    /* create image from string */
    
    $image = imagecreatefromstring($imgString);
    
    $tmp = imagecreatetruecolor($newwidth$newheight);
    
    imagecopyresampled($tmp$image00$x0$newwidth$newheight$w$h);
    
    
    
    /* Save image */
    
    switch ($type) {
        
        case 'image/jpeg':
            imagejpeg($tmp$path100);
            break;
        
        case 'image/png':
            imagepng($tmp$path0);
            break;
        
        case 'image/gif':
            imagegif($tmp$path);
            break;
        
        default:
            
            imagedestroy($image);
            
            imagedestroy($tmp);
            
            return false;
            
    }
    
    /* cleanup memory */
    
    imagedestroy($image);
    
    imagedestroy($tmp);
    
    
    
    return $path;
    
}
   
   
     
include('../crypt.php');
$gettingid = $_REQUEST['id'];
$id = my_simple_crypt($gettingid,'d');   
    $select = "select * from project where id=$id";
    $sql      = mysqli_query($conn$select);
    $result   = mysqli_fetch_array($sql);
    
    date_default_timezone_set('Asia/Kolkata');
   
    $time  = date("H:i:s");
    $date  = date('F d Y');
    $galr  = $_FILES['addgallery']['name'];
    
    
    
    
    if ($_FILES['addgallery']['name'][0] != "") {
        // Thumbail code
        
        
        
        $fieldname = 'addgallery';
        
        $max_file_size = 5000*5000// 200kb
        
        $valid_exts = array(
            'jpeg',
            'jpg',
            'png',
            'gif'
        );
        
        $sizes = (objectarray(
            'width' => 250,
            'height' => 250
        );
        
        $errors = array();
        
        $files = array();
        
        foreach ($_FILES[$fieldname]['name'] as $i => $name) {
            
            try {
                
                
                
                if (!empty($_FILES[$fieldname]['tmp_name'][$i])) {
                    
                    
                    
                    $name = $_FILES[$fieldname]['name'][$i];
                    
                    $size = $_FILES[$fieldname]['size'][$i];
                    
                    $type = $_FILES[$fieldname]['type'][$i];
                    
                    $tmp = $_FILES[$fieldname]['tmp_name'][$i];
                    
                    $error = $_FILES[$fieldname]['error'][$i];
                    
                    
                    
                    $ext = strtolower(pathinfo($name, PATHINFO_EXTENSION));
                    
                    $filesize = filesize($tmp);
                    
                    
                    
                    list($w$h$t$a) = getimagesize($tmp);
                    
                    if (!$w or !$h)
                        throw new Exception(sprintf('Not an image: %s'$name));
                    
                    
                    
                    
                    
                    if ($error == UPLOAD_ERR_OK) {
                        
                        if (is_uploaded_file($tmp)) {
                            
                            
                            
                            if (!in_array($ext$valid_exts))
                                throw new Exception(sprintf('Invalid file extension: %s'$ext));
                            
                            if ($size > $max_file_size)
                                throw new Exception(sprintf('File is too large: %u'$size));
                            
                            
                            
                            // $pathfilespaceremove = pathinfo($name, PATHINFO_FILENAME);
                            $pathfilespaceremove = pathinfo($name, PATHINFO_FILENAME);
                            
                            $pathfilename = str_replace(' ''-'$pathfilespaceremove);
                            
                            $pathfileext = pathinfo($name, PATHINFO_EXTENSION);
                            
                            $rand = rand(10000100000);
                            
                            $dot = '.';
                            
                            $ind = $pathfilename.$rand.$dot.$pathfileext;
                            
                            $pat = '../../images/gallery/thumb/'.$ind;
                            
                            /* resize image */
                            
                            $result = resize($tmp$name$type$w$h$sizes->width$sizes->height$pat);
                            
                            
                            $url = '../../images/gallery/'.$ind;
                            
                            
                            
                            $filename = compress_image($_FILES["addgallery"]["tmp_name"][$i], $url30);
                            
                            
                            
                            $sql2 = "insert into gallery(image,thumb,project_id,status)values('$ind','$ind','$id','1')";
                           
                            
                            
                            if (mysqli_query($conn,$sql2)) {
                                echo 'Gallery Uploaded';
                                
                            }
                            
                            
                            else {
                                echo 'FAiled add gallery';
                                echo '<br />';
                                
                            }
                            
                            
                            
                            
                            if (!$result)
                                throw new Exception(sprintf('error resizing %s'$name));
                            
                            $files[] = $result;
                            
                            
                            
                        } else {
                            
                            throw new Exception('Possible file upload attack');
                            
                        }
                        
                    } else {
                        
                        throw new Exception(uploaderror($error));
                        
                    }
                    
                } else {
                    
                    throw new Exception(sprintf('"tmp_name" for %s is empty - unable to upload correctly'$_FILES[$fieldname]['name'][$i]));
                    
                }
                
            }
            catch (Exception $e) {
                
                $errors[] = $e->getMessage();
                
                continue;
                
            }
            
        }
        
    }
    



}
?>  

Comments