Hi Pyromanik!
whats the "wee preview image" ?
i modyfied https://github.com/axllent/silverstripe-scaled-uploads/blob/master/code/ScaledUploads.php
and changed the switch - function in exifRotation
now all images are handled correct from ipad.
Another issue is that i can only access the ipad camera from wee preview and not from gridfield -bulk - upload.
also the cache problem is solved with a dirty rename action.
Its not updating imediatly but when clicking the second save-button on bottom.
class MyImageDecorator extends DataExtension{
public static $max_width = 1200;
public static $max_height = 1000;
public static $exif_rotation = true;
public function getMaxWidth() {
$w = Config::inst()->get('MyImageDecorator', 'max-width');
return ($w) ? $w : self::$max_width;
}
public function getMaxHeight() {
$h = Config::inst()->get('MyImageDecorator', 'max-height');
return ($h) ? $h : self::$max_height;
}
public function getAutoRotate() {
$r = Config::inst()->get('MyImageDecorator', 'auto-rotate');
if ($r === 0 || $r == 'false') return false;
return self::$exif_rotation;
}
function getCustomFields(){
$selectionGroup = new OptionsetField(
$name = "Rotate",
$title = "Rotate",
$source = array(
"1" => "90º CW",
"2" => "90º CCW",
"3" => "180º"
),
$value = ""
);
$fields = new FieldList();
$fields->push($selectionGroup);
return $fields;
}
function onAfterWrite(){
if(isset($_POST['Rotate'])){
$this->ManualRotate($_POST['Rotate']);
}else{
$this->ExifRotate();
}
}
public function ManualRotate($rotation){
switch($rotation) {
case 1: // 90 rotate CW
$angle = 270;
break;
case 2: // 90 rotate CCW
$angle = 90;
break;
case 3: // image upside down
$angle = 180;
break;
}
$extension = strtolower($this->owner->getExtension());
$original = $this->owner->getFullPath();
$name = $this->owner->Name;
$filename = $this->owner->Filename;
$image = Image::get()->byID($this->owner->ID);
$resampled = $original . '.tmp.' . $extension;
$gd = new GD($original);
$image_loaded = (method_exists('GD', 'hasImageResource')) ? $gd->hasImageResource() : $gd->hasGD();
if($image_loaded){
$transformed = $gd;
$transformed = $transformed->rotate($angle);
if($transformed){
$time = time();
$transformed->writeTo($resampled);
unlink($original);
$this->owner->deleteFormattedImages();
//rename($resampled, $original);
$original = preg_replace('/\\.[^.\\s]{3,4}$/', '', $original); // remove Extention
$orig_Filename = preg_replace('/\\.[^.\\s]{3,4}$/', '', $filename);
$original = preg_replace('/--.*/', '', $original);
$orig_Filename = preg_replace('/--.*/', '', $orig_Filename);
$name = preg_replace('/\\.[^.\\s]{3,4}$/', '', $name);
$name = preg_replace('/--.*/', '', $name);
rename($resampled, $original.'--'.$time.'.'.$extension);
DB::query("UPDATE File SET Filename='".$orig_Filename.'--'.$time.'.'.$extension."', Name='".$name.'--'.$time.'.'.$extension."' WHERE ID = ".$this->owner->ID."");
}
}
}
public function ExifRotate() {
$extension = strtolower($this->owner->getExtension());
if($this->owner->getHeight() > $this->getMaxHeight() || $this->owner->getWidth() > $this->getMaxWidth()) {
$original = $this->owner->getFullPath();
$resampled = $original . '.tmp.' . $extension;
$gd = new GD($original);
/* Backwards compatibility with SilverStripe 3.0 */
$image_loaded = (method_exists('GD', 'hasImageResource')) ? $gd->hasImageResource() : $gd->hasGD();
if ($image_loaded) {
/* Clone original */
$transformed = $gd;
/* If rotation allowed & JPG, test to see if orientation needs switching */
if ($this->getAutoRotate() && preg_match('/jpe?g/i', $extension)) {
$switchorientation = $this->exifRotation($original);
if ($switchorientation) {
$transformed = $transformed->rotate($switchorientation);
}
}
/* Resize to max values */
if ($transformed) {
$transformed = $transformed->resizeRatio($this->getMaxWidth(), $this->getMaxHeight());
}
/* Overwrite original upload with resampled */
if ($transformed) {
$transformed->writeTo($resampled);
unlink($original);
rename($resampled, $original);
}
}
}
}
public function exifRotation($file) {
$exif = @exif_read_data($file);
if (!$exif) return false;
$ort = @$exif['IFD0']['Orientation'];
if (!$ort) $ort = @$exif['Orientation'];
switch($ort) {
case 1:
return false;
break;
case 2:
return '180';
break;
case 3:
return '180';
break;
case 4:
return '180';
break;
case 5:
return '-90';
break;
case 6:
return '-90';
break;
case 7:
return '-90';
break;
case 8:
return '90';
break;
default:
return false;
}
}