Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function uploadfile() {
- JRequest::setVar('tmpl','component');
- $db =& JFactory::getDBO();
- $session = JFactory::getSession();
- $config = RSCommentsHelper::getConfig();
- $file = JRequest::getVar('file', null, 'files', 'array');
- $IdComment = $session->get('com_rscomments.IdComment','');
- $root = JRequest::getVar('root');
- $url_captcha = JRequest::getVar('url_captcha');
- if (!$config->enable_upload) return;
- jimport('joomla.filesystem.file');
- $uploadFolder = JPATH_SITE.DS.'components'.DS.'com_rscomments'.DS.'assets'.DS.'files'.DS;
- $valid = true;
- $successfully_uploaded = '';
- $msg = '';
- if (!empty($file) && empty($file['error']))
- {
- $src = $file['tmp_name'];
- $filename = JFile::makeSafe($file['name']);
- $ext = strtolower(JFile::getExt($filename));
- $filename = JFile::stripExt($filename);
- // if we edit the file and upload another file
- $extensions = strtolower($config->allowed_extensions);
- $extensions = str_replace("r",'',$extensions);
- $extensions = explode("n",$extensions);
- if (!empty($extensions) && is_array($extensions) && in_array($ext,$extensions))
- {
- $max_size = 1024 * 1024;
- $set_size = 1;
- if ($max_size > $file['size'])
- {
- while (JFile::exists($uploadFolder.$filename.'.'. $ext))
- $filename .= rand(10, 99);
- $dest = $uploadFolder.$filename.'.'.$ext;
- // this is the part where I start adding code
- // Get current sizes
- list ($startWidth,$startHeight) = getimagesize($src);
- // conditional statement
- if ($startWidth > 500) {
- // Load image
- $src_to_resize = imagecreatefromjpeg($src);
- // setting new width and calculate new height
- $newWidth = 500;
- $newHeight = (int) ($startHeight * (500 / $startWidth));
- // create a new temporary image
- $tmp = imagecreatetruecolor($newWidth, $newHeight);
- // Resize
- imagecopyresized($tmp, $src_to_resize, 0, 0, 0, 0, $newWidth, $newHeight, $startWidth, $startHeight);
- // Output
- imagejpeg($tmp,$src_to_resize,70);
- JFile::upload($src_to_resize, $dest);
- // echo's to see what's going on
- $msg = 'n$startWidth = '.$startWidth;
- $msg .= 'n$startHeight = '.$startHeight;
- $msg .= 'n$newWidth = '.$newWidth;
- $msg .= 'n$newHeight = '.$newHeight;
- $msg .= 'n$src inside added code = '.$src;
- $msg .= 'n$src_to_resize = '.$src_to_resize;
- $msg .= 'n$tmp inside added code = '.$tmp;
- $msg .= 'n$dest inside added code = '.$dest;
- }
- // from this point it's the original code
- // it was originally not between this else-statement
- else {
- JFile::upload($src, $dest);
- }
- $db->setQuery("INSERT INTO #__rscomments_comments SET `file` = '".$db->getEscaped($filename.'.'.$ext)."'");
- $db->query();
- $IdComment = $db->insertid();
- $session->set('com_rscomments.IdComment', $IdComment);
- $valid = true;
- } else { $msg = JText::sprintf('RSC_ERROR_SIZE',$set_size); $valid = false; }
- } else { $msg = JText::sprintf('RSC_ERROR_EXTENSION',implode(', ',$extensions)); $valid = false; }
- }
- echo '<form name="frameform" id="frameform" action="'.JRoute::_('index.php?option=com_rscomments&task=uploadfile').'" method="post" enctype="multipart/form-data">';
- echo '<input type="file" name="file" />'.$successfully_uploaded;
- echo '<input type="hidden" name="root" id="root" value="" />';
- echo '<input type="hidden" name="url_captcha" id="url_captcha" value="" />';
- echo '</form>';
- if ($valid) {
- echo "<script type="text/javascript">
- alert('".$msg."');
- window.parent.rsc_save('".$root."','".$url_captcha."');
- </script>";
- } else {
- if($msg != ''){
- echo "
- <script type="text/javascript">
- alert('".$msg."');
- // reload captcha
- sign = ('".$url_captcha."'.indexOf('?') == -1) ? '?' : '&';
- if(window.parent.document.getElementById('submit_captcha_image')) window.parent.document.getElementById('submit_captcha_image').src = '".$url_captcha."'+sign+'sid=' + Math.random();
- // reload recaptcha
- if(window.parent.document.getElementById('rsc_recaptcha'))
- {
- window.parent.document.getElementById('recaptcha_response_field').value = '';
- window.parent.Recaptcha.reload();
- }
- </script>";
- }
- }
- exit();
- }
- $startWidth = 600
- $startHeight = 183
- $newWidth = 500
- $newHeight = 152
- $src inside added code = /tmp/phpi108VE
- $src_to_resize inside added code = Resource id #126
- $tmp inside added code = Resource id #127
- $dest inside added code = serverpath/test600px55.jpg
Add Comment
Please, Sign In to add comment