/** * Resamples the image to the maximum Height and Width */ public function resampleImage() { $extension = strtolower($this->owner->getExtension()); if ($this->owner->getHeight() > $this->getMaxX() || $this->owner->getWidth() > $this->getMaxY()) { $original = $this->owner->getFullPath(); $resampled = $original . '.tmp.' . $extension; $gd = new GD($original); if ($gd->hasImageResource()) { $gd = $gd->resizeRatio($this->getMaxX(), $this->getMaxY()); if ($gd) { $gd->writeTo($resampled); unlink($original); rename($resampled, $original); } } } }
public function ScaleUpload() { /* don't use Image->exists() as it is implemented differently for Image */ if ($this->owner->ID > 0 || $this->getBypass()) { return; // only run with new images } $extension = $this->owner->getExtension(); if ($this->owner->getHeight() > $this->getMaxHeight() || $this->owner->getWidth() > $this->getMaxWidth() || $this->getAutoRotate() && preg_match('/jpe?g/i', $extension)) { $original = $this->owner->getFullPath(); /* temporary location for image manipulation */ $resampled = TEMP_FOLDER . '/resampled-' . mt_rand(100000, 999999) . '.' . $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)) { $switch_orientation = $this->exifRotation($original); if ($switch_orientation) { $transformed = $transformed->rotate($switch_orientation); } } /* Resize to max values */ if ($transformed && ($transformed->getWidth() > $this->getMaxWidth() || $transformed->getHeight() > $this->getMaxHeight())) { $transformed = $transformed->resizeRatio($this->getMaxWidth(), $this->getMaxHeight()); } /* Write to tmp file and then overwrite original */ if ($transformed) { $transformed->writeTo($resampled); file_put_contents($original, file_get_contents($resampled)); unlink($resampled); } } } }