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);
             }
         }
     }
 }
Exemple #2
0
 /**
  * Generate an image on the specified format. It will save the image
  * at the location specified by cacheFilename(). The image will be generated
  * using the specific 'generate' method for the specified format.
  * @param string $format Name of the format to generate.
  * @param string $arg1 Argument to pass to the generate method.
  * @param string $arg2 A second argument to pass to the generate method.
  */
 function generateFormattedImage($format, $arg1 = null, $arg2 = null)
 {
     $cacheFile = $this->cacheFilename($format, $arg1, $arg2);
     $gd = new GD($this->URL);
     if ($gd->hasGD()) {
         $generateFunc = "generate{$format}";
         if ($this->hasMethod($generateFunc)) {
             $gd = $this->{$generateFunc}($gd, $arg1, $arg2);
             if ($gd) {
                 $bucket = $this->getUploadBucket();
                 // TODO create bucket if it does not exist
                 // $this->S3->putBucket($bucket, S3::ACL_PUBLIC_READ);
                 $tempFile = tempnam(getTempFolder(), 's3images') . File::get_file_extension($cacheFile);
                 $gd->writeTo($tempFile);
                 $this->S3->putObjectFile($tempFile, $bucket, '_resampled/' . basename($cacheFile), S3::ACL_PUBLIC_READ);
                 unlink($tempFile);
             }
         } else {
             USER_ERROR("Image::generateFormattedImage - Image {$format} function not found.", E_USER_WARNING);
         }
     }
 }
 /**
  * Generate an image on the specified format. It will save the image
  * at the location specified by cacheFilename(). The image will be generated
  * using the specific 'generate' method for the specified format.
  * @param string $format Name of the format to generate.
  * @param string $arg1 Argument to pass to the generate method.
  * @param string $arg2 A second argument to pass to the generate method.
  */
 function generateFormattedImage($format, $arg1 = null, $arg2 = null)
 {
     $cacheFile = $this->cacheFilename($format, $arg1, $arg2);
     $gd = new GD(Director::baseFolder() . "/" . $this->Filename);
     if ($gd->hasGD()) {
         $generateFunc = "generate{$format}";
         if ($this->hasMethod($generateFunc)) {
             $gd = $this->{$generateFunc}($gd, $arg1, $arg2);
             if ($gd) {
                 $gd->writeTo(Director::baseFolder() . "/" . $cacheFile);
             }
         } else {
             user_error("Image::generateFormattedImage - Image {$format} function not found.", E_USER_WARNING);
         }
     }
 }
Exemple #4
0
	/**
	 * Generate an image on the specified format. It will save the image
	 * at the location specified by cacheFilename(). The image will be generated
	 * using the specific 'generate' method for the specified format.
	 * @param string $format Name of the format to generate.
	 * @param string $arg1 Argument to pass to the generate method.
	 * @param string $arg2 A second argument to pass to the generate method.
	 */
	function generateFormattedImage($format, $arg1 = null, $arg2 = null) {
		$cacheFile = $this->cacheFilename($format, $arg1, $arg2);
	
		$gd = new GD("../" . $this->Filename);
		
		
		if($gd->hasGD()){
			$generateFunc = "generate$format";		
			if($this->hasMethod($generateFunc)){
				$gd = $this->$generateFunc($gd, $arg1, $arg2);
				if($gd){
					$gd->writeTo("../" . $cacheFile);
				}
	
			} else {
				USER_ERROR("Image::generateFormattedImage - Image $format function not found.",E_USER_WARNING);
			}
		}
	}