00001 <?php
00002
00003
00004
00005
00006
00007 class ImagetoolstestController extends ControllerBase {
00008
00009
00010
00011
00012
00013 public function get_routes() {
00014 $ret = array();
00015 if (Config::has_feature(ConfigImageTools::IS_TEST_CONTROLLER_ENABLED)) {
00016 $ret = array(
00017 new ExactMatchRoute('imagetools/test/', $this, 'imagetools_test_index', new NoCacheCacheManager()),
00018 new ParameterizedRoute('imagetools/test/image-{type:e:src,resize,crop,watermark,fit,resize-fit}', $this, 'imagetools_test_image', new NoCacheCacheManager())
00019 );
00020 }
00021 return $ret;
00022 }
00023
00024
00025
00026
00027 public function action_imagetools_test_index(PageData $page_data) {
00028 $err = new Status();
00029 if ($page_data->has_post_data()) {
00030 $err->merge($this->do_upload($page_data));
00031 }
00032
00033 $page_data->status = $err;
00034 $view = ViewFactory::create_view(IViewFactory::CONTENT, 'imagetools/test/index', $page_data);
00035 $view->render();
00036 }
00037
00038
00039
00040
00041 protected function do_upload(PageData $page_data) {
00042 $ret = new Status();
00043 $post_item = $page_data->get_post()->get_item('upload');
00044 $i_err = Arr::get_item($post_item, 'error', UPLOAD_ERR_NO_FILE);
00045 switch ($i_err) {
00046 case UPLOAD_ERR_OK:
00047 $tmp_file = Arr::get_item($post_item, 'tmp_name', '');
00048
00049 $this->apply_tools($tmp_file);
00050 break;
00051 case UPLOAD_ERR_NO_FILE:
00052 $ret->append('No file was uploaded');
00053 break;
00054 case UPLOAD_ERR_INI_SIZE:
00055 case UPLOAD_ERR_FORM_SIZE:
00056 $ret->append('The uploaded file is too big');
00057 break;
00058 default:
00059 $ret->append('An unknown error code was retrieved while uploading the file.');
00060 break;
00061 }
00062 return $ret;
00063 }
00064
00065 protected function apply_tools($tmp_file) {
00066 $tmp_dir = Config::get_value(Config::TEMP_DIR);
00067 Load::components('imagetoolsfactory');
00068 $tools = ImageToolsFactory::create_imagetools();
00069 $src = $tools->create_from_file($tmp_file);
00070 $src->save_to_file($tmp_dir . 'imgtooltest-src');
00071
00072 $resized = $tools->resize($src, 300, 100);
00073 $resized->save_to_file($tmp_dir . 'imgtooltest-resize');
00074
00075 $resized = $tools->resize_fit($src, 300, 100);
00076 $resized->save_to_file($tmp_dir . 'imgtooltest-resize-fit');
00077
00078 $resized = $tools->crop($src, ($src->get_width() - 100) / 2, ($src->get_height() - 100) / 2, 100, 100);
00079 $resized->save_to_file($tmp_dir . 'imgtooltest-crop');
00080
00081 $watermark = $tools->watermark($src);
00082 $watermark->save_to_file($tmp_dir . 'imgtooltest-watermark');
00083
00084 $resized = $tools->fit($src, 300, 100);
00085 $resized->save_to_file($tmp_dir . 'imgtooltest-fit');
00086 }
00087
00088
00089
00090
00091 public function action_imagetools_test_image(PageData $page_data, $type) {
00092 $page_data->page_template = 'emptypage';
00093 $basepath = Config::get_value(Config::TEMP_DIR) . 'imgtooltest-' . $type;
00094 foreach(array('jpg' => 'jpeg', 'png' => 'png', 'gif' => 'gif') as $ext => $mime) {
00095 $path = $basepath . '.' . $ext;
00096 if (file_exists($path)) {
00097 header('Content-Type: image/'. $mime);
00098 $page_data->content = file_get_contents($path);
00099 }
00100 }
00101 if (empty($page_data->content)) {
00102 return self::NOT_FOUND;
00103 }
00104 }
00105 }