Exemplo n.º 1
0
 public function resize($src, $dest = null, $width, $height, $quality, $sx = null, $sy = null, $sw = null, $sh = null)
 {
     jimport('joomla.filesystem.folder');
     jimport('joomla.filesystem.file');
     require_once dirname(__FILE__) . '/image/image.php';
     if (!isset($dest) || $dest == '') {
         $dest = $src;
     }
     $ext = strtolower(JFile::getExt($src));
     $src = @JFile::read($src);
     if ($src) {
         $image = new WFImage(null, $this->get('prefer_imagick', true));
         $image->loadString($src);
         // set type
         $image->setType($ext);
         // cropped thumbnail
         if ((isset($sx) || isset($sy)) && isset($sw) && isset($sh)) {
             $image->crop($sw, $sh, $sx, $sy);
         }
         // resize
         $image->resize($width, $height);
         switch ($ext) {
             case 'jpg':
             case 'jpeg':
                 $quality = intval($quality);
                 if ($this->get('ftp', 0)) {
                     @JFile::write($dest, $image->toString($ext, array('quality' => $quality)));
                 } else {
                     $image->toFile($dest, $ext, array('quality' => $quality));
                 }
                 break;
             default:
                 if ($this->get('ftp', 0)) {
                     @JFile::write($dest, $image->toString($ext, array('quality' => $quality)));
                 } else {
                     $image->toFile($dest, $ext, array('quality' => $quality));
                 }
                 break;
         }
         unset($image);
         unset($result);
     }
     if (file_exists($dest)) {
         @JPath::setPermissions($dest);
         return $dest;
     }
     return false;
 }
Exemplo n.º 2
0
 public function saveEdit($file, $name, $options = array(), $quality = 100)
 {
     // Check for request forgeries
     WFToken::checkToken() or die('Access to this resource is restricted');
     // check for image editor access
     if ($this->checkAccess('image_editor', 1) === false) {
         JError::raiseError(403, 'Access to this resource is restricted');
     }
     $browser = $this->getBrowser();
     $filesystem = $browser->getFileSystem();
     // check file
     self::validateImagePath($file);
     // clean temp
     $this->cleanEditorTmp($file, false);
     // check new name
     self::validateImagePath($name);
     $upload = JRequest::getVar('file', '', 'files', 'array');
     // create a filesystem result object
     $result = new WFFileSystemResult();
     if (isset($upload) && isset($upload['tmp_name']) && is_uploaded_file($upload['tmp_name'])) {
         $tmp = $upload['tmp_name'];
         self::validateImageFile($tmp);
         $exif = null;
         // get exif data from orignal file
         if (preg_match('#\\.jp(eg|g)$#i', basename($file)) && basename($file) == basename($name)) {
             // load exif classes
             require_once dirname(__FILE__) . '/pel/PelJpeg.php';
             $src = WFUtility::makePath($filesystem->getBaseDir(), $file);
             $jpeg = new PelJpeg($src);
             $exif = $jpeg->getExif();
         }
         $result = $filesystem->upload('multipart', trim($tmp), dirname($file), basename($name));
         if ($result->state === true && $exif) {
             $pel = new PelDataWindow($result->path);
             if (PelJpeg::isValid($pel)) {
                 $jpeg = new PelJpeg();
                 $jpeg->load($pel);
                 $jpeg->setExif($exif);
                 //$jpeg->saveFile($result->path);
                 // write to file
                 JFile::write($result->path, $jpeg->getBytes());
             }
         }
         @unlink($tmp);
     } else {
         // set upload as false - JSON request
         $upload = false;
         $file = WFUtility::makePath($filesystem->getBaseDir(), $file);
         $dest = dirname($file) . '/' . basename($name);
         // get extension
         $ext = WFUtility::getExtension($dest);
         // load image class
         require_once dirname(__FILE__) . '/image/image.php';
         // create image
         $image = new WFImage($file, $this->getParam('prefer_imagick', true));
         foreach ($options as $filter) {
             if (isset($filter->task)) {
                 $args = isset($filter->args) ? (array) $filter->args : array();
                 switch ($filter->task) {
                     case 'resize':
                         $w = $args[0];
                         $h = $args[1];
                         $image->resize($w, $h);
                         break;
                     case 'crop':
                         $w = $args[0];
                         $h = $args[1];
                         $x = $args[2];
                         $y = $args[3];
                         $image->crop($w, $h, $x, $y);
                         break;
                     case 'rotate':
                         $image->rotate(array_shift($args));
                         break;
                     case 'flip':
                         $image->flip(array_shift($args));
                         break;
                     default:
                         $image->filter($filter->task, $args);
                         break;
                 }
             }
         }
         // get image data
         $data = $image->toString($ext);
         // write to file
         if ($data) {
             $result->state = (bool) @JFile::write($dest, $data);
         }
         // set path
         $result->path = $dest;
     }
     if ($result->state === true) {
         // check if its a valid image
         if (@getimagesize($result->path) === false) {
             JFile::delete($result->path);
             throw new InvalidArgumentException('Invalid image file');
         } else {
             $result->path = str_replace(WFUtility::cleanPath(JPATH_SITE), '', $result->path);
             $browser->setResult(WFUtility::cleanPath($result->path, '/'), 'files');
         }
     } else {
         $browser->setResult($result->message || WFText::_('WF_IMGMANAGER_EXT_EDIT_SAVE_ERROR'), 'error');
     }
     // set header and exit
     if ($upload) {
         header("Expires: Wed, 4 Apr 1984 13:00:00 GMT");
         header("Last-Modified: " . gmdate("D, d M_Y H:i:s") . " GMT");
         header("Cache-Control: no-store, no-cache, must-revalidate");
         header("Cache-Control: post-check=0, pre-check=0", false);
         header("Pragma: no-cache");
         die(json_encode($browser->getResult()));
     }
     // return to WFRequest
     return $browser->getResult();
 }
