Skip to content

Instantly share code, notes, and snippets.

@jalvarado91
Last active February 4, 2016 01:03
Show Gist options
  • Save jalvarado91/337db5aa36fec0c52233 to your computer and use it in GitHub Desktop.
Save jalvarado91/337db5aa36fec0c52233 to your computer and use it in GitHub Desktop.
<?php
include_once("../config.php");
include_once("resizeimage.php");
error_reporting(E_ALL);
$user_id = $_SESSION['account'][0]->id;
$id = intval($_POST['id']);
$type = $_POST['type']; //listing //condo //community //timeline
$standard_width = 1280;
$ratio = .70;
$store_path = ABSPATH."assets/files/"; //Official Directory for all Images
$store_type_path = $store_path.$type."/"; // Path of the type of Image
$store_type_source_path = $store_type_path.$id."/";// Individual Image Group Path
$store_db_path = $type."/".$id."/";
$allowed_ext = array('jpg', 'jpeg', 'png', 'gif', 'pdf','doc','xls','docx','xlsx','pps','pptx');
// Valida si no existe el directorio con el ID del member en la ruta /gdata/condoimages/
if (is_writable($store_path) && (!file_exists($store_type_path))) {
if (!mkdir($store_type_path, 0777))
die("Unable to create photo upload directory, please check permission in $store_path");
chmod($store_type_path, 0777);
}
// Valida si no existe el directorio del condo la ruta /gdata/condoimages/ + el directorio del member
if (is_writable($store_type_path) && (!file_exists($store_type_source_path))) {
if (!mkdir($store_type_source_path, 0777))
die("Unable to create photo upload directory, please check permision in $store_path");
chmod($store_type_source_path, 0777);
}
function ResizeImage($original,$new_name,$width,$height){
$rimg = new RESIZEIMAGE($original);
$rimg->resize_limitwh($width, $height, $new_name);
$rimg->close();
}
if (strtolower($_SERVER['REQUEST_METHOD']) != 'post') {
exit_status('Error! Wrong HTTP method!');
}
if (array_key_exists('pic', $_FILES) && $_FILES['pic']['error'] == 0) {
$pic = $_FILES['pic'];
$ext = get_extension($pic['name']);
if (!in_array($ext, $allowed_ext)) {
exit_status('Only ' . implode(',', $allowed_ext) . ' files are allowed!');
}
$unique_file_name = sanitizeFilename($pic['name']);
$raw_file_name = substr($unique_file_name, 0, strpos($unique_file_name, "."));
$db_alt_name = ucwords(preg_replace("/-/"," ",substr($pic['name'], 0, strpos($pic['name'], "."))));
$small_unique_file_name = $raw_file_name . "-xs.$ext";
$med_unique_file_name = $raw_file_name . "-md.$ext";
$reg_unique_file_name = $raw_file_name . "-reg.$ext";
$org_unique_file_name = $raw_file_name .".$ext";
$thumb_unique_file_name = $raw_file_name . "-tmb.$ext";
$image = array("name" => $store_db_path.$unique_file_name,
"description" => $db_alt_name,
"user_id" => $user_id,
$type."_id" => $id
);
$file_path = $store_type_source_path."/";
$original_file = $file_path.$org_unique_file_name;
$response = $imagedb->addImage($image);
if (!$response) {
exit_status('something went wrong with your upload!');
} else {
// guardo en db -- entonces mover el archivo.
if (move_uploaded_file($pic['tmp_name'], $original_file)) {
if($ext == 'jpg' || $ext == 'jpeg' || $ext == 'png' || $ext == 'gif'){
ResizeImage($original_file,$file_path.$reg_unique_file_name,$standard_width,floor($standard_width*$ratio));
ResizeImage($original_file,$file_path.$med_unique_file_name,720,floor(720*$ratio));
ResizeImage($original_file,$file_path.$small_unique_file_name,420,floor(420*$ratio));
ResizeImage($original_file,$file_path.$thumb_unique_file_name,150,floor(150*$ratio));
}
MoveFilestoAWS($store_type_source_path);
exit_status('File was uploaded...');
}
}
}
function MoveFilestoAWS($file_path){
global $store_db_path;
$adapter = new \League\Flysystem\Adapter\Local($file_path);
$filesystem = new \League\Flysystem\Filesystem($adapter);
$s3Client = \Aws\S3\S3Client::factory([
'credentials' => [
'key' => AWS_ACCESS_KEY_ID,
'secret' => AWS_SECRET_ACCESS_KEY,
],
'region' => 'us-east-1',
'version' => 'latest'
]);
$s3Adapter = new \League\Flysystem\AwsS3v3\AwsS3Adapter($s3Client, AWS_BUCKET_NAME);
$s3 = new \League\Flysystem\Filesystem($s3Adapter,['visibility' => \League\Flysystem\AdapterInterface::VISIBILITY_PUBLIC]);
$files = $filesystem->listContents();
foreach($files as $f){
try {
if ($s3->has($store_db_path . $f['basename']))
$s3->delete($store_db_path . $f['basename']);
$upload = $s3->write($store_db_path . $f['basename'], $filesystem->read($f['path']));
if ($upload) {
$filesystem->delete($f['path']);
}
}catch (\Exception $e) {
die($e->getMessage());
}
}
rmdir($file_path);
}
exit_status('Something went wrong with your upload!');
// Helper functions
function exit_status ($str)
{
echo json_encode(array('status' => $str));
exit;
}
function get_extension ($file_name){
$ext = explode('.', $file_name);
$ext = array_pop($ext);
return strtolower($ext);
}
function sanitizeFilename($f) {
// a combination of various methods
// we don't want to convert html entities, or do any url encoding
// we want to retain the "essence" of the original file name, if possible
// char replace table found at:
// http://www.php.net/manual/en/function.strtr.php#98669
$replace_chars = array(
'Š'=>'S', 'š'=>'s', 'Ð'=>'Dj','Ž'=>'Z', 'ž'=>'z', 'À'=>'A', 'Á'=>'A', 'Â'=>'A', 'Ã'=>'A', 'Ä'=>'A',
'Å'=>'A', 'Æ'=>'A', 'Ç'=>'C', 'È'=>'E', 'É'=>'E', 'Ê'=>'E', 'Ë'=>'E', 'Ì'=>'I', 'Í'=>'I', 'Î'=>'I',
'Ï'=>'I', 'Ñ'=>'N', 'Ò'=>'O', 'Ó'=>'O', 'Ô'=>'O', 'Õ'=>'O', 'Ö'=>'O', 'Ø'=>'O', 'Ù'=>'U', 'Ú'=>'U',
'Û'=>'U', 'Ü'=>'U', 'Ý'=>'Y', 'Þ'=>'B', 'ß'=>'Ss','à'=>'a', 'á'=>'a', 'â'=>'a', 'ã'=>'a', 'ä'=>'a',
'å'=>'a', 'æ'=>'a', 'ç'=>'c', 'è'=>'e', 'é'=>'e', 'ê'=>'e', 'ë'=>'e', 'ì'=>'i', 'í'=>'i', 'î'=>'i',
'ï'=>'i', 'ð'=>'o', 'ñ'=>'n', 'ò'=>'o', 'ó'=>'o', 'ô'=>'o', 'õ'=>'o', 'ö'=>'o', 'ø'=>'o', 'ù'=>'u',
'ú'=>'u', 'û'=>'u', 'ý'=>'y', 'ý'=>'y', 'þ'=>'b', 'ÿ'=>'y', 'ƒ'=>'f'
);
$f = strtr($f, $replace_chars);
// convert & to "and", @ to "at", and # to "number"
$f = preg_replace(array('/[\&]/', '/[\@]/', '/[\#]/'), array('-and-', '-at-', '-number-'), $f);
$f = preg_replace('/[^(\x20-\x7F)]*/','', $f); // removes any special chars we missed
$f = str_replace(' ', '-', $f); // convert space to hyphen
$f = str_replace('\'', '', $f); // removes apostrophes
$f = preg_replace('/[^\w\-\.]+/', '', $f); // remove non-word chars (leaving hyphens and periods)
$f = preg_replace('/[\-]+/', '-', $f); // converts groups of hyphens into one
return strtolower($f);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment