Friday, September 11, 2015

How to resize image:

code is here:
index.php
if(isset($_FILES['image']['name']) && $_FILES['image']['name'] !="" )
        {
            $th_arr = array();
            $th_arr[0] = array('width' => '90', 'height' => '90');
            $th_arr[1] = array('width' => '150', 'height' => '150');
           
            $upload_dir = DIR_UPD.'albums/'.$adminUserId.'/';
           
            $ext = strtolower(getExt($_FILES['image']['name']));
                if(in_array($ext, array('jpeg','jpg','png','gif')))
                {
                    $file=GenerateThumbnail($_FILES['image']['name'],$upload_dir,$_FILES['image']['tmp_name'],$th_arr,'',false);
                    if($file)
                    {
                        $objPost->image = $file;
                    }
                }   
        }

function list:

function GenerateThumbnail($varPhoto, $uploadDir, $tmp_name, $th_arr = array(), $file_nm = '', $addExt = true,$crop_coords=array()) {
    //$ext=strtoupper(substr($varPhoto,strlen($varPhoto)-4));die;
    $ext = '.'.strtoupper(getExt($varPhoto));
    $tot_th = count($th_arr);
    if (($ext == ".JPG" || $ext == ".GIF" || $ext == ".PNG" || $ext == ".BMP" || $ext == ".JPEG")) {
        if(!file_exists($uploadDir)){
            mkdir($uploadDir,0777);
        }
           
        if ($file_nm == '')
            $imagename = rand() . time();
        else
            $imagename = $file_nm;
           
        if($addExt||$file_nm=='')
            $imagename = $imagename.$ext;
       
        $pathToImages = $uploadDir . $imagename;
        $Photo_Source = copy($tmp_name, $pathToImages);

        if ($Photo_Source) {
            for ($i = 0; $i < $tot_th; $i++) {
                resizeImage($uploadDir . $imagename, $uploadDir . 'th' . ($i + 1) . '_' . $imagename, $th_arr[$i]['width'], $th_arr[$i]['height'], false,$crop_coords);
            }
            return $imagename;
        } else {
            return false;
        }
    } else {
        return false;
    }
}

function getExt($file) {
    $path_parts = pathinfo($file);
    $ext        = $path_parts['extension'];
    return $ext;
}

function checkImage($imagePath, $imageName='',$defaultImage = 'no_image_available.jpg') {
    if (is_file(DIR_UPD . $imagePath . $imageName)) {
        return SITE_UPD . $imagePath . $imageName;
    } else {
        return SITE_IMG . $defaultImage;
    }
}


function resizeImage($filename, $newfilename = "", $max_width, $max_height = '', $withSampling = true,$crop_coords = array()) {

    if ($newfilename == "")
        $newfilename = $filename;

    $fileExtension = strtolower(getExt($filename));
    if ($fileExtension == "jpg" || $fileExtension == "jpeg") {
        $img = imagecreatefromjpeg($filename);
    } else if ($fileExtension == "png") {
        $img = imagecreatefrompng($filename);
    } else if ($fileExtension == "gif") {
        $img = imagecreatefromgif($filename);
    }
    else
        $img = imagecreatefromjpeg($filename);

    $width = imageSX($img);
    $height = imageSY($img);

    // Build the thumbnail
    $target_width = $max_width;
    $target_height = $max_height;
    $target_ratio = $target_width / $target_height;
    $img_ratio = $width / $height;

    if(empty($crop_coords)){
   
        if ($target_ratio > $img_ratio) {
            $new_height = $target_height;
            $new_width = $img_ratio * $target_height;
        } else {
            $new_height = $target_width / $img_ratio;
            $new_width = $target_width;
        }
   
        if ($new_height > $target_height) {
            $new_height = $target_height;
        }
        if ($new_width > $target_width) {
            $new_height = $target_width;
        }
       
        /*
        $new_img = imagecreatetruecolor($target_width, $target_height);
        $white = imagecolorallocate($new_img, 255, 255, 255);
        imagecolortransparent($new_img);
        @imagefilledrectangle($new_img, 0, 0, $target_width-1, $target_height-1, $white);
        @imagecopyresampled($new_img, $img, ($target_width-$new_width)/2, ($target_height-$new_height)/2, 0, 0, $new_width, $new_height, $width, $height);
        */
        $new_img = imagecreatetruecolor($new_width, $new_height);
        @imagecopyresampled($new_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
    }else{
        $new_img = imagecreatetruecolor($target_width, $target_height);
        $white = imagecolorallocate($new_img, 255, 255, 255);
        @imagefilledrectangle($new_img, 0, 0, $target_width-1, $target_height-1, $white);
        @imagecopyresampled($new_img, $img, 0, 0, $crop_coords['x1'],$crop_coords['y1'],$target_width,$target_height,$crop_coords['x2'],$crop_coords['y2']);
    }

    if ($fileExtension == "jpg" || $fileExtension == "jpeg") {
        $createImageSave = imagejpeg($new_img, $newfilename, 90);
    } else if ($fileExtension == 'png') {
        $createImageSave = imagepng($new_img, $newfilename, 9);
    } else if ($fileExtension == "gif") {
        $createImageSave = imagegif($new_img, $newfilename, 90);
    }
    else
        $createImageSave = imagejpeg($new_img, $newfilename, 90);
}