+class ImageUploadAndResize{
+ protected $isSubQuery = false;
+ * Image compress and processing
+ public function compressImage($sourceURL, $destinationURL, $minImgWidth, $wmImageSource="", $positionX="", $positionY="", $quality, $newWidth) {
+ $infoImg = getimagesize($sourceURL);
+ if($width<$minImgWidth){
+ echo '<div class="alert alert-danger">Image <strong>WIDTH</strong> is less then '.$minImgWidth.'px</div>';
+ $diff = $width / $newWidth;
+ $newHeight = $height / $diff; // creating new width and height with aspect ratio
+ $watermark = imagecreatefrompng($wmImageSource);
+ $imgResource = imagecreatetruecolor($newWidth, $newHeight);
+ 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;
+ $positionBottom = $positionY;
+ $sx = imagesx($watermark);
+ $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'){
+ $image = imagecreatefrompng($sourceURL);
+ // Set the margins for the watermark and get the height/width of the watermark image
+ $positionRight = $positionX;
+ $positionBottom = $positionY;
+ $sx = imagesx($watermark);
+ $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/gif'){
+ $image = imagecreatefromgif($sourceURL);
+ // Set the margins for the watermark and get the height/width of the watermark image
+ $positionRight = $positionX;
+ $positionBottom = $positionY;
+ $sx = imagesx($watermark);
+ $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);
+ $RET = imagejpeg($imgResource, $destinationURL, $quality);
+ public function createDir($folderName, $permission){
+ if(!file_exists($folderName)) {
+ mkdir($folderName, $permission, true);
+ public function uploadImages($yourFileName, $yourDestination, $minImgWidth=400, $waterMarkImgSrc="", $xPosition="", $yPosition="", $reName="", $permission=0655, $quality=100, $newWidth=""){
+ if(!empty($_FILES[$yourFileName])){
+ foreach($_FILES[$yourFileName]['name'] as $val)
+ $infoExt = getimagesize($_FILES[$yourFileName]['tmp_name'][$this->n]);
+ $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).'/';
+ $fileName = $this->s.$reName.$File_Ext;
+ $fileName = $files[0].$File_Ext;
+ $path = trim($srcPath.$fileName);
+ if(self::compressImage($_FILES[$yourFileName]['tmp_name'][$this->n], $path, $minImgWidth, $waterMarkImgSrc, $xPosition, $yPosition, $quality, $newWidth))
+ $this->prepareNames[] = $fileName; //need to be fixed.
+ $this->Sflag = 1; // success
+ $this->Sflag = 2; // file not move to the destination
+ $this->Sflag = 3; //extention not valid
+ return $this->prepareNames;
+ 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){
+ echo '<div class="alert alert-danger">File extention not good. Try with <em>.PNG, .JPEG, .GIF, .JPG</em></div>';
+ public function uploadMultiFiles($yourFileName, $yourDestination, $reName="", $permission=0655){
+ if(!empty($_FILES[$yourFileName])){
+ foreach($_FILES[$yourFileName]['name'] as $val){
+ $infoExt = getimagesize($_FILES[$yourFileName]['tmp_name'][$this->n]);
+ $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],'.'));
+ $srcPath = self::createDir($yourDestination, $permission).'/';
+ $fileName = $this->s.$reName.$File_Ext;
+ $fileName = $files[0].$File_Ext;
+ $path = trim($srcPath.$fileName);
+ if(move_uploaded_file($_FILES[$yourFileName]['tmp_name'][$this->n], $path)){
+ $this->prepareNames[] = $fileName; //need to be fixed.
+ $this->Sflag = 1; // success
+ $this->Sflag = 2; // file not move to the destination
+ $this->Sflag = 3; //extention not valid
+ return $this->prepareNames;
+ 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){
+ echo '<div class="alert alert-danger">File extention not good. Try with <em>.PNG, .JPEG, .GIF, .JPG</em></div>';