Skip to content
Permalink
Browse files

auto-rotate upright images according to EXIF orientation; updates #732

  • Loading branch information...
following5 committed Aug 16, 2015
1 parent 6ec5728 commit db139b8d1e0e784be8a96df727605bccb2799c26
Showing with 55 additions and 12 deletions.
  1. +42 −2 htdocs/lib2/logic/picture.class.php
  2. +13 −10 htdocs/picture.php
@@ -463,14 +463,14 @@ function getPageLink()
$longSideSize: if longer side of picture > $longSideSize, then it will be prop. shrinked to
returns: true if no error occur, otherwise false
*/
public function shrink($tmpFile,$longSideSize)
public function rotate_and_shrink($tmpFile,$longSideSize)
{
global $opt;
if (extension_loaded('imagick')) {
try {
$image = new Imagick();
$image->readImage($tmpFile);
$this->imagick_rotate($image);
$w=$image->getImageWidth();
$h=$image->getImageHeight();
$image->setImageResolution(PICTURE_RESOLUTION,PICTURE_RESOLUTION);
@@ -519,5 +519,45 @@ public function shrink($tmpFile,$longSideSize)
}
else return false;
}
// rotate image according to EXIF orientation
public function rotate($tmpFile)
{
if (extension_loaded('imagick'))
{
try
{
$image = new Imagick();
$image->readImage($tmpFile);
if ($this->imagick_rotate($image))
{
$image->writeImage($this->getFilename());
$image->clear();
return true;
}
else
$image->clear();
}
catch (Exception $e)
{
if ($image) $image->clear();
}
}
return move_uploaded_file($tmpFile, $this->getFilename());
}
function imagick_rotate(&$image)
{
$exif = $image->getImageProperties();
if (isset($exif['exif:Orientation']))
switch ($exif['exif:Orientation'])
{
case 3: return $image->rotateImage(new ImagickPixel(), 180);
case 6: return $image->rotateImage(new ImagickPixel(), 90);
case 8: return $image->rotateImage(new ImagickPixel(), -90);
}
return false;
}
}
?>
@@ -122,23 +122,26 @@
if ($bError == false)
{
$picture->setLocal(1);
list($fname,$ext)=explode('.',$_FILES['file']['name'],2);
$fname = pathinfo($_FILES['file']['name'], PATHINFO_FILENAME);
$ext = pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION);
// try saving file if smaller unchg_size and browser native format
if (in_array(mb_strtolower($ext), array('gif','png','jpg','jpeg'))
&&($_FILES['file']['size'] < $opt['logic']['pictures']['unchg_size'])){
&& ($_FILES['file']['size'] <= $opt['logic']['pictures']['unchg_size']))
{
$picture->setFilenames($_FILES['file']['name']);
if (!move_uploaded_file($_FILES['file']['tmp_name'], $picture->getFilename()))
{
$bError = true;
}
if (!$picture->rotate($_FILES['file']['tmp_name']))
$bError = true;
}
// try saving as jpg and shrinking if > PICTURE_MAX_LONG_SIDE file
else {
else
{
$picture->setFilenames(mb_strtolower($fname).'.jpg');
if (!$picture->shrink($_FILES['file']['tmp_name'],PICTURE_MAX_LONG_SIDE)) $bError = true;
if (!$picture->rotate_and_shrink($_FILES['file']['tmp_name'], PICTURE_MAX_LONG_SIDE))
$bError = true;
}
//try to save in db
if ( !$bError && $picture->save())
// try to save in db
if (!$bError && $picture->save())
{
if ($redirect == '')
$redirect = $picture->getPageLink();

0 comments on commit db139b8

Please sign in to comment.
You can’t perform that action at this time.