Skip to content

Commit

Permalink
fixed osclass#1753, auto rotate images has been fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
garciademarina committed Jan 13, 2016
1 parent bdda946 commit 303ed1b
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 30 deletions.
4 changes: 3 additions & 1 deletion oc-includes/osclass/ItemActions.php
Original file line number Diff line number Diff line change
Expand Up @@ -1388,7 +1388,9 @@ public function uploadItemResources($aResources,$itemId)
// Create normal size
$normal_path = $path = $tmpName."_normal";
$size = explode('x', osc_normal_dimensions());
$img = ImageResizer::fromFile($tmpName)->autoRotate()->resizeTo($size[0], $size[1]);
$img = ImageResizer::fromFile($tmpName)->autoRotate();

$img = $img->resizeTo($size[0], $size[1]);
if( osc_is_watermark_text() ) {
$img->doWatermarkText(osc_watermark_text(), osc_watermark_text_color());
} else if ( osc_is_watermark_image() ){
Expand Down
76 changes: 49 additions & 27 deletions oc-includes/osclass/classes/ImageResizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public static function fromFile($imagePath) {
private $_color;
private $_width;
private $_height;
private $_exif;
private $_watermarked = false;


Expand All @@ -51,6 +52,8 @@ private function __construct($imagePath) {
$this->im = imagecreatefromstring($content);
$this->_width = imagesx($this->im);
$this->_height = imagesy($this->im);
$this->_exif = exif_read_data($imagePath);
// $this->autoRotate();
}

$this->image_info = @getimagesize($imagePath);
Expand Down Expand Up @@ -157,40 +160,45 @@ public function saveToFile($imagePath, $ext = null) {
}
imagejpeg($this->im, $imagePath);
break;
}
}
}
}

public function autoRotate() {
if(osc_use_imagick()) {
switch($this->im->getImageOrientation()) {
case 1:
default:
// DO NOTHING, THE IMAGE IS OK OR WE DON'T KNOW IF IT'S ROTATED
case imagick::ORIENTATION_TOPRIGHT:
$this->im->flopImage();
break;
case 2:
$this->im->flipImage();

case imagick::ORIENTATION_BOTTOMRIGHT:
$this->im->rotateimage(new ImagickPixel('none'), 180); // rotate 180 degrees
break;
case 3:

case imagick::ORIENTATION_BOTTOMLEFT:
$this->im->flopImage();
$this->im->rotateImage(new ImagickPixel('none'), 180);
break;
case 4:
$this->im->flipImage();
$this->im->rotateImage(new ImagickPixel('none'), 180);

case imagick::ORIENTATION_LEFTTOP:
$this->im->flopImage();
$this->im->rotateImage(new ImagickPixel('none'), -90);
break;
case 5:
$this->im->flipImage();
$this->im->rotateImage(new ImagickPixel('none'), 90);

case imagick::ORIENTATION_RIGHTTOP:
$this->im->rotateimage(new ImagickPixel('none'), 90); // rotate 90 degrees CW
break;
case 6:

case imagick::ORIENTATION_RIGHTBOTTOM:
$this->im->flopImage();
$this->im->rotateImage(new ImagickPixel('none'), 90);
break;
case 7:
$this->im->flipImage();
$this->im->rotateImage(new ImagickPixel('none'), 270);

case imagick::ORIENTATION_LEFTBOTTOM:
$this->im->rotateimage(new ImagickPixel('none'), -90); // rotate 90 degrees CCW
break;
case 8:
$this->im->rotateImage(new ImagickPixel('none'), 270);
default:
// DO NOTHING, THE IMAGE IS OK OR WE DON'T KNOW IF IT'S ROTATED
break;
}
} else {
Expand All @@ -201,30 +209,44 @@ public function autoRotate() {
// DO NOTHING, THE IMAGE IS OK OR WE DON'T KNOW IF IT'S ROTATED
break;
case 2:
$this->im = imageflip($this->im, IMG_FLIP_HORIZONTAL);
imageflip($this->im, IMG_FLIP_HORIZONTAL);
break;
case 3:
$this->im = imagerotate($this->im, 180, 0);
break;
case 4:
$this->im = imagerotate($this->im, 180, 0);
$this->im = imageflip($this->im, IMG_FLIP_HORIZONTAL);
imageflip($this->im, IMG_FLIP_HORIZONTAL);
break;
case 5:
$this->im = imagerotate($this->im, 90, 0);
$this->im = imageflip($this->im, IMG_FLIP_HORIZONTAL);
$this->im = imagerotate($this->im, 270, 0);
imageflip($this->im, IMG_FLIP_HORIZONTAL);
$aux = $this->_height;
$this->_height = $this->_width;
$this->_width = $aux;
break;
case 6:
$this->im = imagerotate($this->im, 90, 0);
$this->im = imagerotate($this->im, -90, 0);
$aux = $this->_height;
$this->_height = $this->_width;
$this->_width = $aux;
break;
case 7:
$this->im = imagerotate($this->im, 270, 0);
$this->im = imageflip($this->im, IMG_FLIP_HORIZONTAL);
$this->im = imagerotate($this->im, 90, 0);
imageflip($this->im, IMG_FLIP_HORIZONTAL);
$aux = $this->_height;
$this->_height = $this->_width;
$this->_width = $aux;
break;
case 8:
$this->im = imagerotate($this->im, 270, 0);
$this->im = imagerotate($this->im, 90, 0);
$aux = $this->_height;
$this->_height = $this->_width;
$this->_width = $aux;
break;
}
$this->_exif['Orientation'] = 1;

}
}
return $this;
Expand Down
9 changes: 7 additions & 2 deletions oc-includes/osclass/controller/ajax.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ function doModel()
$stringToSign = osc_get_alert_public_key() . $encoded_alert;
$signature = hex2b64(hmacsha1(osc_get_alert_private_key(), $stringToSign));
$server_signature = Session::newInstance()->_get('alert_signature');

if($server_signature != $signature) {
echo '-2';
return false;
Expand Down Expand Up @@ -292,7 +292,12 @@ function doModel()
$original = pathinfo($uploader->getOriginalName());
$filename = uniqid("qqfile_").".".$original['extension'];
$result = $uploader->handleUpload(osc_content_path().'uploads/temp/'.$filename);
$result['uploadName'] = $filename;

// auto rotate
$img = ImageResizer::fromFile(osc_content_path().'uploads/temp/'.$filename)->autoRotate();
$img->saveToFile(osc_content_path().'uploads/temp/auto_'.$filename, $original['extension']);

$result['uploadName'] = 'auto_'.$filename;
echo htmlspecialchars(json_encode($result), ENT_NOQUOTES);
break;
case 'ajax_validate':
Expand Down

0 comments on commit 303ed1b

Please sign in to comment.