|
Server : Apache/2.4.62 System : FreeBSD fbsdweb2.web.rcn.net 14.1-RELEASE FreeBSD 14.1-RELEASE releng/14.1-n267679-10e31f0946d8 GENERIC amd64 User : www ( 80) PHP Version : 8.3.8 Disable Function : NONE Directory : /domains/owens.enteract/inc/libs/files/ |
Upload File : |
<?php
/*
Example of an autocrop upload handler
//Handle image upload
$imgHandler = new FileHandler();
$imgHandler->uploadTempFolder = "../assets/uploads/_tmp/";
$imgHandler->uploadFolder = "../assets/uploads/images/";
$sqlFile = "";
//Image
if ($imgHandler->uploadImage("img")){
$filename = $imgHandler->filename;
//Check to make sure there was something uploaded
if ($filename != ""){
if ($imgHandler->autoCropImage($imgHandler->uploadTempFolder . $filename,$imgHandler->uploadFolder . $filename,1.65,1.9,576,324)){
$sqlFile .= "img = '".$filename."',img_dimensions = '".$imgHandler->dimensions."',";
$imgHandler->proportionalImageFit($imgHandler->uploadFolder . $filename,$imgHandler->uploadFolder . "t_" . $filename,177,100);
}else{
$uploadErrors = $imgHandler->errorMessage();
}
}
}else{
$uploadErrors = $imgHandler->errorMessage();
}
//Clean up temp file
$imgHandler->cleanup();
*/
class FileHandler
{
public $uploadedFileName;
public $filename;
public $extension;
public $dimensions;
public $width;
public $height;
public $bytes;
public $uploadFolder = "";
public $uploadTempFolder = "";
public $maxBytes = 3145728; //3MB
public $errorCode = "";
public $hasInit = false;
public function init(){
$this->filename = "";
$this->extension = "";
$this->dimensions = "";
$this->width = 0;
$this->height = 0;
$this->bytes = 0;
$this->errorCode = "";
$this->hasInit = true;
}
public function errorMessage(){
if ($this->errorCode == "image_size"){
return "Image (".$this->uploadedFileName.") is too large to process (". round($this->bytes/1048576,2) ."MB). Image must be smaller than " . round($this->maxBytes/1048576,2) . "MB. ";
}else if ($this->errorCode == "image_format"){
return "Image (".$this->uploadedFileName.") must be a GIF, JPG or PNG file format. ";
}else if ($this->errorCode == "image_too_small"){
return "Image (".$this->uploadedFileName.") is smaller than the minimum size allowed. ";
}else if ($this->errorCode == "image_proportions_invalid"){
return "Image (".$this->uploadedFileName.") falls outside of the minimum and maximum proportion range. ";
}
}
// Uploads an image... if further work is to be done a tempname with a prepend of "__" can be used
public function uploadImage($postName){
$this->init();
//Handle image upload
$this->uploadedFileName = $_FILES[$postName]['name'];
$this->filename = $this->cleanFileName($this->uploadedFileName);
//Process if there's a file
if ($this->filename != ""){
$this->extension = self::getFileExtension($this->filename);
//Cleanup the jpeg file extension
if ($this->extension == "jpeg"){$this->extension = "jpg";}
$this->filename = uniqid() . "." . $this->extension;
$this->bytes = $_FILES[$postName]['size'];
if ($this->bytes > $this->maxBytes) { //1MB
$this->errorCode = "image_size";
}else{
if ($this->extension == "png" || $this->extension == "gif" || $this->extension == "jpg" || $this->extension == "jpeg"){
//Move image to correct place
move_uploaded_file($_FILES[$postName]['tmp_name'], $this->uploadTempFolder . $this->filename);
//GET DIMENSIONS OF IMAGE... create a temp name if this will be manipulated further
list($this->width, $this->height) = getimagesize($this->uploadTempFolder . $this->filename);
$this->dimensions = $width . "x" . $height;
}else{
$this->errorCode ="image_format";
}
}
}
if ($this->errorCode != ""){
return false;
}else{
return true;
}
}
//Fits images within a min/max range of proportions to a specific width/height by center cropping
public function autoCropImage($srcImgPath,$destImgPath,$minProportions,$maxProportions,$targetWidth,$targetHeight){
$this->init();
//Get width/height of srcimage
list($this->width, $this->height) = getimagesize($srcImgPath);
//Get ideal proportions (to two decimals)
$idealProportions = round($targetWidth/$targetHeight,2);
$img_proportions = round($this->width/$this->height,2);
if ($this->width < $targetWidth || $this->height < $targetHeight){
$this->errorCode ="image_too_small";
}else if ($this->width == $targetWidth && $this->height == $targetHeight){
//Width/Height are already set... just rename the file
copy($srcImgPath, $destImgPath);
}else if ($img_proportions > $minProportions && $img_proportions < $maxProportions){
//Ideal proportions
if ($img_proportions == $idealProportions){
//Scale it down
$this->cropScaledImage($srcImgPath,$destImgPath,$targetWidth,$targetHeight,$targetWidth,$targetHeight,0,0,false);
}else if ($img_proportions > $idealProportions) {
//Image is too wide... scale down to height and then crop out extra width
$newWidth = round($targetHeight/$this->height * $this->width);
//Center the crop
$xOffset = round(($targetWidth - $newWidth)/2);
//Scale/crop the image
$this->cropScaledImage($srcImgPath,$destImgPath,$targetWidth,$targetHeight,$newWidth,$targetHeight,$xOffset,0,false);
}else if ($img_proportions < $idealProportions) {
//Image is too tall... scale down to width and then crop out extra height
$newHeight = round($targetWidth/$this->width * $this->height);
//Center the crop
$yOffset = round(($targetHeight - $newHeight)/2);
//Scale/crop the image
$this->cropScaledImage($srcImgPath,$destImgPath,$targetWidth,$targetHeight,$targetWidth,$newHeight,0,$yOffset,false);
}
}else{
$this->errorCode = "image_proportions_invalid";
}
if ($this->errorCode != ""){
return false;
}else{
return true;
}
}
//This function allows you to take in an image that has been scaled and moved (x,y) using a frontend tool and generate the proper crop
public function cropScaledImage($srcImgPath,$destImgPath,$destWidth,$destHeight,$scaledWidth,$scaledHeight,$scaledX,$scaledY,$floatingCrop = true){
//Get file extension
$extension = self::getFileExtension($srcImgPath);
//If this is "float" crop where marquee is set inside of scaled image use x,y as they come in
if ($floatingCrop) {
$x = $scaledX;
$y = $scaledY;
}else{
//If the crop is dragging inside a canvas we need to "flip" x,y
$x = -1 * $scaledX;
$y = -1 * $scaledY;
}
//Get width/height of source
list($srcWidth, $srcHeight) = getimagesize($srcImgPath);
//Get src image (jpeg/png/gif)
if ($extension == "jpg" || $extension == "jpeg"){
$src = imagecreatefromjpeg($srcImgPath);
}else if ($extension == "png"){
$src = imagecreatefrompng($srcImgPath);
}else if ($extension == "gif"){
$src = imagecreatefromgif($srcImgPath);
}
//Create the canvas
$scaledImg=imagecreatetruecolor($scaledWidth,$scaledHeight);
if ($extension == "png"){
imagealphablending($scaledImg, true);
$transparent = imagecolorallocatealpha( $scaledImg, 0, 0, 0, 127 );
imagefill( $scaledImg, 0, 0, $transparent );
}
imagecopyresampled($scaledImg,$src,0,0,0,0,$scaledWidth,$scaledHeight,$srcWidth,$srcHeight);
//Make new image
$finalImg=imagecreatetruecolor($destWidth,$destHeight);
if ($extension == "png"){
imagealphablending($finalImg, true);
$transparent = imagecolorallocatealpha( $finalImg, 0, 0, 0, 127 );
imagefill( $finalImg, 0, 0, $transparent );
}
imagecopyresampled($finalImg,$scaledImg,0,0,$x,$y,$scaledWidth,$scaledHeight,$scaledWidth,$scaledHeight);
//Create jpeg/png
if ($extension == "jpg" || $extension == "jpeg"){
imagejpeg($finalImg,$destImgPath,90);
}else if ($extension == "png"){
imagesavealpha($finalImg,true);
imagepng($finalImg,$destImgPath,9);
}else if ($extension == "gif"){
imagegif($finalImg,$destImgPath);
}
imagedestroy($src);
imagedestroy($scaledImg);
imagedestroy($finalImg);
return true;
}
//Fits image into a specific "box" as close as possible (can't exceed maxWidth or maxHeight)
public function proportionalImageFit($srcImgPath,$destImgPath,$maxWidth,$maxHeight){
//Get file extension
$extension = self::getFileExtension($srcImgPath);
//Get width/height of source
list($src_width, $src_height) = getimagesize($srcImgPath);
//Build out fit
if ($src_width <= $maxWidth and $src_height <= $maxHeight){
//Estimate if both Width and Height of the image are smaller then the given values for Width and Height
//if this is true dan keep the original size of the image
$l_new_width=$src_width;
$l_new_height=$src_height;
}else{
if ($src_height > $maxHeight){
//If the value of the height of the image is greater then the given value for height
//then estimate the percentage of the given value for height in relation to
//the original height value of the image
//Use this percentage to calculate the new width value for the image
//in relation to original width value of the image
$l_percentage_height =$maxHeight/($src_height/100);
$l_new_width=($src_width/100)*$l_percentage_height;
$l_new_height=$maxHeight;
if ($l_new_width > $maxWidth){
//After calculation the new width value can stil be greater then the needed width value
//do the same calculation as above but then for the width needed
$l_percentage_width =$maxWidth/($src_width/100);
$l_new_height=($src_height/100)*$l_percentage_width;
$l_new_width=$maxWidth;
}
} else {
//The height value of the image is not greater then the needed height value
//but original width value of the image is
//calculate the new values for width and height
$l_percentage_width =$maxWidth/($src_width/100);
$l_new_height=($src_height/100)*$l_percentage_width;
$l_new_width=$maxWidth;
if ($l_new_height > $maxHeight){
$l_percentage_height =$maxHeight/($src_height/100);
$l_new_width=($src_width/100)*$l_percentage_height;
$l_new_height=$maxHeight;
}
}
}
//Get src image (jpeg/png/gif)
if ($extension == "jpg" || $extension == "jpeg"){
$src = imagecreatefromjpeg($srcImgPath);
}else if ($extension == "png"){
$src = imagecreatefrompng($srcImgPath);
}else if ($extension == "gif"){
$src = imagecreatefromgif($srcImgPath);
}
//Create the canvas
$tmp = imagecreatetruecolor($l_new_width,$l_new_height);
imagecopyresampled($tmp,$src,0,0,0,0,$l_new_width,$l_new_height,$src_width,$src_height);
//Create jpeg/png
if ($extension == "jpg" || $extension == "jpeg"){
imagejpeg($tmp,$destImgPath,90);
}else if ($extension == "png"){
imagepng($tmp,$destImgPath,9);
}else if ($extension == "gif"){
imagegif($tmp,$destImgPath);
}
imagedestroy($src);
imagedestroy($tmp);
return true;
}
//Remove any temp files
public function cleanup(){
if (file_exists($this->uploadTempFolder . $this->filename) && $this->filename != "") {
unlink($this->uploadTempFolder . $this->filename);
}
}
public function cleanFileName($filename){
if ($filename != ""){
$path_info = pathinfo($filename);
$temp_filename = strtolower(str_replace(" ","_",$path_info['filename']));
return ereg_replace("[^a-z0-9._-]","",$temp_filename) . "." . self::getFileExtension($filename);
}
}
/*
==========================================================================================
STATIC METHODS
==========================================================================================
*/
public static function getFileExtension($filename){
$path_info = pathinfo($filename);
$extension = strtolower($path_info['extension']);
if ($extension == "jpeg"){$extension = "jpg";}
return $extension;
}
}
?>