find_images(); if(count($image_names) === 0) { $this->report_error('No images found.'); } $image_file_name = $this->pick_random($image_names); if($this->make_image($image_file_name) === false) { $this->report_error('Error loading '.$this->relative_path.$image_file_name.'.'); } $this->load_superglobals(); $this->resize_image(); $this->draw_text(); $this->finish_up(); } /* Finds all allowed images in specified folder. */ private function find_images() { $folder_handle = opendir($this->relative_path); if($folder_handle === false) { $this->report_error('Error opening folder '.$this->relative_path.'.'); } $image_names = array(); while(($current_file = readdir($folder_handle)) !== false) { $format = explode('.', $current_file); $format = strtolower(end($format)); if(in_array($format, $this->allowed_formats)) { array_push($image_names, $current_file); } } return $image_names; } /* Randomly picks one image from the found images. */ private function pick_random($image_names) { $image_count = count($image_names); return $image_names[rand(0, $image_count - 1)]; } /* Reads the _GET superglobals, checks them and stores them into instance variables. */ private function load_superglobals() { if(isset($_GET['nick'])) { if((imagefontwidth($this->font) * strlen($_GET['nick'])) < $this->max_width) { $this->text = $_GET['nick']; } else { $this->report_error('Text is too long!'); } } if(isset($_GET['align'])) { $align = strtolower($_GET['align']); if($align == 'left' or $align == 'center' or $align == 'right') { $this->horizontal_align = $align; } } if(isset($_GET['valign'])) { $align = strtolower($_GET['valign']); if($align == 'top' or $align == 'center' or $align == 'bottom') { $this->vertical_align = $align; } } if(isset($_GET['fontcolor'])) { $fontcolor = $_GET['fontcolor']; if(preg_match('/[a-f0-9]{6}$/i', $fontcolor)) { $this->text_color = array(hexdec(substr($fontcolor, 0, 2)), hexdec(substr($fontcolor, 2, 2)), hexdec(substr($fontcolor, 4, 2))); } else { $this->report_error('Incorrect color '.$_GET['fontcolor'].'. Format: FF0000.'); } } } /* Reads the image into a variable. */ private function make_image($image_file_name) { $format = explode('.', $image_file_name); $format = strtolower(end($format)); switch($format) { case 'jpg': case 'jpeg': if(($this->image = imagecreatefromjpeg($image_file_name)) == false) return false; break; case 'gif': if(($this->image = imagecreatefromgif($image_file_name)) == false) return false; break; case 'png': if(($this->image = imagecreatefrompng($image_file_name)) == false) return false; break; } } /* Resizing the resource image, so that it fits within the maximum width and height. */ private function resize_image() { $width = imagesx($this->image); $height = imagesy($this->image); if($width > $this->max_width or $height > $this->max_height) { $max_ratio = $this->max_width/ $this->max_height; $image_ratio = $width / $height; if($image_ratio > $max_ratio) { $width = $this->max_width; $height = $width / $image_ratio; } else { $height = $this->max_height; $width = $height * $image_ratio; } $old_image = $this->image; $this->image = imagecreatetruecolor($width, $height); imagecopyresampled($this->image, $old_image, 0, 0, 0, 0, $width, $height, imagesx($old_image), imagesy($old_image)); } } /* Draws the text on top of the image. */ private function draw_text() { $text_color = imagecolorallocate($this->image, $this->text_color[0], $this->text_color[1], $this->text_color[2]); switch($this->horizontal_align) { case 'left': $x_coordinate = 0.5 * imagefontwidth($this->font); break; case 'right': $x_coordinate = imagesx($this->image) - (strlen($this->text) + 0.5) * imagefontwidth($this->font); break; default: $x_coordinate = 0.5 * imagesx($this->image) - 0.5 * strlen($this->text) * imagefontwidth($this->font); break; } switch($this->vertical_align) { case 'top': $y_coordinate = 2; break; case 'center': $y_coordinate = 0.5 * imagesy($this->image) - 0.5 * imagefontheight($this->font); break; default: $y_coordinate = imagesy($this->image) - imagefontheight($this->font) - 2; break; } imagestring($this->image, $this->font, $x_coordinate, $y_coordinate, $this->text, $text_color); } /* Show the image. */ private function finish_up() { imagepng($this->image); imagedestroy($this->image); } /* Error handling. Prints the error and dies. */ private function report_error($error) { $image = imagecreate(400,200); $background_color = imagecolorallocate($image, 0, 0, 0); $text_color = imagecolorallocate($image, 255, 0, 0); imagestring($image, 4, 5, 5, 'Fatal Error!', $text_color); imagestring($image, 4, 5, 55, $error, $text_color); imagepng($image); imagedestroy($image); die(); } } $image = new random_image; $image->run(); ?>