예제 #1
0
 public function save($conn = null)
 {
     $return = parent::save($conn);
     if ($this->getObject()->getPath() != '') {
         $uploadDir = sfConfig::get('sf_upload_dir') . '/photo/';
         $image = explode('.', $this->getObject()->getPath());
         $img = new sfImage($uploadDir . $image[0] . '.' . $image[1], swImageTransform::mime_content_type($uploadDir . $image[0] . '.' . $image[1]));
         if ($img->getWidth() > $img->getHeight()) {
             $img->resize(780, 518);
         } else {
             $img->resize(344, 518);
         }
         $img->setQuality(95);
         $img->overlay(new sfImage(sfConfig::get('sf_web_dir') . '/images/marcadagua.png', 'image/png'), 'bottom-right');
         $img->saveAs($uploadDir . $this->getObject()->getSlug() . '.' . $image[1]);
         if ($img->getWidth() > $img->getHeight()) {
             $img->resize(130, 86);
         } else {
             $img->resize(130, 196);
             $img->crop(0, 55, 130, 86);
         }
         $img->saveAs($uploadDir . 'thumb_' . $this->getObject()->getSlug() . '.' . $image[1]);
         unlink($uploadDir . $image[0] . '.' . $image[1]);
         $this->getObject()->setPath($this->getObject()->getSlug() . '.' . $image[1]);
         $this->getObject()->save();
     }
     return $return;
 }
