<?php

function default_photo_resize($photo_type, $photo_path, $new_photo_path, $free_up_m) {
    global $session_user_ID;
    $db = DB::getInstance();
    /*     * *********************************************************
      2  - Resize The Image To Fit In Cropping Area
     * ********************************************************* */
    //get the uploaded image size	
    clearstatcache();
    $original_size = getimagesize($photo_path);
    $original_width = $original_size[0];
    $original_height = $original_size[1];
    // Specify The new size
    $main_width = 500; // set the width of the image
    $main_height = $original_height / ($original_width / $main_width); // this sets the height in ratio									
    //create new image using correct php func			
    if ($photo_type == "image/gif") {
        $src2 = imagecreatefromgif($photo_path);
    } elseif ($photo_type == "image/jpeg" || $photo_type == "image/pjpeg") {
        $src2 = imagecreatefromjpeg($photo_path);
    } elseif ($photo_type == "image/png") {
        $src2 = imagecreatefrompng($photo_path);
    } else {
        $errors[] = "There was an error uploading the file. Please upload a .jpg, .gif or .png file. <br />";
    }
    //create the new resized image
    $main = imagecreatetruecolor($main_width, $main_height);
    imagecopyresampled($main, $src2, 0, 0, 0, 0, $main_width, $main_height, $original_width, $original_height);
    //upload new version
    $main_temp = $new_photo_path;
    imagejpeg($main, $main_temp, 95);
    chmod($main_temp, 0777);

    if ($free_up_m == '1') {
        //free up memory
        imagedestroy($src2);
        imagedestroy($main);
        @imagedestroy($photo_path);
        @unlink($photo_path); // delete the original upload	
    }
}

// -------------- RESIZE FUNCTION -------------
// Function for resizing any jpg, gif, or png image files
function ak_img_resize($target, $newcopy, $w, $h, $ext) {
    list($w_orig, $h_orig) = getimagesize($target);
    $scale_ratio = $w_orig / $h_orig;
    if (($w / $h) > $scale_ratio) {
        $w = $h * $scale_ratio;
    } else {
        $h = $w / $scale_ratio;
    }
    $img = "";
    $ext = strtolower($ext);
    if ($ext == "gif") {
        $img = imagecreatefromgif($target);
    } else if ($ext == "png") {
        $img = imagecreatefrompng($target);
    } else {
        $img = imagecreatefromjpeg($target);
    }
    $tci = imagecreatetruecolor($w, $h);
    // imagecopyresampled(dst_img, src_img, dst_x, dst_y, src_x, src_y, dst_w, dst_h, src_w, src_h)
    imagecopyresampled($tci, $img, 0, 0, 0, 0, $w, $h, $w_orig, $h_orig);
    if ($ext == "gif") {
        imagegif($tci, $newcopy);
    } else if ($ext == "png") {
        imagepng($tci, $newcopy);
    } else {
        imagejpeg($tci, $newcopy, 84);
    }
}

// ------------- THUMBNAIL (CROP) FUNCTION -------------
// Function for creating a true thumbnail cropping from any jpg, gif, or png image files
function ak_img_thumb($target, $newcopy, $w, $h, $ext) {
    list($w_orig, $h_orig) = getimagesize($target);
    $src_x = ($w_orig / 2) - ($w / 2);
    $src_y = ($h_orig / 2) - ($h / 2);
    $ext = strtolower($ext);
    $img = "";
    if ($ext == "gif") {
        $img = imagecreatefromgif($target);
    } else if ($ext == "png") {
        $img = imagecreatefrompng($target);
    } else {
        $img = imagecreatefromjpeg($target);
    }
    $tci = imagecreatetruecolor($w, $h);
    imagecopyresampled($tci, $img, 0, 0, $src_x, $src_y, $w, $h, $w, $h);
    if ($ext == "gif") {
        imagegif($tci, $newcopy);
    } else if ($ext == "png") {
        imagepng($tci, $newcopy);
    } else {
        imagejpeg($tci, $newcopy, 84);
    }
}

?>