codeigniter中的图像调整
我想上传多个图像使用两个文件输入以及调整它们的大小。codeigniter中的图像调整,codeigniter,Codeigniter,我想上传多个图像使用两个文件输入以及调整它们的大小。 一,。原始图像 二,。100 x 100 三,。400 x 200 所有的工作都很好,但是图像库没有正确调整大小。它以原始大小上传图像。我检查了以下内容。 一,。图像库只加载一次。我在uu构造()中加载它。 二,。在初始化之前,请清除映像库$此->图像库->清除() 函数do\u upload($name,$shop\u code){ $files=$\u文件; 如果($name=='pre_photo_array'){ $photo_nam
一,。原始图像
二,。100 x 100
三,。400 x 200
所有的工作都很好,但是图像库没有正确调整大小。它以原始大小上传图像。我检查了以下内容。
一,。图像库只加载一次。我在uu构造()中加载它。
二,。在初始化之前,请清除映像库$此->图像库->清除()
函数do\u upload($name,$shop\u code){
$files=$\u文件;
如果($name=='pre_photo_array'){
$photo_name='pre_photo_array';
}否则{
$photo_name='post_photo_array';
}
$cpt=count($_文件[$name]['name']);
--$cpt;
对于($i=0;$i<$cpt;$i++){
$\u文件[$photo\u name]['name']=$FILES[$photo\u name]['name'][$i];
$\u文件[$photo\u name]['type']=$FILES[$photo\u name]['type'][$i];
$\u文件[$photo\u name]['tmp\u name']=$FILES[$photo\u name]['tmp\u name'][$i];
$\u文件[$photo\u name]['error']=$FILES[$photo\u name]['error'][$i];
$\u文件[$photo\u name]['size']=$FILES[$photo\u name]['size'][$i];
如果($photo\u name=='pre\u photo\u array'){
$config['upload_path']='./assets/images/shop_images/pre_pic/';
}否则{
$config['upload_path']='./assets/images/shop_images/post_pic/';
}
$config['allowed_types']='gif | jpg | png | jpeg';
$config['overwrite']=FALSE;
$config['file_name']=$shop_code.'''.time();
$this->upload->initialize($config);
如果(!$this->upload->do_upload($photo_name)){
$error=array('error'=>$this->upload->display_errors());
$file_path[]='';
}否则{
$upload_data=$this->upload->data();
如果($name=='pre_photo_array'){
$imgdata=exif_read_data('assets/images/shop_images/pre_pic/'。$upload_data['file_name'],'IFD0');
$source_image_path=getimagesize('assets/images/shop_images/pre_pic/'。$upload_data['file_name']);
$source_image='assets/images/shop_images/pre_pic/'。$upload_data['file_name'];
$Pre_thumboil_new_image='assets/images/shop_images/Pre_pic/thumboil/'。$upload_data['file_name'];
$Pre_ppt_new_image='assets/images/shop_images/Pre_pic/ppt/'。$upload_data['file_name'];
}否则{
$imgdata=exif_read_data('assets/images/shop_images/post_pic/'。$upload_data['file_name'],'IFD0');
$source_image_path=getimagesize('assets/images/shop_images/post_pic/'。$upload_data['file_name']);
$source_image='assets/images/shop_images/post_pic/'。$upload_data['file_name'];
$Post_thumboil_new_image='assets/images/shop_images/Post_pic/thumboil/'。$upload_data['file_name'];
$Post_ppt_new_image='assets/images/shop_images/Post_pic/ppt/'。$upload_data['file_name'];
}
$rotation_angle=0;
开关(isset($imgdata['Orientation'])){
案例3:
$rotation_angle='180';
打破
案例6:
$rotation_angle='90';
打破
案例8:
$rotation_angle='90';
打破
}
如果($name=='pre_photo_array'){
$Thumbnail\u new\u image=$Pre\u Thumbnail\u new\u image;
$ppt\U new\u image=$Pre\u ppt\u new\u image;
}否则{
$Thumbnail\u new\u image=$Post\u Thumbnail\u new\u image;
$ppt_new_image=$Post_ppt_new_image;
}
//创建缩略图
$Thumbnail['image_library']='gd2';
$Thumbnail['source\u image']=$source\u image;
$Thumbnail['new_image']=$Thumbnail_new_image;
$Thumbnail['create_thumb']=TRUE;
$Thumbnail['thumb_marker']=FALSE;
$Thumbnail['maintain_ratio']=TRUE;
$Thumbnail['width']=100;
$Thumbnail['height']=100;
$Thumbnail['rotation\u angle']=$rotation\u angle;
$this->image_lib->clear();
$this->image\u lib->initialize($Thumbnail);
//调整图像大小
$this->image_lib->resize();
$this->image_lib->rotate();
//如果有任何问题,请处理
如果(!$this->image\u lib->resize()){
回应“如果”;
echo$this->image_lib->display_errors();退出;
}
if($source\u image\u path[0]>$source\u image\u path[1]){
$width=400;
$height=200;
}否则{
$width=200;
$height=400;
}
//创建PPt图像
$ppt['image_library']='gd2';
$ppt['source\u image']=$source\u image;
$ppt['new_image']=$ppt_new_image;
$ppt['create_thumb']=FALSE;
$ppt['thumb_marker']=FALSE;
$ppt['mainten_ratio']=TRUE;
$ppt['width']=400;
$ppt['height']=200;
$ppt['rotation\u angle']=$rotation\u angle;
$this->image_lib->clear();
$this->image\u lib->initialize($ppt);
//调整图像大小
$this->image_lib->resize();
$this->image_lib->rotate();
//如果有任何问题,请处理
如果(!$this->image\u lib->resize()){
echo$this->image_lib->display_errors();退出;
}
如果($photo\u name=='pre\u photo\u array'){
function do_upload($name,$shop_code) {
$files = $_FILES;
if ($name == 'pre_photo_array') {
$photo_name = 'pre_photo_array';
} else {
$photo_name = 'post_photo_array';
}
$cpt = count($_FILES[$name]['name']);
--$cpt;
for ($i = 0; $i < $cpt; $i++) {
$_FILES[$photo_name]['name'] = $files[$photo_name]['name'][$i];
$_FILES[$photo_name]['type'] = $files[$photo_name]['type'][$i];
$_FILES[$photo_name]['tmp_name'] = $files[$photo_name]['tmp_name'][$i];
$_FILES[$photo_name]['error'] = $files[$photo_name]['error'][$i];
$_FILES[$photo_name]['size'] = $files[$photo_name]['size'][$i];
if ($photo_name == 'pre_photo_array') {
$config['upload_path'] = './assets/images/shop_images/pre_pic/';
} else {
$config['upload_path'] = './assets/images/shop_images/post_pic/';
}
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$config['overwrite'] = FALSE;
$config['file_name'] = $shop_code.'_'.time();
$this->upload->initialize($config);
if (!$this->upload->do_upload($photo_name)) {
$error = array('error' => $this->upload->display_errors());
$file_path[] = ' ';
} else {
$upload_data = $this->upload->data();
if ($name == 'pre_photo_array') {
$imgdata=exif_read_data('assets/images/shop_images/pre_pic/'.$upload_data['file_name'], 'IFD0');
$source_image_path = getimagesize('assets/images/shop_images/pre_pic/'.$upload_data['file_name']);
$source_image = 'assets/images/shop_images/pre_pic/'.$upload_data['file_name'];
$Pre_Thumbnail_new_image = 'assets/images/shop_images/pre_pic/Thumbnail/'.$upload_data['file_name'];
$Pre_ppt_new_image = 'assets/images/shop_images/pre_pic/PPT/'.$upload_data['file_name'];
} else {
$imgdata=exif_read_data('assets/images/shop_images/post_pic/'.$upload_data['file_name'], 'IFD0');
$source_image_path = getimagesize('assets/images/shop_images/post_pic/'.$upload_data['file_name']);
$source_image = 'assets/images/shop_images/post_pic/'.$upload_data['file_name'];
$Post_Thumbnail_new_image = 'assets/images/shop_images/post_pic/Thumbnail/'.$upload_data['file_name'];
$Post_ppt_new_image = 'assets/images/shop_images/post_pic/PPT/'.$upload_data['file_name'];
}
$rotation_angle = 0;
switch(isset($imgdata['Orientation'])) {
case 3:
$rotation_angle='180';
break;
case 6:
$rotation_angle='90';
break;
case 8:
$rotation_angle='90';
break;
}
if($name == 'pre_photo_array') {
$Thumbnail_new_image = $Pre_Thumbnail_new_image;
$ppt_new_image = $Pre_ppt_new_image;
} else {
$Thumbnail_new_image = $Post_Thumbnail_new_image;
$ppt_new_image = $Post_ppt_new_image;
}
// Create Thumbnail
$Thumbnail['image_library'] = 'gd2';
$Thumbnail['source_image'] = $source_image;
$Thumbnail['new_image'] = $Thumbnail_new_image;
$Thumbnail['create_thumb'] = TRUE;
$Thumbnail['thumb_marker'] = FALSE;
$Thumbnail['maintain_ratio'] = TRUE;
$Thumbnail['width'] = 100;
$Thumbnail['height'] = 100;
$Thumbnail['rotation_angle'] = $rotation_angle;
$this->image_lib->clear();
$this->image_lib->initialize($Thumbnail);
// resize image
$this->image_lib->resize();
$this->image_lib->rotate();
// handle if there is any problem
if(!$this->image_lib->resize()) {
echo "if";
echo $this->image_lib->display_errors();exit;
}
if($source_image_path[0] > $source_image_path[1]){
$width = 400;
$height = 200;
}else{
$width = 200;
$height = 400;
}
// Create PPt image
$ppt['image_library'] = 'gd2';
$ppt['source_image'] = $source_image;
$ppt['new_image'] = $ppt_new_image;
$ppt['create_thumb'] = FALSE;
$ppt['thumb_marker'] = FALSE;
$ppt['maintain_ratio'] = TRUE;
$ppt['width'] = 400;
$ppt['height'] = 200;
$ppt['rotation_angle']=$rotation_angle;
$this->image_lib->clear();
$this->image_lib->initialize($ppt);
// resize image
$this->image_lib->resize();
$this->image_lib->rotate();
// handle if there is any problem
if(!$this->image_lib->resize()) {
echo $this->image_lib->display_errors();exit;
}
if($photo_name == 'pre_photo_array') {
$path = 'assets/images/shop_images/pre_pic/';
} else {
$path = 'assets/images/shop_images/post_pic/';
}
$file_path[] = $path.$upload_data['file_name'];
}
}
return $file_path;
}
}
//initialize the path where you want to save your images
function __construct(){
parent::__construct();
//return the full path of the directory
//make sure these directories have read and write permessions
$this->original_path = realpath(APPPATH.'../uploads/original');
$this->resized_path = realpath(APPPATH.'../uploads/resized');
$this->thumbs_path = realpath(APPPATH.'../uploads/thumbs');
}
function do_upload(){
$this->load->library('image_lib');
$config = array(
'allowed_types' => 'jpg|jpeg|gif|png', //only accept these file types
'max_size' => 2048, //2MB max
'upload_path' => $this->original_path //upload directory
);
$this->load->library('upload', $config);
$image_data = $this->upload->data(); //upload the image
//your desired config for the resize() function
$config = array(
'source_image' => $image_data['full_path'], //path to the uploaded image
'new_image' => $this->resized_path, //path to
'maintain_ratio' => true,
'width' => 128,
'height' => 128
);
//this is the magic line that enables you generate multiple thumbnails
//you have to call the initialize() function each time you call the resize()
//otherwise it will not work and only generate one thumbnail
$this->image_lib->initialize($config);
$this->image_lib->resize();
$this->image_lib->clear();
$config = array(
'source_image' => $image_data['full_path'],
'new_image' => $this->thumbs_path,
'maintain_ratio' => true,
'width' => 36,
'height' => 36
);
//here is the second thumbnail, notice the call for the initialize() function again
$this->image_lib->initialize($config);
$this->image_lib->resize();
$this->image_lib->clear();
}