Example #1
0
 /**
  * Compress image to config configured size.
  * 
  * @param  string    $imagePath 
  * @access public
  * @return void
  */
 public function compressImage($imagePath)
 {
     $this->app->loadClass('phpthumb', true);
     $imageInfo = pathinfo($imagePath);
     if (!is_writable($imageInfo['dirname'])) {
         return false;
     }
     foreach ($this->config->file->thumbs as $size => $configure) {
         $thumbPath = str_replace('f_', $size . '_', $imagePath);
         if (extension_loaded('gd')) {
             $thumb = phpThumbFactory::create($imagePath);
             $thumb->resize($configure['width'], $configure['height']);
             $thumb->save($thumbPath);
         } else {
             copy($imagePath, $thumbPath);
         }
     }
 }
Example #2
0
 /**
  * 
  * Upload File
  * 
  * 
  */
 public function uploadImage($fileUrl = null)
 {
     jimport('joomla.filesystem.folder');
     jimport('joomla.filesystem.file');
     if ($fileUrl === null) {
         //## Image from uploading form
         $uploadedArr = $_FILES['userimage'];
         //upload file?
         if (!is_uploaded_file($uploadedArr['tmp_name'])) {
             $this->setError('COM_HS_USERS_ERROR_STRANGE_ACCESS');
             return false;
         }
         //check errors
         if ($uploadedArr['error']) {
             $this->setError('COM_HS_USERS_ERROR_FAILED_TO_UPLOAD');
             return false;
         }
     } else {
         //## download image from social web services
         $image = file_get_contents($fileUrl);
         if ($image === false) {
             $this->setError('COM_HS_USERS_MODEL_SETING_ERROR_FAILED_TO_GET_IMAGE');
             return false;
         }
         $tmp_name = uniqid('st_' . date('YmdHis') . '_');
         $tmp_folder = JPATH_ROOT . '/tmp/hsu';
         if (!is_dir($tmp_folder)) {
             JFolder::create($tmp_folder);
         }
         $tmp_path = $tmp_folder . '/' . $tmp_name;
         file_put_contents($tmp_path, $image);
         $uploadedArr['tmp_name'] = $tmp_path;
         $uploadedArr['name'] = uniqid('ssi_') . '.' . JFile::getExt($fileUrl);
     }
     $pathTmpFile = $uploadedArr['tmp_name'];
     $content = file_get_contents($pathTmpFile);
     //security check
     mb_regex_encoding("ASCII");
     if (mb_eregi('^.*<\\?php.*$', $content)) {
         $this->setError('STRANGE_FILE');
         return false;
     }
     $rawName = JFile::makeSafe($uploadedArr['name']);
     $onlyName = JFile::getName($rawName);
     //$type = $uploadedArr['type'];
     //$size =	$uploadedArr['size'];
     require_once JPATH_COMPONENT_SITE . '/helpers/folder.php';
     require_once JPATH_COMPONENT_SITE . '/helpers/image.php';
     //get image data
     $imgData = getimagesize($pathTmpFile);
     if ($imgData === false) {
         $this->setError('COM_HS_USERS_MODEL_SETTING_ERROR_CANNOT_GET_IMAGE_DATA');
         return false;
     }
     //get image
     $imgResource = imagecreatefromstring(file_get_contents($pathTmpFile));
     if ($imgResource === false) {
         $this->setError('COM_HS_USERS_MODEL_SETTING_ERROR_FAILED_TO_READ_DATA');
         return false;
     }
     //$this->setAcceptableMimes();
     //check extension
     if (!array_key_exists($imgData['mime'], $this->acceptableMimes)) {
         $this->setError('COM_HS_USERS_MODEL_SETTING_ERROR_MIME_TYPE_ERROR');
         return false;
     }
     //get extension name
     $extension = $this->acceptableMimes[$imgData['mime']];
     //convert original image to png or jpg
     if ($extension === 'png') {
         $r = imagejpeg($imgResource, $pathTmpFile, 100);
     } else {
         $r = imagepng($imgResource, $pathTmpFile, 0);
     }
     imagedestroy($imgResource);
     if ($r === false) {
         $this->setError('COM_HS_USERS_MODEL_SETTING_ERROR_FAILED_TO_CONVERT_FILE');
         return false;
     }
     //reload
     $imgResource = imagecreatefromstring(file_get_contents($pathTmpFile));
     //delete
     JFile::delete($pathTmpFile);
     //add extension name
     switch ($extension) {
         case 'gif':
             imagegif($imgResource, $pathTmpFile);
             break;
         case 'png':
             imagepng($imgResource, $pathTmpFile, 0);
             break;
         case 'jpg':
         default:
             imagejpeg($imgResource, $pathTmpFile, 100);
             break;
     }
     imagedestroy($imgResource);
     //determines actual folder
     $preData = $this->getUserData();
     $basePath = HsuHelperFolder::getBasePath();
     if (empty($preData)) {
         $folder = HsuHelperFolder::getFolder();
     } else {
         $folder = $preData->image_folder;
         if (mb_strlen(trim($folder)) < 1) {
             $folder = HsuHelperFolder::getFolder();
         }
     }
     //thumbnail
     require_once JPATH_COMPONENT . '/asset/phpthumb/ThumbLib.inc.php';
     $thumb = phpThumbFactory::create($pathTmpFile);
     //FIXME size
     $thumb->adaptiveResize(50, 50);
     //$saveName = $this->_getUniqueImageName($basePath.'/'.$folder, $extension);
     //find not duplicated file name....
     $files = JFolder::files(JPATH_ROOT . '/' . $basePath . '/' . $folder);
     $notFound = true;
     $c = 5;
     $saveName = '';
     while ($notFound) {
         $saveName = $this->_getRandomString($c) . '.' . $extension;
         if (!in_array($saveName, $files)) {
             $notFound = false;
             break;
         }
         $saveName = $this->_getRandomString($c) . '.' . $extension;
         if (!in_array($saveName, $files)) {
             $notFound = false;
             break;
         }
         $c++;
     }
     $thumb->save(JPATH_ROOT . '/' . $basePath . '/' . $folder . '/' . $saveName);
     $length = mb_strlen($onlyName);
     //check name string
     if ($length > 45) {
         $onlyName = mb_substr($onlyName, 0, 45);
         $rawName = $onlyName . '.' . $extension;
     } else {
         if ($length < 1) {
             $rawName = 'noname.' . $extension;
         }
     }
     $table = $this->getTable();
     $date = JFactory::getDate();
     $user = JFactory::getUser();
     $d = new stdClass();
     if (empty($preData)) {
         //new
         $d->image_folder = $folder;
         $d->image_name = $saveName;
         $d->image_raw_name = $rawName;
         $d->created_at = $date->toSql();
         $d->user_id = $user->get('id');
     } else {
         //have a record
         //delete previous file
         if (mb_strlen($preData->image_name) > 0) {
             $dPath = JPATH_ROOT . '/' . $basePath . '/' . $preData->image_folder . '/' . $preData->image_name;
             if (file_exists($dPath)) {
                 @JFile::delete($dPath);
             }
         }
         //
         $d->id = $preData->id;
         $d->image_folder = $folder;
         $d->image_name = $saveName;
         $d->image_raw_name = $rawName;
         $d->modified_at = $date->toSql();
     }
     //save data into database
     $table->save($d);
     //save table_id to session
     $app = JFactory::getApplication();
     $app->setUserState('com_hs_users.user_image_id', (int) $table->id);
     //delete temp
     JFile::delete($pathTmpFile);
 }
