上传后如何裁剪图片并获取图片网址?

时间:2016-10-13 14:18:32

标签: php codeigniter codeigniter-3

我正在使用codeigniter 3.1 我想在上传后裁剪图像,但裁剪不起作用 裁剪后如何获取图片网址?

if ($_FILES['upload']['size'] > 0) {
       $this->upload->initialize(array( 
                   "upload_path" => $this->upload_path,
                   "encrypt_name" => TRUE,
                   "remove_spaces" => TRUE,
                ));

                $data = $this->upload->data();
                $image = $data['file_name'];

        $this->load->library('image_lib');
        $this->image_lib->initialize(array( 
             "source_image" => $data,
             "new_image" => $this->upload_path. $image,
             "x_axis" => 300,
             "y_axis" => 300
                ));

        $data = $this->upload->data();
        $image = $data['file_name'];

  //Get full path of image

  }

1 个答案:

答案 0 :(得分:1)

在模型中使用这些功能

<LinearLayout
    android:orientation="vertical"
    android:layout_height="match_parent"
    android:layout_margin="5dp"
    android:layout_width="wrap_content">


    <LinearLayout
        android:orientation="horizontal"
        android:layout_height="wrap_content"
        android:layout_width="match_parent">

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:textColor="@color/text_color"
            android:textSize="@dimen/medium_font"
            android:layout_gravity="center"
            android:text="A long text that wraps correctly but then gets cut off"
            android:layout_weight="1" />

        <ImageButton
            android:contentDescription="@string/share_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/share_button"
            android:padding="5dp"
            android:layout_weight="0"
            android:layout_gravity="center"
            android:scaleType="center"
            android:background="@android:color/transparent"
            android:src="@mipmap/ic_menu_share_holo_dark"/>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/button_background"
        android:id="@+id/distances_parent">
        <Spinner
            android:layout_margin="5dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:entries="@array/distance_labels"
            android:id="@+id/distances" />
    </LinearLayout>

    <Space
        android:layout_weight="1"
        android:layout_width="match_parent"
        android:layout_height="0dp" />

    <FrameLayout
        android:background="@color/back_end"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        ...
    </FrameLayout>
</LinearLayout>

在此控制器中调用此功能

<?php 
 class Image_model extends CI_Model {
public function __construct()   {
    parent::__construct();
    $this->load->helper('url');
    $this->load->library('upload');
    $this->load->library('image_lib');
}

public function do_resize($filename)
{

    $source_path =  'uploads/' . $filename;
    $target_path =  'uploads/thumb/'.$filename;

    $config_manip = array(

        'image_library' => 'gd2',
        'source_image' => $source_path,
        'new_image' => $target_path,
        'maintain_ratio' => TRUE,
        'width' => 300,
        'height' => 300
    );
    $this->image_lib->initialize($config_manip);
    $this->load->library('image_lib', $config_manip);


    if (!$this->image_lib->resize()) {
        echo $this->image_lib->display_errors();
        die();
    }
}

public function img_upload()
{
    $config = array(
        'upload_path' => "uploads",
        'allowed_types' => "*",
        'overwrite' => TRUE,
        'max_size' => "5048000", // Can be set to particular file size , here it is 2 MB(2048 Kb)
        'max_height' => "3000",
        'max_width' => "3000"
    );
    $this->upload->initialize($config);
    $this->load->library('upload', $config);

    if($this->upload->do_upload('myFile')) { //<input type="file" name="myFile" />
        $response   =    array('upload_data' => $this->upload->data());
        $this->do_resize($response['upload_data']['file_name']);
        //return $response;
    }
    else{
        $error              =   array('error'=>$this->upload->display_errors());
        print_r($error);die(); 

    }
 }
}