[英]Codeigniter - Multiple Resize Image not working
我嘗試清除,取消設置以將原始圖像調整為不同尺寸的配置,但無濟於事,Under是我當前的控制器代碼。 當用戶選擇圖像時(上載原始圖像,會創建拇指,並顯示給用戶:(使用AJAX)。當用戶點擊提交表單時,我必須使用不同尺寸的resize()函數調整每個圖像的大小,然后調用upload_news_images() ,沒有錯誤顯示。
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class DataHandler extends CI_Controller {
public function insert_news(){
// uploaded images array
$uploaded_images = $this->input->post('uploads');
// move images
foreach($uploaded_images as $img){
$j = 0;
for($k=0; $k<3; $k++){
if($k==0){
$this->resize($img, 176, 176);
}
if($k==1){
$this->resize($img, 360, 360);
}
if($k==2){
$this->resize($img, 1024, 1024);
}
}
//rename("./uploads/tmp/orignals/".$img, "./uploads/images/news/orignals/".$img);
}
exit;
// WE INSERT NEWS DATA SECOND
$this->load->model("News_model");
$news = new News_model();
$news->title = $this->input->post('title');
$news->short_desc = $this->input->post('short_desc');
$news->desc = $this->input->post('description');
$news->pub_date = date("Y/m/d H:i:s");
$news->active = $this->input->post('active');
$tr_id = $this->input->post('tr_id');
if($tr_id == ""){
$news->tr_id = NULL;
}else{
$news->tr_id = $this->input->post('tr_id');
}
$sp_id = $this->input->post('sp_id');
if($sp_id == ""){
$news->sp_id = NULL;
}else{
$news->sp_id = $this->input->post('sp_id');
}
$pl_id = $this->input->post('pl_id');
if($pl_id == ""){
$news->pl_id = NULL;
}else{
$news->pl_id = $this->input->post('pl_id');
}
$city_id = $this->input->post('city_id');
if($city_id == ""){
$news->city_id = NULL;
}else{
$news->city_id = $this->input->post('city_id');
}
$news->save();
$this->session->set_flashdata('i', 1);
redirect(SITE_BASE_URL.'admin/addnews');
}
public function upload_news_images() {
$id=$this->generateRandomString();
$source_image = $_FILES['file']['tmp_name'];
$source_name = $_FILES['file']['name'];
$file_extension = pathinfo($source_name,PATHINFO_EXTENSION);
//echo AAPPATH;exit;
$tmp_upload_path = "./uploads/tmp/orignals/";
$tmp_upload_thumb_path = "./uploads/tmp/thumbs/";
$new_source = $tmp_upload_path.$id.".".$file_extension;
$new_thumb = $tmp_upload_thumb_path.$id.".".$file_extension;
if(file_exists($tmp_upload_path.$source_name)){
unlink($tmp_upload_path.$source_name); //remove the file
}
move_uploaded_file($source_image , $new_source);
$upload_img_arr[] = $new_source;
$upload_img_data = array("upload_img_arr"=>$upload_img_arr);
//$this->session->set_userdata($upload_img_data);
$config['image_library'] = 'gd2';
$config['source_image'] = $new_source;
$config['new_image'] = $new_thumb;
$config['file_permissions'] = 0644;
$config['create_thumb'] = FALSE;
$config['maintain_ratio'] = TRUE;
$config['width'] = 76;
$config['height'] = 76;
$this->load->library('image_lib', $config);
$this->image_lib->initialize();
$this->image_lib->resize();
$this->image_lib->clear();
unset($config);
if (!$this->image_lib->resize()) {
echo $this->image_lib->display_errors();
}
$response = array();
$response['id'] = $id;
$response['thumb_url'] = $new_thumb;
echo json_encode($response);
}
function generateRandomString($length = 63) {
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$charactersLength = strlen($characters);
$randomString = '';
for ($i = 0; $i < $length; $i++) {
$randomString .= $characters[rand(0, $charactersLength - 1)];
}
return $randomString;
}
function resize($img, $width=0, $height=0){
$config['image_library'] = 'gd2';
$config['source_image'] = "./uploads/tmp/orignals/".$img;
if($width==176){
$config['new_image'] = "./uploads/images/news/smalls/".$img;
}else if($width==360){
$config['new_image'] = "./uploads/images/news/mediums/".$img;
}else if($width==1024){
$config['new_image'] = "./uploads/images/news/large/".$img;
}
echo $config['new_image']."<br>";
$config['file_permissions'] = 0644;
$config['create_thumb'] = FALSE;
$config['maintain_ratio'] = TRUE;
$config['width'] = $width;
$config['height'] = $height;
$this->load->library('image_lib', $config);
$this->image_lib->initialize();
$this->image_lib->resize();
$this->image_lib->clear();
unset($config);
if (!$this->image_lib->resize()) {
echo $this->image_lib->display_errors();
}
}
}
我知道了:
$this->image_lib->initialize();
每次使用時都應具有新的配置,例如:
$this->image_lib->initialize($config);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.