/** * Rotate right a image * * @access public */ function RotateRight() { $post = jaws()->request->fetch(array('image', 'fromalbum'), 'post'); //FIXME: Ugly, maybe we need to pass just the image id, also we need to create a //class to manage image actions(resize, rotate) $model = $this->gadget->model->load('Photos'); $image = $model->GetImageEntry($post['image']); if (Jaws_Error::IsError($image)) { Jaws_Header::Location(BASE_SCRIPT . '?gadget=Phoo&action=EditPhoto&image=' . $post['image'] . '&album=' . $post['fromalbum']); } include_once JAWS_PATH . 'include/Jaws/Image.php'; $objImage = Jaws_Image::factory(); if (Jaws_Error::IsError($objImage)) { $GLOBALS['app']->Session->PushLastResponse($objImage->getMessage(), RESPONSE_ERROR); } else { // thumb $objImage->load(JAWS_DATA . 'phoo/' . rawurldecode($image['thumb'])); $objImage->rotate(90); $res = $objImage->save(); $objImage->free(); if (Jaws_Error::IsError($res)) { $GLOBALS['app']->Session->PushLastResponse($res->getMessage(), RESPONSE_ERROR); } else { // medium $objImage->load(JAWS_DATA . 'phoo/' . rawurldecode($image['medium'])); $objImage->rotate(90); $res = $objImage->save(); $objImage->free(); if (Jaws_Error::IsError($res)) { $GLOBALS['app']->Session->PushLastResponse($res->getMessage(), RESPONSE_ERROR); } else { // original image $objImage->load(JAWS_DATA . 'phoo/' . rawurldecode($image['image'])); $objImage->rotate(90); $res = $objImage->save(); $objImage->free(); if (Jaws_Error::IsError($res)) { $GLOBALS['app']->Session->PushLastResponse($res->getMessage(), RESPONSE_ERROR); } else { $GLOBALS['app']->Session->PushLastResponse(_t('GLOBAL_IMAGE_ROTATED_RIGHT'), RESPONSE_NOTICE); } } } } Jaws_Header::Location(BASE_SCRIPT . '?gadget=Phoo&action=EditPhoto&image=' . $post['image'] . '&album=' . $post['fromalbum']); }
/** * Resize an image on the fly * * FIXME: I don't know if is better to get it as a standalone function... * * @returns binary Image resized */ function Thumb() { $image = jaws()->request->fetch('image', 'get'); $objImage = Jaws_Image::factory(); if (!Jaws_Error::IsError($objImage)) { $result = $objImage->load(JAWS_DATA . 'phoo/import/' . $image); if (!Jaws_Error::IsError($result)) { $thumbSize = explode('x', $this->gadget->registry->fetch('thumbsize')); $objImage->resize($thumbSize[0], $thumbSize[1]); $result = $objImage->display(); if (!Jaws_Error::IsError($result)) { return $result; } } } header('Content-type: image/png'); return file_get_contents('gadgets/Phoo/Resources/images/logo.png'); }
/** * Returns avatar as stream data * * @access public * @return bool True on success, false otherwise */ function LoadAvatar() { $file = jaws()->request->fetch('file', 'get'); $objImage = Jaws_Image::factory(); if (!Jaws_Error::IsError($objImage)) { if (!empty($file)) { $file = preg_replace("/[^[:alnum:]_\\.\\-]*/i", "", $file); $result = $objImage->load(Jaws_Utils::upload_tmp_dir() . '/' . $file, true); if (!Jaws_Error::IsError($result)) { $result = $objImage->display(); if (!Jaws_Error::IsError($result)) { return $result; } } } } return false; }
/** * Returns menu image as stream data * * @access public * @return bool True on successful, False otherwise */ function LoadImage() { $params = jaws()->request->fetch(array('id', 'file'), 'get'); $objImage = Jaws_Image::factory(); if (!Jaws_Error::IsError($objImage)) { if (is_null($params['file'])) { $model = $this->gadget->model->load('Menu'); $result = $model->GetMenuImage($params['id']); if (!Jaws_Error::IsError($result)) { $result = $objImage->setData($result, true); } } else { $params['file'] = preg_replace("/[^[:alnum:]_\\.\\-]*/i", "", $params['file']); $result = $objImage->load(Jaws_Utils::upload_tmp_dir() . '/' . $params['file'], true); } if (!Jaws_Error::IsError($result)) { $result = $objImage->display(); if (!Jaws_Error::IsError($result)) { return $result; } } } return false; }
/** * Returns menu image as stream data * * @access public * @return bool True on successful, False otherwise */ function LoadImage() { $id = (int) jaws()->request->fetch('id', 'get'); $model = $this->gadget->model->load('Menu'); $image = $model->GetMenuImage($id); if (!Jaws_Error::IsError($image)) { $objImage = Jaws_Image::factory(); if (!Jaws_Error::IsError($objImage)) { $objImage->setData($image, true); $res = $objImage->display('', null, 315360000); // cached for 10 years! if (!Jaws_Error::IsError($res)) { return $res; } } } return false; }
/** * Add a new entry * * @access public * @param string $user User who is adding the photo * @param array $files info like original name, tmp name and size * @param string $title Title of the image * @param string $description Description of the image * @param bool $fromControlPanel Is it called from ControlPanel? * @param array $album Array containing the required info about the album * @return mixed Returns the ID of the new entry and Jaws_Error on error */ function NewEntry($user, $files, $title, $description, $fromControlPanel = true, $album) { // check if it's really a uploaded file. /*if (is_uploaded_file($files['tmp_name'])) { $GLOBALS['app']->Session->PushLastResponse(_t('PHOO_ERROR_CANT_UPLOAD_PHOTO'), RESPONSE_ERROR); return new Jaws_Error(_t('PHOO_ERROR_CANT_UPLOAD_PHOTO')); }*/ if (!preg_match("/\\.png\$|\\.jpg\$|\\.jpeg\$|\\.gif\$/i", $files['name'])) { $GLOBALS['app']->Session->PushLastResponse(_t('PHOO_ERROR_CANT_UPLOAD_PHOTO_EXT'), RESPONSE_ERROR); return new Jaws_Error(_t('PHOO_ERROR_CANT_UPLOAD_PHOTO_EXT')); } // Create directories $uploaddir = JAWS_DATA . 'phoo/' . date('Y_m_d') . '/'; if (!is_dir($uploaddir)) { if (!Jaws_Utils::is_writable(JAWS_DATA . 'phoo/')) { $GLOBALS['app']->Session->PushLastResponse(_t('PHOO_ERROR_CANT_UPLOAD_PHOTO'), RESPONSE_ERROR); return new Jaws_Error(_t('PHOO_ERROR_CANT_UPLOAD_PHOTO')); } $new_dirs = array(); $new_dirs[] = $uploaddir; $new_dirs[] = $uploaddir . 'thumb'; $new_dirs[] = $uploaddir . 'medium'; foreach ($new_dirs as $new_dir) { if (!Jaws_Utils::mkdir($new_dir)) { $GLOBALS['app']->Session->PushLastResponse(_t('PHOO_ERROR_CANT_UPLOAD_PHOTO'), RESPONSE_ERROR); return new Jaws_Error(_t('PHOO_ERROR_CANT_UPLOAD_PHOTO')); } } } $filename = $files['name']; if (file_exists($uploaddir . $files['name'])) { $filename = time() . '_' . $files['name']; } $res = Jaws_Utils::UploadFiles($files, $uploaddir, 'jpg,gif,png,jpeg', false, !$fromControlPanel); if (Jaws_Error::IsError($res)) { $GLOBALS['app']->Session->PushLastResponse($res->getMessage(), RESPONSE_ERROR); return new Jaws_Error($res->getMessage()); } elseif (empty($res)) { $GLOBALS['app']->Session->PushLastResponse(_t('GLOBAL_ERROR_UPLOAD_4'), RESPONSE_ERROR); return new Jaws_Error(_t('GLOBAL_ERROR_UPLOAD_4')); } $filename = $res[0][0]['host_filename']; $uploadfile = $uploaddir . $filename; // Resize Image include_once JAWS_PATH . 'include/Jaws/Image.php'; $objImage = Jaws_Image::factory(); if (Jaws_Error::IsError($objImage)) { return Jaws_Error::raiseError($objImage->getMessage()); } $thumbSize = explode('x', $this->gadget->registry->fetch('thumbsize')); $mediumSize = explode('x', $this->gadget->registry->fetch('mediumsize')); $objImage->load($uploadfile); $objImage->resize($thumbSize[0], $thumbSize[1]); $res = $objImage->save($this->GetThumbPath($uploadfile)); $objImage->free(); if (Jaws_Error::IsError($res)) { // Return an error if image can't be resized $GLOBALS['app']->Session->PushLastResponse(_t('PHOO_ERROR_CANT_RESIZE_TO_THUMB'), RESPONSE_ERROR); return new Jaws_Error($res->getMessage()); } $objImage->load($uploadfile); $objImage->resize($mediumSize[0], $mediumSize[1]); $res = $objImage->save($this->GetMediumPath($uploadfile)); $objImage->free(); if (Jaws_Error::IsError($res)) { // Return an error if image can't be resized $GLOBALS['app']->Session->PushLastResponse($res->getMessage(), RESPONSE_ERROR); return new Jaws_Error(_t('PHOO_ERROR_CANT_RESIZE_TO_MEDIUM')); } $data = array(); $data['user_id'] = $user; $data['filename'] = date('Y_m_d') . '/' . $filename; $data['title'] = $title; $data['description'] = $description; if ($this->gadget->registry->fetch('allow_comments') === 'true' && $album['allow_comments']) { $data['allow_comments'] = true; } else { $data['allow_comments'] = false; } if ($this->gadget->registry->fetch('published') === 'true' && $this->gadget->GetPermission('ManageAlbums')) { $data['published'] = true; } else { $data['published'] = false; } $jDate = Jaws_Date::getInstance(); $createtime = Jaws_DB::getInstance()->date(); if (function_exists('exif_read_data') && preg_match("/\\.jpg\$|\\.jpeg\$/i", $files['name']) && ($exifData = @exif_read_data($uploadfile, 1, true)) && !empty($exifData['IFD0']['DateTime']) && $jDate->ValidDBDate($exifData['IFD0']['DateTime'])) { $aux = explode(' ', $exifData['IFD0']['DateTime']); $auxdate = str_replace(':', '-', $aux[0]); $auxtime = $aux[1]; $createtime = $auxdate . ' ' . $auxtime; } $data['createtime'] = $createtime; $table = Jaws_ORM::getInstance()->table('phoo_image'); $result = $table->insert($data)->exec(); if (Jaws_Error::IsError($result)) { $GLOBALS['app']->Session->PushLastResponse(_t('PHOO_ERROR_CANT_UPLOAD_PHOTO'), RESPONSE_ERROR); return new Jaws_Error(_t('PHOO_ERROR_CANT_UPLOAD_PHOTO')); } // Lets remove the original if keep_original = false if ($this->gadget->registry->fetch('keep_original') == 'false') { if (!empty($data['filename'])) { Jaws_Utils::delete(JAWS_DATA . 'phoo/' . $data['filename']); } } // shout SiteActivity event $saParams = array(); $saParams['action'] = 'Photo'; $this->gadget->event->shout('SiteActivity', $saParams); $GLOBALS['app']->Session->PushLastResponse(_t('PHOO_PHOTO_ADDED'), RESPONSE_NOTICE); return $result; }