Example #3
0
 /**
  * Resize an image.
  * 
  * @param  int    $rawImage 
  * @param  int    $target 
  * @param  int    $width 
  * @param  int    $height 
  * @access public
  * @return void
  */
 public function resizeImage($rawImage, $target, $width, $height)
 {
     $this->app->loadClass('phpthumb', true);
     if (!extension_loaded('gd')) {
         return false;
     }
     $thumber = phpThumbFactory::create($rawImage);
     $thumber->resize($width, $height);
     $thumber->save($target);
 }
 public function canBackgroundFillSolidColorWithResize($width, $height, &$that)
 {
     $this->parentInstance = $that;
     $this->currentDimensions = $this->parentInstance->getCurrentDimensions();
     $this->workingImage = $this->parentInstance->getWorkingImage();
     // Resize the image (not adaptive) to see where we need to pad
     $thumb = phpThumbFactory::create($that->getFileName(), array('jpegQuality' => 100));
     $thumb->resize($width, $height);
     $this->testImage = $thumb->getWorkingImage();
     $this->currentDimensions = $thumb->getCurrentDimensions();
     // Check if we are padding vertical / horizontally
     if ($this->currentDimensions['width'] != $width) {
         if (count(array_unique($this->getColorsBetweenCorners('topleft', 'bottomleft', $this->testImage))) > 10) {
             return false;
         }
         if (count(array_unique($this->getColorsBetweenCorners('topright', 'bottomright', $this->testImage))) > 10) {
             return false;
         }
         return true;
     } elseif ($this->currentDimensions['height'] != $height) {
         if (count(array_unique($this->getColorsBetweenCorners('topleft', 'topright', $this->testImage))) > 10) {
             return false;
         }
         if (count(array_unique($this->getColorsBetweenCorners('bottomleft', 'bottomright', $this->testImage))) > 10) {
             return false;
         }
         return true;
     }
 }