PP Reader
This script reads save files from Pocket Physics, a very cool Nintendo DS Homebrew game.
- It reads the built in thumbnail
- It can create a large preview by reading all elements itself.
You'll need this image:
Download script as zip
Tags:
GD
Pocket Physics
PHP
- It reads the built in thumbnail
- It can create a large preview by reading all elements itself.
You'll need this image:
Download script as zip
Source
- ppreader.class.php
- image.class.php
- <?php
- /**
- * Lucky's Framework
- * A highly extendable MVC PHP framework
- *
- * Created by Lucas van Dijk (http://www.return1.net)
- * Copyright 2007 by Lucas van Dijk
- *
- * $Id$
- *
- * ---------------------------------------------------------------------
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
- include_once 'image.class.php';
- class PPReader
- {
- protected $filename;
- protected $xml;
- protected $thumb;
- protected $screenshot;
- public function __construct($filename)
- {
- {
- throw new InvalidArgumentException('File does not exists');
- }
- $this -> filename = $filename;
- // remove html entities
- $this -> xml = simplexml_load_string($contents);
- if(!$this -> validate())
- {
- throw new Exception('Invalid Pocket Physics file! You need at least Pocket Physics 0.6');
- }
- $this -> author = $this -> xml -> author;
- }
- public function __get($name)
- {
- {
- return $this -> $name;
- }
- }
- public function validate()
- {
- return $this -> xml -> creator && $this -> xml -> world && $this -> xml -> image;
- }
- public function generate_thumb()
- {
- $image_data = $this -> xml -> image;
- $this -> thumb = new Image(64, 48);
- {
- $rgb15col = $n1 + 256 * $n2;
- $col = $this -> thumb -> colorallocate($r, $g, $b);
- $x = $i % 64;
- $this -> thumb -> setpixel($x, $y, $col);
- }
- }
- public function get_thumb()
- {
- return $this -> thumb;
- }
- public function generate_screenshot()
- {
- // Edit the path
- $this -> screenshot = new Image('back.png');
- $red = $this -> screenshot -> allocate_color('FF0000');
- $blue = $this -> screenshot -> allocate_color('0000FF');
- $this -> screenshot -> setthickness(1);
- // Draw the elements
- // Start with lines and rectangles
- foreach($this -> xml -> world -> polygon as $polygon)
- {
- switch((string) $polygon['type'])
- {
- case "solid":
- $color = $red;
- break;
- case "dynamic":
- $color = $blue;
- break;
- }
- $base_x = (int) $polygon['x'] / 3;
- $base_y = (int) $polygon['y'] / 3;
- {
- $this -> screenshot -> linethick($base_x + ((int) $polygon -> vertex[$i]['x'] / 3), $base_y + ((int) $polygon -> vertex[$i]['y'] / 3), $base_x + ((int) $polygon -> vertex[$i + 1]['x'] / 3), $base_y + ((int) $polygon -> vertex[$i + 1]['y'] / 3), $color, 1);
- }
- if((int) $polygon['closed'] == 1)
- {
- $this -> screenshot -> linethick($base_x + ((int) $polygon -> vertex[$i]['x'] / 3), $base_y + ((int) $polygon -> vertex[$i]['y'] / 3), $base_x + ((int) $polygon -> vertex[0]['x'] / 3), $base_y + ((int) $polygon -> vertex[0]['y'] / 3), $color, 1);
- }
- }
- // And now all circles
- foreach($this -> xml -> world -> circle as $circle)
- {
- switch((string) $circle['type'])
- {
- case "solid":
- $color = $red;
- break;
- case "dynamic":
- $color = $blue;
- break;
- }
- $this -> screenshot -> ellipse((int) $circle['x'] / 3, (int) $circle['y'] / 3, (int) $circle['radius'] / 2, (int) $circle['radius'] / 2, $color);
- }
- }
- public function get_screenshot()
- {
- return $this -> screenshot;
- }
- }
- <?php
- /**
- * Lucky's Framework
- * A highly extendable MVC PHP framework
- *
- * Created by Lucas van Dijk (http://www.return1.net)
- * Copyright 2007 by Lucas van Dijk
- *
- * $Id$
- *
- * ---------------------------------------------------------------------
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
- /**
- * Represents an image.
- *
- * @package Images
- * @author Lucas van Dijk
- */
- class Image
- {
- /**
- * Holds the image GD resource
- *
- * @var resource
- */
- protected $image_resource;
- /**
- * Holds the width of the image
- *
- * @var int
- */
- protected $width;
- /**
- * Holds the height of the image
- *
- * @var int
- */
- protected $height;
- /**
- * Constructor, creates the image resource
- * @param mixed $param1 The filename to open, or the width of the image
- * @param int $param2 The height of the image, when creating a new image (not from a file)
- */
- public function __construct($param1, $param2 = null)
- {
- {
- // Just one parameter given,
- // Param1 should be a filename
- $this -> image_resource = $this -> create_image($param1);
- }
- else
- {
- // Param 2 is given
- // So we create a new image with the width and height
- }
- if(!$this -> image_resource)
- {
- throw new Exception('Could not create image', 100);
- }
- $this -> update_size();
- }
- public function __call($name, $args)
- {
- {
- }
- {
- }
- }
- /**
- * Creates an GD image resource based on image type
- * @param string $file The file to open
- * @return resource
- */
- protected function create_image($file)
- {
- $img = false;
- switch($image_type)
- {
- case 3:
- $img = imagecreatefrompng($file);
- break;
- case 2:
- $img = imagecreatefromjpeg($file);
- break;
- case 1:
- $img = imagecreatefromgif($file);
- break;
- case 6:
- $img = imagecreatefromwbmp($file);
- break;
- default:
- throw new Exception('File is not a valid Image', 101);
- }
- return $img;
- }
- /**
- * Allocates a color, based on a hex color
- * @param string $color The hex color to allocate
- * @param int The background color
- */
- public function allocate_color($color)
- {
- {
- }
- $parts = str_split($color, 2);
- return eval('return imagecolorallocate($this -> image_resource, 0x'.$parts[0].', 0x'.$parts[1].', 0x'.$parts[2].');');
- }
- /**
- * Generates a random hex color
- * @param bool $return_array return an array with three elememts containing each a part of the color, default false
- * @return string|array The random hex color
- */
- protected function random_hex_color($return_array = false)
- {
- $rand_color = '#';
- for($i = 0; $i < 6; $i++)
- {
- }
- if($return_array)
- {
- return $rand_color_array;
- }
- return $rand_color;
- }
- /**
- * Generates a random RGB Color
- * @return array with key 0 as red, key 1 as green, and key 2 as blue
- */
- protected function random_rgb_color()
- {
- for ($c = 0; $c < 3; $c++)
- {
- }
- return $color;
- }
- /**
- * Updates the width and height of the image
- */
- protected function update_size()
- {
- $this -> width = imagesx($this -> image_resource);
- $this -> height = imagesy($this -> image_resource);
- }
- /**
- * Returns the image resource
- * @return resource
- */
- public function get_resource()
- {
- return $this -> image_resource;
- }
- /**
- * Copies an Image object into the current image
- * @param Image $image the image object to copy
- * @param int $x The X position
- * @param int $y The Y psoition
- */
- {
- imagecopy($this -> image_resource, $image -> get_resource(), $x, $y, 0, 0, $image -> get_width() - 1, $image -> get_height() - 1);
- }
- /**
- * Resizes the image
- * @param int $max_width max width of the image
- * @param int $max_height max height of the image
- * @param bool $keep_aspect_ratio True if you wnat to keep the aspect ratio
- */
- public function resize($max_width, $max_height, $keep_aspect_ratio = true)
- {
- if($keep_aspect_ratio)
- {
- // Find resize scale
- $new_width = $resize_scale * $this -> width;
- $new_height = $resize_scale * $this -> height;
- }
- else
- {
- $new_width = $max_width;
- $new_height = $max_height;
- }
- $new_img = imagecreatetruecolor($new_width, $new_height);
- // Resize it
- imagecopyresampled($new_img, $this -> image_resource, 0, 0, 0, 0, $new_width, $new_height, $this -> width, $this -> height);
- $this -> image_resource = $new_img;
- $this -> update_size();
- }
- public function linethick($x1, $y1, $x2, $y2, $color, $thick = 1)
- {
- if ($thick == 1)
- {
- return imageline($this -> image_resource, $x1, $y1, $x2, $y2, $color);
- }
- $t = $thick / 2 - 0.5;
- if ($x1 == $x2 || $y1 == $y2)
- {
- }
- $k = ($y2 - $y1) / ($x2 - $x1); //y = kx + q
- );
- imagefilledpolygon($this -> image_resource, $points, 4, $color);
- return imagepolygon($this -> image_resource, $points, 4, $color);
- }
- /**
- * Outputs the image to the screen
- *
- * This method outputs the current image to the screen.
- * Note: This function does <b>not</b> send the right Content-Type
- * for the image, you'll have to do that by yourself
- * @param string $type The image type to output
- * @param int $quality The quality of the image (only needed for JPEG images)
- */
- public function show_image($type = "png", $quality = 70)
- {
- switch($type)
- {
- case "png":
- imagepng($this -> image_resource);
- break;
- case "jpg":
- case "jpeg":
- imagejpeg($this -> image_resource, null, $quality);
- break;
- case "gif":
- imagegif($this -> image_resource);
- break;
- default:
- imagepng($this -> image_resource);
- }
- }
- /**
- * Saves the image to a specific path
- * @param string $file The path where to save to
- * @param string $type The image type
- * @param int $quality The quality of the image (only needed for JPEG images)
- */
- public function save_to_file($file, $type = "png", $quality = 70)
- {
- switch($type)
- {
- case "png":
- imagepng($this -> image_resource, $file);
- break;
- case "jpg":
- case "jpeg":
- imagejpeg($this -> image_resource, $file, $quality);
- break;
- case "gif":
- imagegif($this -> image_resource, $file);
- break;
- default:
- imagepng($this -> image_resource, $file);
- }
- }
- /**
- * Returns the width of the image
- *
- * @return int
- */
- public function get_width()
- {
- return $this -> width;
- }
- /**
- * Returns the height of the image
- *
- * @return int
- */
- public function get_height()
- {
- return $this -> height;
- }
- /**
- * Outputs the image
- */
- public function __toString()
- {
- imagepng($this -> image_resource);
- return $contents;
- }
- }

