/** * Process the upload of an image * @return type string response page */ public static function upload() { $error = array('status' => 0, 'msg' => ''); $post = Core\Input::post(); //var_dump($post); //exit; $files = Core\Input::files(); $imageType = $files["fileToUpload"]['type']; $fileName = $files["fileToUpload"]['name']; $imageSize = $files["fileToUpload"]['size']; $target_dir = GROUP_UPLOAD_PATH . '/'; $target_file = $target_dir . $fileName; // the full path we want the image uploaded to $imageFileType = pathinfo($target_file, PATHINFO_EXTENSION); // extension and path info if (isset($post["submit"]) && $error['status'] === 0 && !empty($files["fileToUpload"]["tmp_name"])) { $check = getimagesize($files["fileToUpload"]["tmp_name"]); // image dimensions and other data if ($check !== false) { // File is an image if (file_exists($target_file)) { // File with at target path already exists, try to create a new unique target path $name = explode('.', $fileName); if ($name !== false) { $fileName = $name[0] . '_' . time() . '.' . $imageFileType; $target_file = $target_dir . $fileName; } else { $fileName = microtime() . '.' . $imageFileType; $target_file = $target_dir . $fileName; } } if (!file_exists($target_file)) { // File does not exist on target path, yet // Check file size if ($files["fileToUpload"]["size"] > 10000000) { // 10mb max $error['status'] = 1; $error['msg'] = "Sorry, the file is too large."; } else { // Allow certain file formats if ($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "gif") { $error['status'] = 1; $error['msg'] = "Sorry, only JPG, JPEG, PNG & GIF files are allowed."; } else { if (move_uploaded_file($files["fileToUpload"]["tmp_name"], $target_file)) { // move uploaded file from temporary directory to target path // Get new dimensions list($width_orig, $height_orig) = getimagesize($target_file); $width_lrg = $width_orig; $height_lrg = $height_orig; $target_file_lrg_name = $fileName; $target_file_lrg = $target_dir . $target_file_lrg_name; // Resample uploaded image switch ($imageFileType) { case 'jpeg': $image = imagecreatefromjpeg($target_file); break; case 'jpg': $image = imagecreatefromjpeg($target_file); break; case 'png': $image = imagecreatefrompng($target_file); break; case 'gif': $image = imagecreatefromgif($target_file); break; default: die("Image not supported"); } $name = explode('.', $fileName); $width_med = $width_orig * 0.75; $height_med = $height_orig * 0.75; /* $rgba = imagecolorat($image, 0, 0); $alpha = ($rgba & 0x7F000000) >> 24; */ $image_med = imagecreatetruecolor($width_med, $height_med); // create a black background if ($imageFileType === 'png') { imagesavealpha($image_med, true); $trans_colour = imagecolorallocatealpha($image_med, 0, 0, 0, 127); imagefill($image_med, 0, 0, $trans_colour); } imagecopyresampled($image_med, $image, 0, 0, 0, 0, $width_med, $height_med, $width_orig, $height_orig); // overlay resampled image $size = '_' . round($width_med) . 'x' . round($height_med); $name = isset($name[0]) ? $name[0] : microtime(); $target_file_med_name = $name . $size . '.' . $imageFileType; $target_file_med = $target_dir . $target_file_med_name; $width_sm = $width_orig * 0.5; $height_sm = $height_orig * 0.5; $image_sm = imagecreatetruecolor($width_sm, $height_sm); if ($imageFileType === 'png') { imagesavealpha($image_sm, true); $trans_colour = imagecolorallocatealpha($image_sm, 0, 0, 0, 127); imagefill($image_sm, 0, 0, $trans_colour); } imagecopyresampled($image_sm, $image, 0, 0, 0, 0, $width_sm, $height_sm, $width_orig, $height_orig); $size = '_' . round($width_sm) . 'x' . round($height_sm); $target_file_sm_name = $name . $size . '.' . $imageFileType; $target_file_sm = $target_dir . $target_file_sm_name; // Capture image data ob_start(); switch ($imageFileType) { case 'jpeg': imagejpeg($image_med); break; case 'jpg': imagejpeg($image_med); break; case 'png': imagepng($image_med); break; case 'gif': imagejpeg($image_med); break; default: die("Image not supported"); } $med = ob_get_contents(); ob_end_clean(); ob_start(); switch ($imageFileType) { case 'jpeg': imagejpeg($image_sm); break; case 'jpg': imagejpeg($image_sm); break; case 'png': imagepng($image_sm); break; case 'gif': imagejpeg($image_sm); break; default: die("Image not supported"); } $sm = ob_get_contents(); ob_end_clean(); // Write image data to local files //$bytesWrittenLrg = file_put_contents($target_file_lrg, $lrg); $bytesWrittenMed = file_put_contents($target_file_med, $med); $bytesWrittenSm = file_put_contents($target_file_sm, $sm); // JSON structure to store thumbnail data $thumbnails = array('med' => array('dimensions' => '', 'fileName' => ''), 'sm' => array('dimensions' => '', 'fileName' => '')); $thumbnails['med']['dimensions'] = $bytesWrittenMed > 0 ? $width_med . 'x' . $height_med : ''; $thumbnails['sm']['dimensions'] = $bytesWrittenSm > 0 ? $width_sm . 'x' . $height_sm : ''; $thumbnails['med']['fileName'] = $bytesWrittenMed > 0 ? $target_file_med_name : ''; $thumbnails['sm']['fileName'] = $bytesWrittenSm > 0 ? $target_file_sm_name : ''; $thumbnailsJSON = json_encode($thumbnails); //var_dump($json); //exit; $petName = isset($post['petName']) ? trim($post['petName']) : ''; $description = isset($post['petDescription']) ? trim($post['petDescription']) : ''; $petSpecialNeeds = isset($post['petSpecialNeeds']) ? trim($post['petSpecialNeeds']) : ''; $weight = isset($post['weight']) && $post['weight'] !== '' ? trim($post['weight']) : 'DEFAULT'; $species = isset($post['species']) ? $post['species'] : ''; $breed = isset($post['breed']) ? $post['breed'] : 'DEFAULT'; $age = isset($post['age']) && $post['age'] !== '' ? trim($post['age']) : "DEFAULT"; $sex = isset($post['sex']) && $post['sex'] !== '' ? $post['sex'] : 'none'; if (!isset($_SESSION['user'])) { throw new Exception("User must be logged in to post a pet for adoption"); } $userId = $_SESSION['user']; $adoptionId = ''; $created = time(); $updated = time(); $visibility = 'y'; $approved = 0; $pet = new Pet($petName, $description, $petSpecialNeeds, $weight, $species, $breed, $age, $sex, $userId, $adoptionId, $created, $updated, $visibility, $approved); $petTableRowId = $pet->save(); //exit; if ($petTableRowId === false) { // log errors $msg = Core\Db::getErrorMessage(); //error_log("\n" . date('Y-m-d H:i:s', time()) . ": " . $msg, 3, LOG_PATH . '/mysql_error_log'); throw new Exception($msg); } // Create Image object, which creates a record in the DB of its class members $petId = 'pet:' . $petTableRowId; $newImage = new Image('', '', $fileName, $imageType, $imageSize, $width_orig, $height_orig, $thumbnailsJSON, $petId); $imageTableRowId = $newImage->save(); if ($imageTableRowId === false) { // log errors $msg = Core\Db::getErrorMessage(); //error_log("\n" . date('Y-m-d H:i:s', time()) . ": " . $msg, 3, LOG_PATH . '/mysql_error_log'); throw new Exception($msg); } imagedestroy($image); // free up resources } else { $error['status'] = 1; $error['msg'] = "Sorry, there was an error uploading your file."; } } } } else { $msg = "The file {$fileName} already exists. "; $error['status'] = 1; $error['msg'] = $msg; } } else { $msg = "Failed to calculate image metadata"; $error['status'] = 1; $error['msg'] = $msg; } } if ($error['status'] === 1) { return self::index(array('error' => $error['msg'])); } return self::myPets(); }
/** * Create an array of Images associated with this Pet instance * @throws Exception */ public function loadImages() { $petId = 'pet:' . $this->id; $res = Model\Image::getImagesByPetId($petId); if ($res === false) { // log errors $msg = Core\Db::getErrorMessage(); error_log("\n" . date('Y-m-d H:i:s', time()) . ": " . $msg, 3, LOG_PATH . '/mysql_error_log'); throw new Exception($msg); } $this->images = array(); foreach ($res as $imageRow) { $this->images[] = Image::constructByRow($imageRow); } }