1

在实现图像调整器脚本时遇到问题:

<?php
// Create image from file
switch(strtolower($_FILES['Filedata']['type']))
{
case 'image/jpeg':
         $image = imagecreatefromjpeg($_FILES['Filedata']['tmp_name']);
         break;
case 'image/png':
         $image = imagecreatefrompng($_FILES['Filedata']['tmp_name']);
         break;
case 'image/gif':
         $image = imagecreatefromgif($_FILES['Filedata']['tmp_name']);
         break;
}
// Target dimensions
$max_width = 240;
$max_height = 180;
// Get current dimensions
$old_width = imagesx($image);
$old_height = imagesy($image);
// Calculate the scaling we need to do to fit the image inside our frame
$scale   = min($max_width/$old_width, $max_height/$old_height);
// Get the new dimensions
$new_width = ceil($scale*$old_width);
$new_height = ceil($scale*$old_height);
// Create new empty image
$new = imagecreatetruecolor($new_width, $new_height);
// Resize old image into new
imagecopyresampled($new, $image,
0, 0, 0, 0,
$new_width, $new_height, $old_width, $old_height);

// Catch the imagedata
ob_start();
imagejpeg($new, NULL, 90);
$data = ob_get_clean();
// Destroy resources
imagedestroy($image);
imagedestroy($new);
?>

到以下上传脚本:

<?php
/* *
         * upload avatar
         */
        function uploadavatar()
        {
                $app = JFactory::getApplication();

                $db = JFactory::getDBO();
                $user =  JFactory::getUser();           

                // load language for component media
                JPlugin::loadLanguage( 'com_media', JPATH_SITE );
                JPlugin::loadLanguage( 'com_media', JPATH_ADMINISTRATOR );

                $params = JComponentHelper::getParams('com_media');

                require_once( JPATH_SITE.DS.'components'.DS.'com_media'.DS.'helpers'.DS.'media.php' );

                define('COM_AUP_MEDIA_BASE_IMAGE', JPATH_ROOT.DS.'components'.DS.'com_alphauserpoints'.DS.'assets'.DS.'images');                

                // Check for request forgeries
                JRequest::checkToken( 'request' ) or jexit( 'Invalid Token' );

                $file            = JRequest::getVar( 'Filedata', '', 'files', 'array' );
                $folder         = JRequest::getVar( 'folder', 'avatars', '', 'path' );
                $format         = JRequest::getVar( 'format', 'html', '', 'cmd');
                $return         = JRequest::getVar( 'return-url', null, 'post', 'base64' );
                $referrerid = JRequest::getVar( 'referrerid', '', 'post', 'string' );
                $err            = null;

                // Set FTP credentials, if given
                jimport('joomla.client.helper');
                JClientHelper::setCredentialsFromRequest('ftp');

                // Make the filename safe
                jimport('joomla.filesystem.file');
                $file['name'] = JFile::makeSafe($file['name']);

                if (isset($file['name']) && $referrerid!='') {
                        $extention = JFile::getExt($file['name']);
                        $newnameavatar = strtolower($referrerid.'.'.$extention);

                        //chmod (COM_AUP_MEDIA_BASE_IMAGE.DS.$folder, 0755) ;
                        $filepath = JPath::clean(COM_AUP_MEDIA_BASE_IMAGE.DS.$folder.DS.$newnameavatar);
                        // erase old avatar
                        if ( file_exists($filepath) ) @unlink( $filepath );

                        if (!MediaHelper::canUpload( $file, $err )) {
                                if ($format == 'json') {
                                        jimport('joomla.error.log');
                                        $log = JLog::getInstance('upload.error.php');
                                        $log->addEntry(array('comment' => 'Invalid: '.$filepath.': '.$err));
                                        header('HTTP/1.0 415 Unsupported Media Type');
                                        jexit('Error. Unsupported Media Type!');
                                } else {
                                        JError::raiseNotice(100, JText::_($err));
                                        // REDIRECT
                                        if ($return) {
                                                $app->redirect(base64_decode($return));
                                        }
                                        return;
                                }
                        }

                        if (JFile::exists($filepath)) {
                                if ($format == 'json') {
                                        jimport('joomla.error.log');
                                        $log = JLog::getInstance('upload.error.php');
                                        $log->addEntry(array('comment' => 'File already exists: '.$filepath));
                                        header('HTTP/1.0 409 Conflict');
                                        jexit('Error. File already exists');
                                } else {
                                        JError::raiseNotice(100, JText::_('UPLOAD FAILED. FILE ALREADY EXISTS'));
                                        // REDIRECT
                                        if ($return) {
                                                $app->redirect(base64_decode($return));
                                        }
                                        return;
                                }
                        }

                        if (!JFile::upload($file['tmp_name'], $filepath)) {
                                if ($format == 'json') {
                                        jimport('joomla.error.log');
                                        $log = JLog::getInstance('upload.error.php');
                                        $log->addEntry(array('comment' => 'Cannot upload: '.$filepath));
                                        header('HTTP/1.0 400 Bad Request');
                                        jexit('ERROR. UNABLE TO UPLOAD FILE');
                                } else {
                                        JError::raiseWarning(100, JText::_('ERROR. UNABLE TO UPLOAD FILE'));
                                        // REDIRECT
                                        if ($return) {
                                                $app->redirect(base64_decode($return));
                                        }
                                        return;
                                }
                        } else {
                                // SAVE IN PROFIL USER ALPHAUSERPOINTS
                                $query = "UPDATE #__alpha_userpoints" .
                                "\n SET avatar='".$newnameavatar."'" .
                                "\n WHERE referreid='".$referrerid."' AND userid='" . $user->id . "'"
                                ;
                                $db->setQuery( $query );                        
                                if (!$db->query())
                                {
                                        JError::raiseError( 500, $db->getErrorMsg() );                  
                                        return false;
                                }

                                require_once (JPATH_SITE.DS.'components'.DS.'com_alphauserpoints'.DS.'helper.php');

                                if ($format == 'json') {
                                        jimport('joomla.error.log');
                                        $log = JLog::getInstance();
                                        $log->addEntry(array('comment' => $folder));
                                        jexit('Upload complete');
                                        // apply rule for upload avatar
                                        AlphaUserPointsHelper::userpoints( 'sysplgaup_uploadavatar', '', 0, $referrerid );

                                } else {
                                        $app->enqueueMessage(JText::_('UPLOAD COMPLETE'));

                                        // apply rule for upload avatar
                                        AlphaUserPointsHelper::userpoints( 'sysplgaup_uploadavatar', '', 0, $referrerid );

                                        // REDIRECT
                                        if ($return) {
                                                $app->redirect(base64_decode($return));
                                        }
                                        return;
                                }
                        }
                } else {
                        $app->redirect('index.php', 'Invalid Request', 'error');
                }
        }

}
?>

我试图用 $image 替换 $file 字符串,但没有运气。我认为问题可能在于:

$file = JRequest::getVar( 'Filedata', '', 'files', 'array' );

没有得到与此相同的数据:

$_FILES['Filedata']

这就是为什么我可能会遇到问题。但是转换脚本本身会自行工作并获取图像数据,调整大小并返回图像,问题在于用当前脚本实现它。

如果我确实替换了这个:

$file = $_FILES['Filedata']

有了这个:

$file = JRequest::getVar( 'Filedata', '', 'files', 'array' );

它会检索相同的图像数据吗?

我通常可以做很多这样的事情,虽然这需要我一段时间,但这个让我卡住了。我还没有达到我可以知道如何使它工作的水平,我必须通过反复试验和研究和示例来做到这一点,这很困难。

我尝试将以下内容放在检查伪造注释下并删除 $file = ...。然后将其余的 $file 字符串更改为 $newfile,但没有运气,它要么返回错误,要么忽略调整大小脚本,取决于我是否删除 $file =... 等

// Target dimensions
$max_width = 240;
$max_height = 180;
// Get current dimensions
$old_width = imagesx($file);
$old_height = imagesy($file);
// Calculate the scaling we need to do to fit the image inside our frame
$scale   = min($max_width/$old_width, $max_height/$old_height);
// Get the new dimensions
$new_width = ceil($scale*$old_width);
$new_height = ceil($scale*$old_height);
// Create new empty image
$new = imagecreatetruecolor($new_width, $new_height);
// Resize old image into new
imagecopyresampled($new, file,
0, 0, 0, 0,
$new_width, $new_height, $old_width, $old_height);
// Catch the imagedata
ob_start();
imagejpeg($new, NULL, 90);
$filenew = ob_get_clean();
// Destroy resources
imagedestroy($file);
imagedestroy($new);
4

0 回答 0