Path: blob/master/src/applications/files/PhabricatorImageTransformer.php
12241 views
<?php12/**3* @task enormous Detecting Enormous Images4* @task save Saving Image Data5*/6final class PhabricatorImageTransformer extends Phobject {789/* -( Saving Image Data )-------------------------------------------------- */101112/**13* Save an image resource to a string representation suitable for storage or14* transmission as an image file.15*16* Optionally, you can specify a preferred MIME type like `"image/png"`.17* Generally, you should specify the MIME type of the original file if you're18* applying file transformations. The MIME type may not be honored if19* Phabricator can not encode images in the given format (based on available20* extensions), but can save images in another format.21*22* @param resource GD image resource.23* @param string? Optionally, preferred mime type.24* @return string Bytes of an image file.25* @task save26*/27public static function saveImageDataInAnyFormat($data, $preferred_mime = '') {28$preferred = null;29switch ($preferred_mime) {30case 'image/gif':31$preferred = self::saveImageDataAsGIF($data);32break;33case 'image/png':34$preferred = self::saveImageDataAsPNG($data);35break;36}3738if ($preferred !== null) {39return $preferred;40}4142$data = self::saveImageDataAsJPG($data);43if ($data !== null) {44return $data;45}4647$data = self::saveImageDataAsPNG($data);48if ($data !== null) {49return $data;50}5152$data = self::saveImageDataAsGIF($data);53if ($data !== null) {54return $data;55}5657throw new Exception(pht('Failed to save image data into any format.'));58}596061/**62* Save an image in PNG format, returning the file data as a string.63*64* @param resource GD image resource.65* @return string|null PNG file as a string, or null on failure.66* @task save67*/68private static function saveImageDataAsPNG($image) {69if (!function_exists('imagepng')) {70return null;71}7273// NOTE: Empirically, the highest compression level (9) seems to take74// up to twice as long as the default compression level (6) but produce75// only slightly smaller files (10% on avatars, 3% on screenshots).7677ob_start();78$result = imagepng($image, null, 6);79$output = ob_get_clean();8081if (!$result) {82return null;83}8485return $output;86}878889/**90* Save an image in GIF format, returning the file data as a string.91*92* @param resource GD image resource.93* @return string|null GIF file as a string, or null on failure.94* @task save95*/96private static function saveImageDataAsGIF($image) {97if (!function_exists('imagegif')) {98return null;99}100101ob_start();102$result = imagegif($image);103$output = ob_get_clean();104105if (!$result) {106return null;107}108109return $output;110}111112113/**114* Save an image in JPG format, returning the file data as a string.115*116* @param resource GD image resource.117* @return string|null JPG file as a string, or null on failure.118* @task save119*/120private static function saveImageDataAsJPG($image) {121if (!function_exists('imagejpeg')) {122return null;123}124125ob_start();126$result = imagejpeg($image);127$output = ob_get_clean();128129if (!$result) {130return null;131}132133return $output;134}135136137}138139140