public function temp_filename_test()
 {
     $filename = system::temp_filename("file", "ext");
     $this->assert_true(file_exists($filename), "File not created");
     unlink($filename);
     $this->assert_pattern($filename, "|/file.*\\.ext\$|");
 }
Esempio n. 2
0
 public function rotate($id, $dir)
 {
     access::verify_csrf();
     $item = model_cache::get("item", $id);
     access::required("view", $item);
     access::required("edit", $item);
     $degrees = 0;
     switch ($dir) {
         case "ccw":
             $degrees = -90;
             break;
         case "cw":
             $degrees = 90;
             break;
     }
     if ($degrees) {
         $tmpfile = system::temp_filename("rotate", pathinfo($item->file_path(), PATHINFO_EXTENSION));
         gallery_graphics::rotate($item->file_path(), $tmpfile, array("degrees" => $degrees), $item);
         $item->set_data_file($tmpfile);
         $item->save();
     }
     if (Input::instance()->get("page_type") == "collection") {
         json::reply(array("src" => $item->thumb_url(), "width" => $item->thumb_width, "height" => $item->thumb_height));
     } else {
         json::reply(array("src" => $item->resize_url(), "width" => $item->resize_width, "height" => $item->resize_height));
     }
 }
 static function item_created($item)
 {
     if ($item->is_photo()) {
         $input_file = $item->file_path();
         $output_file = system::temp_filename("rawphoto-", "jpg");
         $success = rawphoto_graphics::convert($input_file, $output_file);
         if ($success) {
             $item->set_data_file($output_file);
             $item->save();
             unlink($output_file);
         }
     }
 }
Esempio n. 4
0
 /**
  * Get PDF file metadata (height and width)
  * (ref: movie::get_file_metadata())
  */
 static function movie_get_file_metadata($file_path, $metadata)
 {
     if (strtolower(pathinfo($file_path, PATHINFO_EXTENSION)) == "pdf" && ($path = pdf::find_gs())) {
         // Parsing gs output properly can be a pain.  So, let's go for a reliable albeit inefficient
         // approach: re-extract the frame (into tmp) and get its image size.
         $temp_file = system::temp_filename("pdf_", "jpg");
         pdf_event::movie_extract_frame($file_path, $temp_file, null, null);
         list($metadata->height, $metadata->width) = photo::get_file_metadata($temp_file);
     }
 }
Esempio n. 5
0
 /**
  * Overlay an image on top of the input file.
  *
  * Valid options are: file, position, transparency, padding
  *
  * Valid positions: northwest, north, northeast,
  *                  west, center, east,
  *                  southwest, south, southeast
  *
  * padding is in pixels
  *
  * @param string     $input_file
  * @param string     $output_file
  * @param array      $options
  * @param Item_Model $item (optional)
  */
 static function composite($input_file, $output_file, $options, $item = null)
 {
     try {
         graphics::init_toolkit();
         $temp_file = system::temp_filename("composite_", pathinfo($output_file, PATHINFO_EXTENSION));
         module::event("graphics_composite", $input_file, $temp_file, $options, $item);
         if (@filesize($temp_file) > 0) {
             // A graphics_composite event made an image - move it to output_file and use it.
             @rename($temp_file, $output_file);
         } else {
             // No events made an image - proceed with standard process.
             list($width, $height) = photo::get_file_metadata($input_file);
             list($w_width, $w_height) = photo::get_file_metadata($options["file"]);
             $pad = isset($options["padding"]) ? $options["padding"] : 10;
             $top = $pad;
             $left = $pad;
             $y_center = max($height / 2 - $w_height / 2, $pad);
             $x_center = max($width / 2 - $w_width / 2, $pad);
             $bottom = max($height - $w_height - $pad, $pad);
             $right = max($width - $w_width - $pad, $pad);
             switch ($options["position"]) {
                 case "northwest":
                     $x = $left;
                     $y = $top;
                     break;
                 case "north":
                     $x = $x_center;
                     $y = $top;
                     break;
                 case "northeast":
                     $x = $right;
                     $y = $top;
                     break;
                 case "west":
                     $x = $left;
                     $y = $y_center;
                     break;
                 case "center":
                     $x = $x_center;
                     $y = $y_center;
                     break;
                 case "east":
                     $x = $right;
                     $y = $y_center;
                     break;
                 case "southwest":
                     $x = $left;
                     $y = $bottom;
                     break;
                 case "south":
                     $x = $x_center;
                     $y = $bottom;
                     break;
                 case "southeast":
                     $x = $right;
                     $y = $bottom;
                     break;
             }
             Image::factory($input_file)->composite($options["file"], $x, $y, $options["transparency"])->quality(module::get_var("gallery", "image_quality"))->save($output_file);
         }
         module::event("graphics_composite_completed", $input_file, $output_file, $options, $item);
     } catch (ErrorException $e) {
         // Unlike rotate and resize, composite catches its exceptions here.  This is because
         // composite is typically called for watermarks.  If during thumb/resize generation
         // the watermark fails, we'd still like the image resized, just without its watermark.
         // If the exception isn't caught here, graphics::generate will replace it with a
         // placeholder.
         Kohana_Log::add("error", $e->getMessage());
     }
 }