Skip to content

Commit

Permalink
Update az.multi.upload.class.php
Browse files Browse the repository at this point in the history
Also, change the static function msg.
  • Loading branch information
mianzaid authored Aug 19, 2020
1 parent 61c7aec commit 0611470
Showing 1 changed file with 61 additions and 62 deletions.
123 changes: 61 additions & 62 deletions az.multi.upload.class.php
Original file line number Diff line number Diff line change
@@ -1,38 +1,40 @@
<?php
class ImageUploadAndResize{

class ImageUploadAndResize
{

private $newWidth;
private $folderName;
public $n = 0;
public $s = 0;
public $Sflag = 0;
public $prepareNames;
protected $isSubQuery = false;

/**
* Image compress and processing
*/

public function compressImage($sourceURL, $destinationURL, $minImgWidth, $wmImageSource="", $positionX="", $positionY="", $quality, $newWidth) {
* Image compress and processing
*/

public function compressImage($sourceURL, $destinationURL, $minImgWidth, $wmImageSource = "", $positionX = "", $positionY = "", $quality, $newWidth)
{
$infoImg = getimagesize($sourceURL);
$width = $infoImg[0];
$height = $infoImg[1];
if($width<$minImgWidth){
echo '<div class="alert alert-danger">Image <strong>WIDTH</strong> is less then '.$minImgWidth.'px</div>';
if ($width < $minImgWidth) {
echo '<div class="alert alert-danger">Image <strong>WIDTH</strong> is less then ' . $minImgWidth . 'px</div>';
exit;
}
if($newWidth!=""){
if ($newWidth != "") {
$diff = $width / $newWidth;
$newHeight = $height / $diff; // creating new width and height with aspect ratio
}else{
} else {
$newWidth = $width;
$newHeight = $height;
}

$watermark = imagecreatefrompng($wmImageSource);

$imgResource = imagecreatetruecolor($newWidth, $newHeight);
if ($infoImg['mime'] == 'image/jpeg'){
if ($infoImg['mime'] == 'image/jpeg') {
$image = imagecreatefromjpeg($sourceURL);
// Set the margins for the watermark and get the height/width of the watermark image
$positionRight = $positionX;
Expand All @@ -41,9 +43,9 @@ public function compressImage($sourceURL, $destinationURL, $minImgWidth, $wmImag
$sy = imagesy($watermark);
// width to calculate positioning of the watermark.
imagecopy($image, $watermark, imagesx($image) - $sx - $positionRight, imagesy($image) - $sy - $positionBottom, 0, 0, imagesx($watermark), imagesy($watermark));

imagecopyresampled($imgResource, $image, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);
}elseif ($infoImg['mime'] == 'image/jpg'){
} elseif ($infoImg['mime'] == 'image/jpg') {
$image = imagecreatefromjpeg($sourceURL);
// Set the margins for the watermark and get the height/width of the watermark image
$positionRight = $positionX;
Expand All @@ -52,9 +54,9 @@ public function compressImage($sourceURL, $destinationURL, $minImgWidth, $wmImag
$sy = imagesy($watermark);
// width to calculate positioning of the watermark.
imagecopy($image, $watermark, imagesx($image) - $sx - $positionRight, imagesy($image) - $sy - $positionBottom, 0, 0, imagesx($watermark), imagesy($watermark));

imagecopyresampled($imgResource, $image, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);
} elseif ($infoImg['mime'] == 'image/png'){
} elseif ($infoImg['mime'] == 'image/png') {
$image = imagecreatefrompng($sourceURL);
// Set the margins for the watermark and get the height/width of the watermark image
$positionRight = $positionX;
Expand All @@ -63,12 +65,12 @@ public function compressImage($sourceURL, $destinationURL, $minImgWidth, $wmImag
$sy = imagesy($watermark);
// width to calculate positioning of the watermark.
imagecopy($image, $watermark, imagesx($image) - $sx - $positionRight, imagesy($image) - $sy - $positionBottom, 0, 0, imagesx($watermark), imagesy($watermark));

imagealphablending($image, false);
imagesavealpha($image, true);

imagecopyresampled($imgResource, $image, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);
} elseif ($infoImg['mime'] == 'image/gif'){
} elseif ($infoImg['mime'] == 'image/gif') {
$image = imagecreatefromgif($sourceURL);
// Set the margins for the watermark and get the height/width of the watermark image
$positionRight = $positionX;
Expand All @@ -77,77 +79,74 @@ public function compressImage($sourceURL, $destinationURL, $minImgWidth, $wmImag
$sy = imagesy($watermark);
// width to calculate positioning of the watermark.
imagecopy($image, $watermark, imagesx($image) - $sx - $positionRight, imagesy($image) - $sy - $positionBottom, 0, 0, imagesx($watermark), imagesy($watermark));

imagealphablending($image, false);
imagesavealpha($image, true);

imagecopyresampled($imgResource, $image, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);
}

$RET = imagejpeg($imgResource, $destinationURL, $quality);
imagedestroy($image);
return $RET;
}

public function createDir($folderName, $permission){
if(!file_exists($folderName)) {

public function createDir($folderName, $permission)
{
if (!file_exists($folderName)) {
mkdir($folderName, $permission, true);
$fName = $folderName;
}else{
} else {
$fName = $folderName;
}
return $fName;
}


public function uploadFiles($yourFileName, $yourDestination, $createThumb=false, $minImgWidth=400, $waterMarkImgSrc="", $xPosition="", $yPosition="", $reName="", $permission=0655, $quality=100, $newWidth="", $thumbWidth=""){
if(!empty($_FILES[$yourFileName])){
if($createThumb!="" and $createThumb===true){
$srcThumbPath = self::createDir($yourDestination.'/thumb', $permission).'/';


public function uploadFiles($yourFileName, $yourDestination, $createThumb = false, $minImgWidth = 400, $waterMarkImgSrc = "", $xPosition = "", $yPosition = "", $reName = "", $permission = 0655, $quality = 100, $newWidth = "", $thumbWidth = "")
{
if (!empty($_FILES[$yourFileName])) {
if ($createThumb != "" and $createThumb === true) {
$srcThumbPath = $this->createDir($yourDestination . '/thumb', $permission) . '/';
}
foreach($_FILES[$yourFileName]['name'] as $val)
{
foreach ($_FILES[$yourFileName]['name'] as $val) {
$infoExt = getimagesize($_FILES[$yourFileName]['tmp_name'][$this->n]);
$this->s++;
$filesName = str_replace(" ","",trim($_FILES[$yourFileName]['name'][$this->n]));
$files = explode(".",$filesName);
$File_Ext = substr($_FILES[$yourFileName]['name'][$this->n], strrpos($_FILES[$yourFileName]['name'][$this->n],'.'));

if($infoExt['mime'] == 'image/gif' || $infoExt['mime'] == 'image/jpeg' || $infoExt['mime'] == 'image/png')
{
$srcPath = self::createDir($yourDestination, $permission).'/';
if($reName!=""){
$fileName = $this->s.$reName.$File_Ext;
}else{
$fileName = $files[0].$File_Ext;
$filesName = str_replace(" ", "", trim($_FILES[$yourFileName]['name'][$this->n]));
$files = explode(".", $filesName);
$File_Ext = substr($_FILES[$yourFileName]['name'][$this->n], strrpos($_FILES[$yourFileName]['name'][$this->n], '.'));

if ($infoExt['mime'] == 'image/gif' || $infoExt['mime'] == 'image/jpeg' || $infoExt['mime'] == 'image/png') {
$srcPath = $this->createDir($yourDestination, $permission) . '/';
if ($reName != "") {
$fileName = $this->s . $reName . $File_Ext;
} else {
$fileName = $files[0] . $File_Ext;
}
$path = trim($srcPath.$fileName);
$thumbPath = trim($srcThumbPath.$fileName);
if(self::compressImage($_FILES[$yourFileName]['tmp_name'][$this->n], $path, $minImgWidth, $waterMarkImgSrc, $xPosition, $yPosition, $quality, $newWidth)){
self::compressImage($_FILES[$yourFileName]['tmp_name'][$this->n], $thumbPath, $minImgWidth, $waterMarkImgSrc, $xPosition, $yPosition, $quality, $thumbWidth);
$path = trim($srcPath . $fileName);
$thumbPath = trim($srcThumbPath . $fileName);
if ($this->compressImage($_FILES[$yourFileName]['tmp_name'][$this->n], $path, $minImgWidth, $waterMarkImgSrc, $xPosition, $yPosition, $quality, $newWidth)) {

$this->compressImage($_FILES[$yourFileName]['tmp_name'][$this->n], $thumbPath, $minImgWidth, $waterMarkImgSrc, $xPosition, $yPosition, $quality, $thumbWidth);

$this->prepareNames[] = $fileName; //need to be fixed.
$this->Sflag = 1; // success
}else{
} else {
$this->Sflag = 2; // file not move to the destination
}
}
else
{
} else {
$this->Sflag = 3; //extention not valid
}
$this->n++;
}
if($this->Sflag==1){
if ($this->Sflag == 1) {
return $this->prepareNames;
echo '<div class="alert alert-success">Images uploaded successfully!</div>';
}else if($this->Sflag==2){
echo '<div class="alert alert-success">Images uploaded successfully!</div>';
} else if ($this->Sflag == 2) {
echo '<div class="alert alert-danger">File not move to the destination.</div>';
}else if($this->Sflag==3){
} else if ($this->Sflag == 3) {
echo '<div class="alert alert-danger">File extention not good. Try with <em>.PNG, .JPEG, .GIF, .JPG</em></div>';
}
}
}
}
?>

0 comments on commit 0611470

Please sign in to comment.