Skip to content

Commit

Permalink
Refactored output and save method (#126)
Browse files Browse the repository at this point in the history
* Center crop image and then resize it to fit within specified dimensions

* Refactoring of output and save methods

* Refactoring of output and save methods

* Refactoring of output and save methods

* Refactored generate method to return array with mimetype and image string. Refactored methods using generate to accomodate for the change.
  • Loading branch information
alexxandar authored and claviska committed Jul 28, 2016
1 parent cb3e36c commit 968272a
Showing 1 changed file with 41 additions and 69 deletions.
110 changes: 41 additions & 69 deletions src/abeautifulsite/SimpleImage.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -452,6 +452,13 @@ function flip($direction) {

}

/**
* Generates an image
*
* @return int
*
*/

/**
* Get the current height
*
Expand Down Expand Up @@ -599,15 +606,15 @@ 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
*
* @throws Exception
*
*/
function output($format = null, $quality = null) {
protected function generate($format = null, $quality = null) {

// Determine quality
$quality = $quality ?: $this->quality;
Expand All @@ -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':
Expand All @@ -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;
}

/**
Expand All @@ -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);

}

Expand Down Expand Up @@ -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;

Expand Down

0 comments on commit 968272a

Please sign in to comment.