Esempio n. 1
0
 public function prewriteResize()
 {
     if (!empty($_POST['fw']) && !empty($_POST['fh'])) {
         $req_width = $_POST['fw'];
         $req_height = $_POST['fh'];
     } elseif (isset($_POST['resize'])) {
         $req_height = $req_width = $_POST['resize'];
     } else {
         $req_width = $this->_max_width;
         $req_height = $this->_max_height;
     }
     if ($req_width < $this->width || $req_height < $this->height) {
         $resize_width =& $req_width;
         $resize_height =& $req_height;
     } elseif ($this->width > $this->_max_width || $this->height > $this->_max_height) {
         $resize_width =& $this->_max_width;
         $resize_height =& $this->_max_height;
     } else {
         // The request is greater in size than the original.
         return true;
     }
     $tmp_file = $this->_upload->upload['tmp_name'];
     $cpy_file = $tmp_file . '.rs';
     $result = PHPWS_File::scaleImage($tmp_file, $cpy_file, $resize_width, $resize_height);
     if (!PHPWS_Error::logIfError($result) && !$result) {
         return PHPWS_Error::get(FC_IMAGE_DIMENSION, 'filecabinet', 'PHPWS_Image::prewriteResize', array($this->width, $this->height, $this->_max_width, $this->_max_height));
     } else {
         if (!@copy($cpy_file, $tmp_file)) {
             return PHPWS_Error::get(FC_IMAGE_DIMENSION, 'filecabinet', 'PHPWS_Image::prewriteResize', array($this->width, $this->height, $this->_max_width, $this->_max_height));
         } else {
             list($this->width, $this->height, $image_type, $image_attr) = getimagesize($tmp_file);
             $image_name = $this->file_name;
             $a_image = explode('.', $image_name);
             $ext = array_pop($a_image);
             $this->file_name = sprintf('%s_%sx%s.%s', implode('.', $a_image), $this->width, $this->height, $ext);
         }
     }
     return true;
 }
Esempio n. 2
0
 public function resize($orig, $dst, $max_width, $max_height)
 {
     if (!$this->width || !$this->height) {
         return false;
     }
     $src_proportion = $this->width / $this->height;
     $new_width = $this->width;
     $new_height = $this->height;
     if ($max_width > $this->width) {
         $crop_width = $new_width;
         $crop_height = $max_height;
     } elseif ($max_height > $this->height) {
         $crop_width = $max_width;
         $crop_height = $new_height;
     } elseif ($max_width <= $max_height) {
         $new_height = $max_height;
         $new_width = round($new_height * $src_proportion);
         $crop_width = $max_width;
         $crop_height = $new_height;
         if ($crop_width > $new_width) {
             $new_width = $max_width;
             $new_height = round($new_width / $src_proportion);
         }
     } else {
         $new_width = $max_width;
         $new_height = round($new_width * $src_proportion);
         $crop_width = $new_width;
         $crop_height = $max_height;
     }
     \PHPWS_File::scaleImage($orig, $dst, $new_width, $new_height);
     return \PHPWS_File::cropImage($dst, $dst, $crop_width, $crop_height);
 }
Esempio n. 3
0
 /**
  * Backward compatibility
  */
 public static function makeThumbnail($fileName, $directory, $tndirectory, $maxWidth = 125, $maxHeight = 125, $replaceFile = false)
 {
     $source_dir = $directory . $fileName;
     $new_file = preg_replace('/\\.(jpg|jpeg|gif|png)$/i', '_tn.\\1', $fileName);
     $dest_dir = $tndirectory . $new_file;
     if (!PHPWS_File::scaleImage($source_dir, $dest_dir, $maxWidth, $maxHeight)) {
         return false;
     } else {
         $size = getimagesize($dest_dir);
         return array($new_file, $size[0], $size[1]);
     }
 }
Esempio n. 4
0
 private static function savePicture(array $file, \election\Resource\Candidate $candidate)
 {
     $filename = $file['name'];
     $tmpDir = $file['tmp_name'];
     $filetype = $file['type'];
     $size = $file['size'];
     if (!in_array($filetype, array('image/png', 'image/jpg', 'image/jpeg', 'image/gif'))) {
         throw new \Exception('Bad file type. Expecting image.');
     }
     $subDirectory = self::getImageDirectory();
     if (!is_dir($subDirectory)) {
         mkdir($subDirectory, 0755);
     }
     if (!is_writable($subDirectory)) {
         throw new \Exception('Election image directory not writable.');
     }
     $extension = \PHPWS_File::getFileExtension($filename);
     if (empty($extension)) {
         throw new \Exception('Bad file type. Expecting image.');
     }
     $filename = preg_replace('/[^\\w\\-]/', '-', $candidate->getFirstName()) . '-' . preg_replace('/[^\\w\\-]/', '-', $candidate->getLastName()) . '-' . time();
     $destination = $subDirectory . $filename . '.' . $extension;
     $count = 0;
     while (is_file($destination)) {
         $count++;
         $destination = $subDirectory . $filename . $count . '.' . $extension;
     }
     move_uploaded_file($tmpDir, $destination);
     list($width, $height) = getimagesize($destination);
     if ($width > ELECTION_MAX_CANDIDATE_WIDTH) {
         \PHPWS_File::scaleImage($destination, $destination, ELECTION_MAX_CANDIDATE_WIDTH, ELECTION_MAX_CANDIDATE_HEIGHT);
     }
     return $filename . '.' . $extension;
 }