diff --git a/src/abeautifulsite/SimpleImage.php b/src/abeautifulsite/SimpleImage.php index f43d218..79e58f0 100644 --- a/src/abeautifulsite/SimpleImage.php +++ b/src/abeautifulsite/SimpleImage.php @@ -26,7 +26,7 @@ class SimpleImage { */ public $quality = 80; - protected $image, $filename, $original_info, $width, $height, $imagestring; + protected $image, $filename, $original_info, $width, $height, $imagestring, $mimetype; /** * Create instance and load an image, or create an image from scratch @@ -452,6 +452,13 @@ function flip($direction) { } + /** + * Generates an image + * + * @return int + * + */ + /** * Get the current height * @@ -599,7 +606,7 @@ function opacity($opacity) { } /** - * Outputs image without saving + * Generates the image as a string it and sets mime type * * @param null|string $format If omitted or null - format of original file will be used, may be gif|jpg|png * @param int|null $quality Output image quality in percents 0-100 @@ -607,7 +614,7 @@ function opacity($opacity) { * @throws Exception * */ - function output($format = null, $quality = null) { + protected function generate($format = null, $quality = null) { // Determine quality $quality = $quality ?: $this->quality; @@ -632,14 +639,13 @@ function output($format = null, $quality = null) { break; } - // Output the image - header('Content-Type: '.$mimetype); + // Sets the image data + ob_start(); switch ($mimetype) { case 'image/gif': imagegif($this->image); break; case 'image/jpeg': - imageinterlace($this->image, true); imagejpeg($this->image, null, round($quality)); break; case 'image/png': @@ -649,6 +655,28 @@ function output($format = null, $quality = null) { throw new Exception('Unsupported image format: '.$this->filename); break; } + $imagestring = ob_get_contents(); + ob_end_clean(); + + return [ $mimetype, $imagestring ]; + } + + /** + * Outputs image without saving + * + * @param null|string $format If omitted or null - format of original file will be used, may be gif|jpg|png + * @param int|null $quality Output image quality in percents 0-100 + * + * @throws Exception + * + */ + function output($format = null, $quality = null) { + + list( $mimetype, $imagestring ) = $this->generate( $format, $quality ); + + // Output the image + header('Content-Type: '.$mimetype); + echo $imagestring; } /** @@ -663,50 +691,10 @@ function output($format = null, $quality = null) { */ function output_base64($format = null, $quality = null) { - // Determine quality - $quality = $quality ?: $this->quality; - - // Determine mimetype - switch (strtolower($format)) { - case 'gif': - $mimetype = 'image/gif'; - break; - case 'jpeg': - case 'jpg': - imageinterlace($this->image, true); - $mimetype = 'image/jpeg'; - break; - case 'png': - $mimetype = 'image/png'; - break; - default: - $info = getimagesize($this->filename); - $mimetype = $info['mime']; - unset($info); - break; - } - - // Output the image - ob_start(); - switch ($mimetype) { - case 'image/gif': - imagegif($this->image); - break; - case 'image/jpeg': - imagejpeg($this->image, null, round($quality)); - break; - case 'image/png': - imagepng($this->image, null, round(9 * $quality / 100)); - break; - default: - throw new Exception('Unsupported image format: '.$this->filename); - break; - } - $image_data = ob_get_contents(); - ob_end_clean(); + list( $mimetype, $imagestring ) = $this->generate( $format, $quality ); // Returns formatted string for img src - return 'data:'.$mimetype.';base64,'.base64_encode($image_data); + return "data:{$mimetype};base64,".base64_encode($imagestring); } @@ -881,32 +869,16 @@ function rotate($angle, $bg_color = '#000000') { function save($filename = null, $quality = null, $format = null) { // Determine quality, filename, and format - $quality = $quality ?: $this->quality; $filename = $filename ?: $this->filename; - if( !$format ) { + if( !$format ) $format = $this->file_ext($filename) ?: $this->original_info['format']; - } - // Create the image - switch (strtolower($format)) { - case 'gif': - $result = imagegif($this->image, $filename); - break; - case 'jpg': - case 'jpeg': - imageinterlace($this->image, true); - $result = imagejpeg($this->image, $filename, round($quality)); - break; - case 'png': - $result = imagepng($this->image, $filename, round(9 * $quality / 100)); - break; - default: - throw new Exception('Unsupported format'); - } + list( $mimetype, $imagestring ) = $this->generate( $format, $quality ); - if (!$result) { + // Save the image + $result = file_put_contents( $filename, $imagestring ); + if (!$result) throw new Exception('Unable to save image: ' . $filename); - } return $this;