예제 #2
0
 public function transform(sfImage $img)
 {
     $this->directory = dirname($img->getFilename());
     $arr = array_reverse(explode("/", $img->getFilename()));
     $this->fileName = $arr[0];
     $arr2 = explode(".", $this->fileName);
     if (count($arr2) == 1) {
         $this->fileName .= '.png';
     }
     $destDir = '/tmp';
     $ccFile = $destDir . DIRECTORY_SEPARATOR . 'cc_' . $this->fileName;
     $bwFile = $destDir . DIRECTORY_SEPARATOR . 'bw_' . $this->fileName;
     $ccSmallFile = $destDir . DIRECTORY_SEPARATOR . 'cc_s_' . $this->fileName;
     $bwSmallFile = $destDir . DIRECTORY_SEPARATOR . 'bw_s_' . $this->fileName;
     if (!file_exists($destDir)) {
         mkdir($destDir);
     }
     if ($img->getWidth() > IMG_MAX_WIDTH || $img->getHeight() > IMG_MAX_HEIGHT) {
         if ($img->getWidth() > $img->getHeight() * IMG_RATIO) {
             $img->resize(IMG_MAX_WIDTH, null);
         } else {
             $img->resize(null, IMG_MAX_HEIGHT);
         }
     }
     $img->saveAs($ccFile);
     $img->greyscale()->saveAs($bwFile);
     $smallImg = new sfImage($ccFile);
     if ($smallImg->getWidth() > IMG_SMALL_WIDTH || $smallImg->getHeight() > IMG_SMALL_WIDTH) {
         if ($smallImg->getWidth() > $img->getHeight() * IMG_RATIO) {
             $smallImg->resize(null, IMG_MAX_HEIGHT);
         } else {
             $smallImg->resize(IMG_MAX_WIDTH, null);
         }
     }
     if ($smallImg->getWidth() > IMG_SMALL_WIDTH || $smallImg->getHeight() > IMG_SMALL_HEIGHT) {
         if ($smallImg->getWidth() > $smallImg->getHeight() * IMG_RATIO) {
             $smallImg->resize(null, IMG_SMALL_HEIGHT);
         } else {
             $smallImg->resize(IMG_SMALL_WIDTH, null);
         }
     }
     $x1 = ($smallImg->getWidth() - IMG_SMALL_WIDTH) / 2;
     $y1 = ($smallImg->getHeight() - IMG_SMALL_HEIGHT) / 3;
     $smallImg->crop($x1, $y1, IMG_SMALL_WIDTH, IMG_SMALL_HEIGHT)->saveAs($ccSmallFile);
     $smallImg->greyscale()->saveAs($bwSmallFile);
 }
 /**
  * Apply the transformation to the image and returns the image thumbnail
  */
 protected function transform(sfImage $image)
 {
     $resource_w = $image->getWidth();
     $resource_h = $image->getHeight();
     $scale_w = $this->getWidth() / $resource_w;
     $scale_h = $this->getHeight() / $resource_h;
     $ratio_w = $resource_w / $this->getWidth();
     $ratio_h = $resource_w / $this->getHeight();
     switch ($this->getMethod()) {
         case 'deflate':
         case 'inflate':
             return $image->resize($this->getWidth(), $this->getHeight());
         case 'west':
             $image->scale(max($scale_w, $scale_h));
             return $image->crop($this->getWidth(), $this->getHeight(), 0, 0);
         case 'east':
             $image->scale(max($scale_w, $scale_h));
             return $image->crop($this->getWidth(), $this->getHeight(), (int) ($image->getWidth() - $this->getWidth()), 0);
         case 'north':
             $image->scale(max($scale_w, $scale_h));
             return $image->crop($this->getWidth(), $this->getHeight(), 0, 0);
         case 'south':
             $image->scale(max($scale_w, $scale_h));
             return $image->crop($this->getWidth(), $this->getHeight(), 0, $image->getHeight() - $this->getHeight());
         case 'center':
             $image->scale(max($scale_w, $scale_h));
             $left = (int) round(($image->getWidth() - $this->getWidth()) / 2);
             $top = (int) round(($image->getHeight() - $this->getHeight()) / 2);
             return $image->crop($this->getWidth(), $this->getHeight(), $left, $top);
         case 'scale':
         default:
             return $image->scale(min($scale_w, $scale_h));
     }
 }
 /**
  * Apply the transformation to the image and returns the image thumbnail
  */
 protected function transform(sfImage $image)
 {
     $resource_w = $image->getWidth();
     $resource_h = $image->getHeight();
     $scale_w = $this->getWidth() / $resource_w;
     $scale_h = $this->getHeight() / $resource_h;
     switch ($this->getMethod()) {
         case 'deflate':
         case 'inflate':
             return $image->resize($this->getWidth(), $this->getHeight());
         case 'left':
             $image->scale(max($scale_w, $scale_h));
             return $image->crop(0, (int) round(($image->getHeight() - $this->getHeight()) / 2), $this->getWidth(), $this->getHeight());
         case 'right':
             $image->scale(max($scale_w, $scale_h));
             return $image->crop($image->getWidth() - $this->getWidth(), (int) round(($image->getHeight() - $this->getHeight()) / 2), $this->getWidth(), $this->getHeight());
         case 'top':
             $image->scale(max($scale_w, $scale_h));
             return $image->crop((int) round(($image->getWidth() - $this->getWidth()) / 2), 0, $this->getWidth(), $this->getHeight());
         case 'bottom':
             $image->scale(max($scale_w, $scale_h));
             return $image->crop((int) round(($image->getWidth() - $this->getWidth()) / 2), $image->getHeight() - $this->getHeight(), $this->getWidth(), $this->getHeight());
         case 'center':
             $image->scale(max($scale_w, $scale_h));
             $left = (int) round(($image->getWidth() - $this->getWidth()) / 2);
             $top = (int) round(($image->getHeight() - $this->getHeight()) / 2);
             return $image->crop($left, $top, $this->getWidth(), $this->getHeight());
         case 'scale':
             return $image->scale(min($scale_w, $scale_h));
         case 'fit':
         default:
             $img = clone $image;
             $image->create($this->getWidth(), $this->getHeight());
             // Set a background color if specified
             if (!is_null($this->getBackground()) && $this->getBackground() != '') {
                 $image->fill(0, 0, $this->getBackground());
             }
             $img->scale(min($this->getWidth() / $img->getWidth(), $this->getHeight() / $img->getHeight()));
             $image->overlay($img, 'center');
             return $image;
     }
 }