function upload($uid, $uploadName = false, $FILE, $newWidth = 0, $newHeight = 0) { global $OP; $dots = explode(".", $FILE['name']); $extension = strtolower($dots[count($dots) - 1]); $extensions = array("png", "jpg", "gif", "jpeg"); if ($uploadName === false) { $uploadName = $OP->randStr(5) . "_" . $OP->randStr(5) . "_" . $OP->randStr(5); } /** * Check if User Id is numeric and $FILE array contain more than 0 items */ if (is_numeric($uid) && is_array($FILE) && count($FILE) > 0) { /** * Check if File Extension is supported */ if (array_search($extension, $extensions) !== false) { $path = $FILE['tmp_name']; $resize = new ResizeImage($path); $ratio = $resize->imgw() / $resize->imgh(); $newWidth = ($newWidth == 0 ? $resize->imgw() : $newWidth) * $ratio; $newHeight = ($newHeight == 0 ? $resize->imgh() : $newHeight) * $ratio; /** * We resize to reduce the file size of image */ $resize->resizeTo($newWidth, $newHeight, 'exact'); $resize->saveImage($path, 50); /** * For Saving Database Space, we md5 the upload file name */ $uploadMD5Name = md5($uploadName); $uploadContent = file_get_contents($path); /** * Only do data insertion if the data doesn't exist, else update the already existing value */ $sql = $OP->dbh->prepare("SELECT 1 FROM `data` WHERE `uid`=? AND `name`=?"); $sql->execute(array($uid, $uploadMD5Name)); if ($sql->rowCount() == 0) { $sql = $OP->dbh->prepare("INSERT INTO `data` (`uid`, `name`, `txt`) VALUES (?, ?, ?)"); $sql->execute(array($uid, $uploadMD5Name, $uploadContent)); } else { $sql = $OP->dbh->prepare("UPDATE `data` SET `txt` = ? WHERE `uid`=? AND `name`=?"); $sql->execute(array($uploadContent, $uid, $uploadMD5Name)); } /* We, for fun add a .png extension to the file name */ $uploadName .= ".png"; /* and return the image URL */ return Open::URL("/data/{$uid}/{$uploadName}"); } else { return "extensionNotSupported"; } } else { return false; } }
/* Was it already cached before by the browser ? The old etag will be sent by the browsers as HTTP_IF_NONE_MATCH. We interpret it */ $_SERVER["HTTP_IF_NONE_MATCH"] = isset($_SERVER["HTTP_IF_NONE_MATCH"]) && $_SERVER["HTTP_IF_NONE_MATCH"] != null ? $_SERVER["HTTP_IF_NONE_MATCH"] : 501; if ($_SERVER["HTTP_IF_NONE_MATCH"] == $etag) { /* "Yes, it's the old version and nothing has been changed" - We send this message to the browser */ header("HTTP/1.1 304 Not Modified"); } /* Serve the small image if it's the one that is requested */ if (isset($resizeIMG)) { /* make a temporary file and replace it with the original image */ $temp = tempnam("/tmp", "FOO"); file_put_contents($temp, $fileData); $resize = new ResizeImage($temp); /** * Make the small image resize to small according to the original ratio */ $ratio = $resize->imgw() / $resize->imgh(); $newWidth = $ratio * $fileCrop; $newHeight = $ratio * $fileCrop; if ($newWidth > $resize->imgw() || $newHeight > $resize->imgh()) { $fileData = file_get_contents($temp); } else { /** * Resize & Save */ $resize->resizeTo($newWidth, $newHeight, 'exact'); $resize->saveImage($temp, 80); $fileData = file_get_contents($temp); } } /* Output the file data */ echo $fileData;
$etag = hash("md5", $fileName . $fileCrop); header("ETag: {$etag}"); /* Was it already cached before by the browser ? The old etag will be sent by the browsers as HTTP_IF_NONE_MATCH. We interpret it */ $_SERVER["HTTP_IF_NONE_MATCH"] = isset($_SERVER["HTTP_IF_NONE_MATCH"]) && $_SERVER["HTTP_IF_NONE_MATCH"] != null ? $_SERVER["HTTP_IF_NONE_MATCH"] : 501; if ($_SERVER["HTTP_IF_NONE_MATCH"] == $etag) { /* "Yes, it's the old version and nothing has been changed" - We send this message to the browser */ header("HTTP/1.1 304 Not Modified"); } /* Get the original image file */ $fileData = base64_decode($fileData); /* Serve the small image if it's the one that is requested */ if (isset($resizeIMG)) { /* make a temporary file and replace it with the original image */ $temp = tempnam("/tmp", "FOO"); file_put_contents($temp, $fileData); $resize = new ResizeImage($temp); /* Make the small image resize to small according to the original ratio */ $total = $resize->imgw() + $resize->imgh(); $newWidth = $resize->imgw() / $total * $fileCrop; $newHeight = $resize->imgh() / $total * $fileCrop; if ($newWidth > $resize->imgw() || $newHeight > $resize->imgh()) { $OP->ser(); } /* Resize & Save */ $resize->resizeTo($newWidth, $newHeight, 'exact'); $resize->saveImage($temp, 80); $fileData = file_get_contents($temp); } /* Output the file data */ echo $fileData; }