/** * Конвертация изображений с разным расширением в jpeg * * @param string $source * @param string $target_directory * @param integer $quality * @param boolean $remove_source_image * @return boolean * @throws Kohana_Exception */ public static function convert_to_jpeg($source, $target_directory = NULL, $quality = 100, $remove_source_image = FALSE) { if (!file_exists($source)) { return FALSE; } $ext = pathinfo($source, PATHINFO_EXTENSION); $filename = pathinfo($source, PATHINFO_FILENAME); if ($target_directory !== NULL) { if (!is_dir($target_directory) and is_writable($target_directory)) { mkdir($target_directory, 0777); } } else { $target_directory = pathinfo($source, PATHINFO_DIRNAME); } if (!is_writable($target_directory)) { throw new Kohana_Exception('Unable to write to the target directory :resource', array(':resource' => $target_directory)); } if (Valid::regex($ext, '/jpg|jpeg/i')) { $image_tmp = imagecreatefromjpeg($source); } else { if (Valid::regex($ext, '/png/i')) { $image_tmp = imagecreatefrompng($source); } else { if (Valid::regex($ext, '/gif/i')) { $image_tmp = imagecreatefromgif($source); } else { if (Valid::regex($ext, '/bmp/i')) { $image_tmp = imagecreatefrombmp($source); } else { return FALSE; } } } } // quality is a value from 0 (worst) to 100 (best) imagejpeg($image_tmp, $dirname . $filename . '.jpg', $quality); imagedestroy($image_tmp); if ($remove_source_image === TRUE) { unlink($source); } return TRUE; }
/** * Tests Valid::range() * * Tests if a number is within a range. * * @test * @dataProvider provider_regex * @param string Value to test against * @param string Valid pcre regular expression * @param bool Does the value match the expression? */ public function test_regex($value, $regex, $expected) { $this->AssertSame($expected, Valid::regex($value, $regex)); }
/** * Handling of output data set in action methods with $this->rest_output($data). * * @param array|object $data * @param int $code */ protected function rest_output($data = array(), $code = 200) { // Handle an empty and valid response. if (empty($data) && 200 == $code) { $data = array('code' => 404, 'error' => 'No records found'); $code = 404; } if ($this->_suppress_response_codes) { $this->response->status(200); $data['responseCode'] = $code; } else { $this->response->status($code); } $mime = File::mime_by_ext($this->output_format); $format_method = '_format_' . $this->output_format; // If the format method exists, call and return the output in that format if (method_exists($this, $format_method)) { $output_data = $this->{$format_method}($data); $this->response->headers('content-type', File::mime_by_ext($this->output_format)); $this->response->headers('content-length', (string) strlen($output_data)); // Support attachment header if (isset($this->_params['attachment']) && Valid::regex($this->_params['attachment'], '/^[-\\pL\\pN_, ]++$/uD')) { $this->response->headers('content-disposition', 'attachment; filename=' . $this->_params['attachment'] . '.' . $this->output_format); } $this->response->body($output_data); } else { // Report an error. $this->response->status(500); throw new Kohana_Exception('Unknown format method requested'); } }