Skip to content

Commit

Permalink
Rewrite
Browse files Browse the repository at this point in the history
  • Loading branch information
axllent committed Jun 28, 2017
1 parent 26a9802 commit 65a9613
Showing 1 changed file with 38 additions and 31 deletions.
69 changes: 38 additions & 31 deletions src/ScaledUploads.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,35 @@

class ScaledUploads extends Extension
{
protected $max_width;
protected $max_height;
protected $auto_rotate;
protected $bypass;
protected $force_resampling;

public function __construct()
{
$this->config = Config::inst();
$this->max_width = $this->currConfig('max-width', 960);
$this->max_height = $this->currConfig('max-height', 800);
$this->auto_rotate = $this->currConfig('auto-rotate', true);
$this->bypass = $this->currConfig('bypass', false);
$this->force_resampling = $this->currConfig('force-resampling', true);
}

public function onAfterLoadIntoFile($file)
{
if ($this->getBypass() || !$file->IsImage) {
if ($this->bypass || !$file->IsImage) {
return;
}

$extension = $file->getExtension();

if (
($this->getMaxHeight() && $file->getHeight() > $this->getMaxHeight()) ||
($this->getMaxWidth() && $file->getWidth() > $this->getMaxWidth()) ||
($this->getAutoRotate() && preg_match('/jpe?g/i', $file->getExtension()))
$this->force_resampling ||
($this->max_height && $file->getHeight() > $this->max_height) ||
($this->max_width && $file->getWidth() > $this->max_width) ||
($this->auto_rotate && preg_match('/jpe?g/i', $file->getExtension()))
) {
$this->ScaleUploadedImage($file);
}
Expand All @@ -68,9 +79,8 @@ private function ScaleUploadedImage($file)
$transformed = $gd;

/* If rotation allowed & JPG, test to see if orientation needs switching */
if ($this->getAutoRotate() && preg_match('/jpe?g/i', $file->getExtension())) {
if ($this->auto_rotate && preg_match('/jpe?g/i', $file->getExtension())) {
$switch_orientation = $this->exifRotation($tmp_image);
// die('rotating?: ' . $switch_orientation);
if ($switch_orientation) {
$modified = true;
$transformed = $transformed->rotate($switch_orientation);
Expand All @@ -81,18 +91,20 @@ private function ScaleUploadedImage($file)
if (
$transformed &&
(
($this->getMaxWidth() && $transformed->getWidth() > $this->getMaxWidth()) ||
($this->getMaxHeight() && $transformed->getHeight() > $this->getMaxHeight())
($this->max_width && $transformed->getWidth() > $this->max_width) ||
($this->max_height && $transformed->getHeight() > $this->max_height)
)
) {
if ($this->getMaxWidth() && $this->getMaxHeight()) {
$transformed = $transformed->resizeRatio($this->getMaxWidth(), $this->getMaxHeight());
} elseif ($this->getMaxWidth()) {
$transformed = $transformed->resizeByWidth($this->getMaxWidth());
if ($this->max_width && $this->max_height) {
$transformed = $transformed->resizeRatio($this->max_width, $this->max_height);
} elseif ($this->max_width) {
$transformed = $transformed->resizeByWidth($this->max_width);
} else {
$transformed = $transformed->resizeByHeight($this->getMaxHeight());
$transformed = $transformed->resizeByHeight($this->max_height);
}
$modified = true;
} elseif ($transformed && $this->force_resampling) {
$modified = true;
}

/* Write to tmp file and then overwrite original */
Expand All @@ -104,31 +116,22 @@ private function ScaleUploadedImage($file)
$file->File->deleteFile(); // delete original else a rogue copy is left

$file->setFromLocalFile($tmp_image, $file->FileName); // set new image

}
}

@unlink($tmp_image); // delete tmp file
}

public function getBypass()
{
return $this->config->get('Axllent\\ScaledUploads\\ScaledUploads', 'bypass');
}

public function getMaxWidth()
{
return $this->config->get('Axllent\\ScaledUploads\\ScaledUploads', 'max-width');
}

public function getMaxHeight()
{
return $this->config->get('Axllent\\ScaledUploads\\ScaledUploads', 'max-height');
}

public function getAutoRotate()
/**
* Check current config else return a default
* @param String, value
* @return value
*/
protected function currConfig($key, $default = false)
{
return $this->config->get('Axllent\\ScaledUploads\\ScaledUploads', 'auto-rotate');
$val = $this->config->get('Axllent\\ScaledUploads\\ScaledUploads', $key);
return (isset($val)) ? $val : $default;
}

/**
Expand All @@ -141,15 +144,19 @@ public function exifRotation($file)
if (!function_exists('exif_read_data')) {
return false;
}

$exif = @exif_read_data($file);
// var_dump($exif);

if (!$exif) {
return false;
}

$ort = @$exif['IFD0']['Orientation'];

if (!$ort) {
$ort = @$exif['Orientation'];
}

switch ($ort) {
case 3: // image upside down
return '180';
Expand Down

0 comments on commit 65a9613

Please sign in to comment.