function uploadImage($file, $name, $path) { // check if path exist if (!file_exists($path)) { mkdir($path); } $imageUpload = new ImageUpload($file); $imageUpload->dstPath = $path; $imageUpload->dstName = $name; if ($imageUpload->save()) { return $imageUpload->response; } else { return $imageUpload->response; } }
function addImage($eventId) { //Create folder using BaseClass::createDir($path) function $path = "images/" . $eventId . "/"; //Save image using ImageUpload class //Image name = "cover.jpg" //Update the image path in database. BaseClass::createDir($path); $imageUpload = new ImageUpload($this->image); $imageUpload->dstPath = $path; $imageUpload->dstName = "cover"; if ($imageUpload->save()) { //ImageUpload class by default saves all the image to jpg $imagePath = $path . "cover.jpg"; $sql = "UPDATE Events SET ImagePath = '{$imagePath}' WHERE EventId = {$eventId};"; if ($result = $this->mysqli->query($sql)) { $response = BaseClass::createResponse(1, "Gallery created.."); } else { $response = BaseClass::createResponse(0, $this->mysqli->error); } } else { $response = $imageUpload->response; } //End return $response; }
function saveGalleryImage($galleryId) { //todo-ambuj save image file then the details in GalleryImage Table $resp = array(); $path2 = "albums/" . $galleryId . "/SD/"; //have to save multiple images foreach ($this->images as $img) { //$img is same as $this->coverImage in saveGallery function //while saving the image save it with their id name // Eg: // $imageUpload->dstPath = $galleryPath; // $imageUpload->dstName = $this->generateImageId(); $imgId = $this->generateImageId(); $imageUpload = new ImageUpload($img); $imageUpload->dstPath = $path2; $imageUpload->dstName = $imgId; if ($imageUpload->save()) { $imageSavePath = $path2 . $imgId . ".jpg"; // Get new sizes list($width, $height) = getimagesize($imageSavePath); list($newWidth, $newHeight) = $this->getScaledDimArray($imageSavePath); // Load $thumb = imagecreatetruecolor($newWidth, $newHeight); $source = imagecreatefromjpeg($imageSavePath); // Resize imagecopyresized($thumb, $source, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height); // Output $thumbsFilePath = "albums/" . $galleryId . "/Thumbs/" . $imgId . ".jpg"; imagejpeg($thumb, $thumbsFilePath); $sql = "insert into GalleryImage(Id,GalleryId,ImagePath,ThumbsPath,Caption,ImageWidth,ImageHeight) values({$imgId},{$galleryId},'{$imageSavePath}', '{$thumbsFilePath}', null,'" . $width . "','" . $height . "')"; if ($result = $this->mysqli->query($sql)) { $resp = BaseClass::createResponse(1, "Image saved.."); } else { $resp = BaseClass::createResponse(0, "Image not saved.."); } } } $sql = "UPDATE Gallery SET ImageCount = (SELECT COUNT(*) as 'ImageCount' FROM GalleryImage WHERE GalleryImage.GalleryId = {$galleryId}) WHERE Gallery.Id = {$galleryId}"; $this->mysqli->query($sql) or die($this->mysqli->error); return $resp; }
foreach ($_FILES['fileToUpload'] as $k => $l) { foreach ($l as $i => $v) { if (!array_key_exists($i, $files)) { $files[$i] = array(); } $files[$i][$k] = $v; } } print_r($files); $path = ROOT . "images/newUsers/"; $name = "test"; foreach ($files as $file) { $imageUpload = new ImageUpload($file); $imageUpload->dstPath = $path; $imageUpload->dstName = $name; if ($imageUpload->save()) { print_r($imageUpload->response); } else { print_r($imageUpload->response); } } } ?> <form action="test.php" enctype="multipart/form-data" method="post"> <input type="file" name="fileToUpload[]" id="openFileBtn3" > <input type="submit" value="Submit"> </form> <script> document.getElementById('openFileBtn3').onchange = function (e) { if(e.target.files[0].size > 5242880 ){ return;