Exemplo n.º 3
0
 /**
  * Get the available Graphics Library
  * @return object Graphics Library Instance
  * @throws RuntimeException 
  */
 public static function getLib($lib = null)
 {
     if (!isset(self::$lib)) {
         if (extension_loaded('Imagick')) {
             $lib = 'Imagick';
         } else {
             if (extension_loaded('gd')) {
                 $lib = 'GD';
             } else {
                 throw new RuntimeException('No supported Image library available.');
             }
         }
         if ($lib == 'Imagick' && self::$preferImagick === false) {
             $lib = 'GD';
         }
         require_once dirname(__FILE__) . '/' . strtolower($lib) . '.php';
         $class = 'WFImage' . $lib;
         if (class_exists($class)) {
             self::$lib = new $class();
         } else {
             throw new RuntimeException('Class ' . $class . ' not found');
         }
     }
     return self::$lib;
 }
Exemplo n.º 4
0
 /**
  * Method to write the current image out to a file.
  *
  * @param   string   $path     The filesystem path to save the image.
  * @param   integer  $type     The image type to save the file as.
  * @param   array    $options  The image type options to use in saving the file.
  *
  * @return  void
  *
  * @see     http://www.php.net/manual/image.constants.php
  * @since   11.3
  * @throws  LogicException
  */
 public function toFile($path, $type = IMAGETYPE_JPEG, array $options = array())
 {
     // Make sure the resource handle is valid.
     if (!$this->isLoaded()) {
         throw new LogicException('No valid image was loaded');
     }
     if (is_string($type)) {
         $type = WFImage::getImageType($type);
     }
     switch ($type) {
         case IMAGETYPE_GIF:
             $result = imagegif($this->handle, $path);
             break;
         case IMAGETYPE_PNG:
             $quality = array_key_exists('quality', $options) ? $options['quality'] : 0;
             // get as value from 0-9
             if ($quality) {
                 // png compression is a value from 0 (none) to 9 (max)
                 $quality = 100 - $quality;
                 // convert to value between 0 - 9
                 $quality = min(floor($quality / 10), 9);
             }
             $result = imagepng($this->handle, $path, $quality);
             break;
         case IMAGETYPE_JPEG:
         default:
             $result = imagejpeg($this->handle, $path, array_key_exists('quality', $options) ? $options['quality'] : 100);
     }
     $this->destroy();
     return $result;
 }