简体   繁体   English

在Joomla 2.5中从上传的图像创建缩略图

[英]Creating thumbnail from an uploaded image in Joomla 2.5

I'm really new to joomla, I don't have idea what should I do to make it done. 我真的是joomla的新手,我不知道该怎么做。 I just have this kind of code in administrator table, it refers to uploading files. 我在管理员表中只有这种代码,它是指上传文件。

                //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;

I could upload the file (in this case, an image) from the codes above, but what I'll do next is creating a thumbnail for the uploaded image. 我可以从上面的代码上传文件(在这种情况下为图像),但是接下来我要为上传的图像创建缩略图。 I searched for the php codes through the internet but it doesn't seem to work since I can't synchronize it into joomla codes. 我通过互联网搜索了php代码,但由于无法将其同步到joomla代码中,因此似乎无法正常工作。 Umm.. I've made a folder named thumbnail in images folder. 嗯..我在图像文件夹中创建了一个名为缩略图的文件夹。 So what should I do next? 那我下一步该怎么办?

I'll be so happy and grateful if any of you could help me with this. 如果您能帮助我,我将非常高兴和感激。 Thanks. 谢谢。

Well i can share technique I'm using, i hope it will help: 好吧,我可以分享我正在使用的技术,希望对您有所帮助:

In table's method check after the all validation is done (at the end of the method, just before returning true) i add the following code: 在完成所有验证之后(在方法的结尾,就在返回true之前)在表的方法检查中 ,我添加了以下代码:

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

The i create a new method called storeImage() : 我创建了一个名为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;
}

This method returns uploaded file filename, which table stores in 'image' column. 此方法返回上载的文件名,该表存储在“图像”列中。 It creates two files, one original image and resized thumb with file prefix '_thumb'. 它创建两个文件,一个原始图像,并以文件前缀“ _thumb”调整大小。

I hope it helps :) 希望对您有所帮助:)

I used Jimage : https://api.joomla.org/cms-3/classes/JImage.html 我使用了Jimage: https ://api.joomla.org/cms-3/classes/JImage.html

$JImage = new JImage($img_path);
$size_thumb = '150x150';
$JImage->createThumbs($size_thumb,1,$path.'/thumb');

Short, simple and efficient. 简短,简单而高效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM