1

我认为此功能没有应有的效率。我很感激一些关于如何将其构建得更快并占用更少内存的建议。这就是代码的作用:

  1. 检查图像是否已上传
  2. 将有关它的详细信息(标签、名称、详细信息)添加到数据库
  3. 如果设置了变量 $orientation,则旋转图像
  4. 如果图像宽于 600 像素,请调整其大小
  5. 创建缩略图

我认为效率低下的原因在于将步骤 3、4、5 全部分开。有什么方法可以巩固它们吗?谢谢!

 function insert()
  {
    $config['upload_path'] = './uploads/';
    $config['allowed_types'] = 'jpg';
    $config['max_size'] = '5000';
    $config['max_width']  = '4096';
    $config['max_height']  = '4096';

    $this->load->library('upload', $config);

    if (!$this->upload->do_upload())
    {
      $data = array('error' => $this->upload->display_errors());

      $data['title'] = "Add Photo | Mark The Dark";
      $this->load->view('photo_add_view', $data);
    }   
    else
    {
      //get uploaded image info
      $data = array('upload_data' => $this->upload->data());

      //clean the data
      $data = $this->input->xss_clean($data);

      //get orientation info and erase it from POST variable
      //$orientation = $_POST['orientation'];
      //unset($_POST['orientation']);

      //grab the tags
      $tags = $_POST['tags'];
      unset($_POST['tags']);

      //add in some other stuff
      $_POST['time'] = date('YmdHis');
      $_POST['author'] = $this->dx_auth->get_user_id();

      //insert it in the database
      $this->db->insert('photos', $_POST);
      $photo_id = $this->db->insert_id();

      //add stuff to tags table
      /*
      $tags_array = preg_split('/[\s,;]+/', $tags);
      foreach($tags_array as $tag)
      {
        if($tag != "" || $tag != null)
          $this->db->insert('tags', array('id' => $photo_id, 'word' => $tag));
      }*/

      //CXtags
      /*$tags_array = preg_split('/[\s,;]+/', $tags);
      foreach($tags_array as $tag)
      {
        if($tag == "" || $tag == null)
        {unset($tags_array[$tag]);}
      }
      */
      $tags_array = $this->CXTags->comma_to_array($tags);
      foreach($tags_array as $tag)
      {$tags_array[$tag] = $this->CXTags->make_safe_tag($tag);}
      $topass = array(
        'table' => 'photos',
        'tags' => $tags_array,
        'row_id' => $photo_id,
        'user_id' => $_POST['author']
      );
      $this->CXTags->add_tags($topass);

      //rename the file to the id of the record in the database
      rename("./uploads/" . $data['upload_data']['file_name'], "./uploads/" . $photo_id . ".jpg");

      list($width, $height, $type, $attr) = getimagesize("./uploads/" . $photo_id . '.jpg');


      if (($orientation == 1) || ($orientation == 2))
      {
        //echo $orientation;
        //rotate image
        $config['image_library'] = 'GD2';
        $config['source_image'] = './uploads/' . $photo_id . '.jpg';
        if ($orientation == 1)
        {
          $config['rotation_angle'] = 270;
        }
        elseif ($orientation == 2)
        {
          $config['rotation_angle'] = 90;
        }
        $this->load->library('image_lib', $config);
        $this->image_lib->initialize($config);
        if(!$this->image_lib->rotate())
        {
          echo $this->image_lib->display_errors();
        }
      }


      $this->load->library('image_lib');
      if ($width > 600)
      {
        //resize image
        $config['image_library'] = 'GD2';
        $config['source_image'] = './uploads/' . $photo_id . '.jpg';
        $config['new_image'] = './uploads/photos/' . $photo_id . '.jpg';
        $config['create_thumb'] = FALSE;
        $config['maintain_ratio'] = TRUE;
        $config['width'] = 600;//180
        $config['height'] = 480;
        $config['master_dim'] = 'width';
        $this->image_lib->initialize($config);
        $this->load->library('image_lib', $config);
        if(!$this->image_lib->resize())
        {
          echo $this->image_lib->display_errors();
        }
      }
      else
      {

        $source = './uploads/' . $photo_id . '.jpg';
        $destination = './uploads/photos/' . $photo_id . '.jpg';
        rename($source, $destination);
      /*//buggy php???
        $result = copy($source, $destination);
        echo "HO" . $result;
        */

      }

      //create thumbnail
      $config['image_library'] = 'GD2';
      $config['source_image'] = './uploads/photos/' . $photo_id . '.jpg';
      $config['new_image'] = './uploads/thumbnails/' . $photo_id . '.jpg';
      $config['create_thumb'] = TRUE;
      $config['thumb_marker'] = '_thumb';
      $config['maintain_ratio'] = TRUE;
      $config['width'] = 180;//180
      $config['height'] = 100;
      $config['master_dim'] = 'width';
      $this->image_lib->initialize($config);
      $this->load->library('image_lib', $config);
      $this->image_lib->resize();

      redirect('photo/show/' . $photo_id);

    }

    //redirect('photo/photo_add/');
  }
4

3 回答 3

2

对于使用代码 igniter 为您公开的对象进行图像处理所需的所有工作,您可能希望自己严格使用 php.ini 中的 gd 函数来执行此操作。我没有用过code igniter,但是我在php中做了很多关于图像处理的东西,而且不是很困难。

仅通过查看您的代码,并且我假设这是代码点火器方式,我看到您调用image_lib->initialize()load->library()为每个单独的操作。我会查看这些方法以及您正在使用的方法rotate()resize()方法,看看它们是否在每次操作时创建和破坏图像资源。如果您使用 gd 库,则可以在每个步骤中重复使用相同的图像资源,然后在准备好时将其写入文件(重复使用相同的图像资源来创建缩略图)。这可能会在执行速度和使用的内存方面带来巨大的性能提升。

PHP 中的 GD 函数

于 2009-01-14T23:00:08.567 回答
2

好吧,我可以回答您的部分问题-您可以exif使用Codeigniter旋转带有旋转的图像。

首先: 检测上传的图片exif数据。
查看$exif数组并处理 Orientation 项。
传递所需的旋转角度以codeigniters image_lib->rotate()发挥作用。

例如:

public function auto_rotate_image($upload_data){//iphone rotation fix

        $path = $upload_data['full_path'];

        $exif = exif_read_data($path);
        if(isset($exif['Orientation'])){
            $rotate = false;
            switch($exif['Orientation']){//only really interested in the rotation
                case 1: // nothing
                break;
                case 2:// horizontal flip
                break;     
                case 3: // 180 rotate left
                    $rotate = 180;
                break;   
                case 4: // vertical flip
                break;
                case 5: // vertical flip + 90 rotate right
                break;
                case 6: // 90 rotate right
                    $rotate = 270;
                break;
                case 7: // horizontal flip + 90 rotate right
                break; 
                case 8: // 90 rotate left
                    $rotate = 90;
                break;
            }

            if($rotate){

                $config=array();
                $config['image_library'] = 'gd2';
                $config['source_image'] = $path;
                $config['rotation_angle'] = $rotate;
                $config['overwrite'] = TRUE;
                $this->load->library('image_lib',$config);

                if(!$this->image_lib->rotate()){
                    echo $this->image_lib->display_errors();
                }
            }
        }
    }
于 2012-06-27T23:55:58.807 回答
0

您可以根据codeigniter 文档将您的配置存储在其他地方,这将收紧代码。我不知道你怎么能把这些事件串起来。

于 2009-01-14T22:34:06.107 回答