2014-01-06 70 views
1

我对joomla非常陌生,我不知道该怎么做才能完成。我在管理员表中只有这种代码,它指的是上传文件。从Joomla 2.5中上传的图像创建缩略图

   //Support for file field: cover 
      if(isset($_FILES['jform']['name']['cover'])):   
       jimport('joomla.filesystem.file'); 
       jimport('joomla.filesystem.file'); 
       $file = $_FILES['jform']; 

       //Check if the server found any error. 
       $fileError = $file['error']['cover']; 
       $message = ''; 
       if($fileError > 0 && $fileError != 4) { 
        switch ($fileError) : 
         case 1: 
          $message = JText::_('File size exceeds allowed by the server'); 
          break; 
         case 2: 
          $message = JText::_('File size exceeds allowed by the html form'); 
          break; 
         case 3: 
          $message = JText::_('Partial upload error'); 
          break; 
        endswitch; 
        if($message != '') : 
         JError::raiseWarning(500,$message); 
         return false; 
        endif; 
       } 
       else if($fileError == 4){ 
        if(isset($array['cover_hidden'])):; 
         $array['cover'] = $array['cover_hidden']; 
        endif; 
       } 
       else{ 

        //Check for filesize 
        $fileSize = $file['size']['cover']; 
        if($fileSize > 10485760): 
         JError::raiseWarning(500, 'File bigger than 10MB'); 
         return false; 
        endif; 

        //Replace any special characters in the filename 
        $filename = explode('.',$file['name']['cover']); 
        $filename[0] = preg_replace("/[^A-Za-z0-9]/i", "-", $filename[0]); 


        //Add Timestamp MD5 to avoid overwriting 
        $filename = md5(time()) . '-' . implode('.',$filename); 
        $uploadPath = JPATH_ADMINISTRATOR.DIRECTORY_SEPARATOR.'components'.DIRECTORY_SEPARATOR.'com_comic'.DIRECTORY_SEPARATOR.'images'.DIRECTORY_SEPARATOR.$filename; 
        $fileTemp = $file['tmp_name']['cover']; 

        if(!JFile::exists($uploadPath)): 

         if (!JFile::upload($fileTemp, $uploadPath)): 

          JError::raiseWarning(500,'Error moving file'); 

          return false; 

         endif; 

        endif; 
        $array['cover'] = $filename; 
       } 

      endif; 

我可以上传从上面的代码文件(在这种情况下,图像),但我会做下一个是创造上传的图片的缩略图。我通过互联网搜索了php代码,但它似乎不工作,因为我无法将它同步到joomla代码。嗯..我在图像文件夹中创建了一个名为缩略图的文件夹。那么接下来我应该做什么?

如果你们中的任何一位能够帮助我,我会非常高兴和感激。谢谢。

+0

Joomla有一个api(JImage),可以为你处理大小调整。 – Elin

+0

是的,我一直在检查api,我现在正在使用它来检查di3sel的代码。 – AGF

回答

3

嗯,我可以分享我使用的技术,我希望这将有助于:

在所有验证完成后表的方法检查(在方法结束,就返回true前)我添加以下代码:

$input = JFactory::getApplication()->input; 
$files = $input->files->get('jform'); 
if (!is_null($files) && isset($files['image'])) 
    $this->image = $this->storeImage($files['image']); 

第i创建一个名为storeImage(新方法)

protected $_thumb = array('max_w' => 200, 'max_h' => 200); 

private function storeImage($file) { 
    jimport('joomla.filesystem.file'); 

    $filename = JFile::makeSafe($file['name']); 
    $imageSrc = $file['tmp_name']; 
    $extension = strtolower(JFile::getExt($filename)); 
    // You can add custom images path here 
    $imagesPath = JPATH_ROOT . '/media/'; 

    if (in_array($extension, array('jpg', 'jpeg', 'png', 'gif'))) { 
    // Generate random filename 
    $noExt = rand(1000, 9999) . time() . rand(1000, 9999); 
    $newFilename = $noExt . '.' . $extension; 
    $imageDest = $imagesPath . $newFilename; 

    if (JFile::upload($imageSrc, $imageDest)) { 
     // Get image size 
     list($w, $h, $type) = GetImageSize($imageDest); 

     switch ($extension) { 
     case 'jpg': 
     case 'jpeg': 
      $srcRes = imagecreatefromjpeg($imageDest); 
      break; 
     case 'png': 
      $srcRes = imagecreatefrompng($imageDest); 
      break; 
     case 'gif': 
      $srcRes = imagecreatefromgif($imageDest); 
      break; 
     } 

     // Calculating thumb size 
     if($w > $h) { 
      $width_ratio = $this->_thumb['max_w']/$w; 
      $new_width = $this->_thumb['max_w']; 
      $new_height = $h * $width_ratio; 
     } else { 
      $height_ratio = $this->_thumb['max_w']/$h; 
      $new_width = $w * $height_ratio; 
      $new_height = $this->_thumb['max_w']; 
     } 

     $destRes = imagecreatetruecolor($new_width, $new_height); 
     imagecopyresampled($destRes, $srcRes, 0, 0, 0, 0, $new_width, $new_height, $w, $h); 

     // Creating resized thumbnail 
     switch ($extension) { 
      case 'jpg': 
      case 'jpeg': 
      imagejpeg($destRes, $imagesPath . 'thumb_' . $newFilename, 100); 
      break; 
      case 'png': 
      imagepng($destRes, $imagesPath . 'thumb_' . $newFilename, 1); 
      break; 
      case 'gif': 
      imagegif($destRes, $imagesPath . 'thumb_' . $newFilename, 100); 
      break; 
      } 

      imagedestroy($destRes); 

      // Delete old image if it was set before 

      if (($this->image != "") && JFile::exists($imagesPath . $this->image)) { 
     JFile::delete($imagesPath . $this->image); 
      JFile::delete($imagesPath . 'thumb_' . $this->image); 
      } 
      return $newFilename; 
     } 
     } 
    } 
    } 
    return null; 
} 

该方法返回上传的文件文件名,该表存储在'图像'列中。它创建两个文件,一个原始图像和文件前缀'_thumb'调整大小的拇指。

我希望它能帮助:)

+0

@ d3sel你可以使用JImage来简化它吗? https://github.com/elinw/jshell/blob/master/createthumbnails.php 此外,为什么不只是使用/扩展核心JFormFieldMedia?我会小心上传,而不要执行内置的内置危险文件检查,即检查图像中的恶意代码,检查可执行文件。上传文件可能是您可以做的最危险的事情之一。 makeSafe实际上并没有使文件安全,这只是许多使用的第一步。它所做的就是剥离一些非法字符的名称。 – Elin

+0

嗨艾琳,当然你是正确的关于文件检查/等,我只是添加上传表的一部分。我使用JFormFieldMedia :: file,它添加了所有需要的验证规则。 – di3sel

+0

@ di3sel我应该用你的密码替换我的密码吗?还是可以的?另外我现在很困惑,我应该在哪里放置受保护的$ _thumb = array('max_w'=> 200,'max_h'=> 200); >>这个代码? – AGF