Codeigniter Image_lib library source_img - codeigniter

Here's my code :
if(isset($_FILES["image"])){
$upload_dir = "files/images/user/profile/";
$ext = end(explode(".", $_FILES["image"]["name"]));
$config['upload_path'] = $upload_dir."original/";
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$config['max_size'] = "2048";
$config['encrypt_name'] = TRUE;
// $config['file_name'] = $this->session->userdata("username").time().".".$ext;
$this->load->library("upload", $config);
if($this->upload->do_upload("image")){
$image = $this->upload->data();
$conf_res["image_library"] = "gd2";
$conf_res["source_img"] = realpath($upload_dir."original/".$image["file_name"]);
$conf_res["new_image"] = $upload_dir."200x200/".$image["file_name"];
$conf_res["create_thumb"] = TRUE;
$conf_res["maintain_ratio"] = TRUE;
$conf_res["width"] = 200;
$conf_res["height"] = 200;
$this->load->library("image_lib", $conf_res);
if($this->image_lib->resize()){
echo "done."; die();
} else {
echo $this->image_lib->display_errors().$conf_res["new_image"].$image["file_name"];
}
} else {
$error = $this->upload->display_errors();
echo $error.$config['upload_path'];
}
}
And the error i'm getting is :
<p>You must specify a source image in your preferences.</p>
<p>Your server does not support the GD function required to process this type of image.</p>
files/images/user/profile/200x200/2606737d1b67a54cfea0a9d4f16ef336.jpg
2606737d1b67a54cfea0a9d4f16ef336.jpg
I've been thinking for about an hour what is wrong with my path. I've even checked phpinfo if it has gd2 installed. I cant find out what is the problem!

Try changing from
$conf_res["source_img"] = realpath($upload_dir."original/".$image["file_name"]);
to this
$conf_res['source_image'] = realpath($upload_dir."original/".$image["file_name"]);
and this also
$this->load->library('image_lib');
$this->image_lib->initialize($conf_res);
See more info here

Related

Can't Resize Photo in Codeigniter

I need to resize photo when uploaded into path, but the photo can't resize, please correct me about my code here:
private function _do_upload()
{
$config['upload_path'] = './uploads/';
$config['allowed_types'] = '*';
$config['file_name'] = slug($this->input->post('judul')).'_'.time();
$config['image_library'] = 'gd2';
$config['quality'] = '20%';
$config['remove_space'] = TRUE;
$config['create_thumb'] = TRUE;
$config['maintain_ratio'] = TRUE;
$config['width'] = 75;
$config['height'] = 50;
$this->load->library('image_lib', $config);
$this->image_lib->initialize($config);
$this->image_lib->resize();
$this->load->library('upload',$config);
if (!$this->upload->do_upload('photo')) {
$this->session->set_flashdata('msg', $this->upload->display_errors('',''));
redirect('backoffice/tambah_video');
}
return $this->upload->data('file_name');
}
Please specify new image path and try to save.
$config['new_image'] = '/path/to/new_image.jpg';
Also make sure that you have GD library enabled.
You need to do resize() after do_upload(), and separate the configs for the image_lib and upload libraries, now they are mixed.
Try this:
private function _do_upload()
{
// step 1: upload
$upload_config['upload_path'] = './uploads/';
$upload_config['allowed_types'] = '*';
$upload_config['file_name'] = slug($this->input->post('judul')).'_'.time();
$this->load->library('upload', $upload_config);
if (!$this->upload->do_upload('photo')) {
$this->session->set_flashdata('msg', $this->upload->display_errors('',''));
redirect('backoffice/tambah_video');
}
// step 2: resize
$resize_config['source_image'] = $this->upload->upload_path . $this->upload->file_name; // uploaded file path
$resize_config['image_library'] = 'gd2';
$resize_config['quality'] = '20%';
$resize_config['remove_space'] = TRUE;
$resize_config['create_thumb'] = TRUE;
$resize_config['maintain_ratio'] = TRUE;
$resize_config['width'] = 75;
$resize_config['height'] = 50;
$this->load->library('image_lib', $resize_config);
$this->image_lib->resize();
return $this->upload->data('file_name');
}
If $resize_config['create_thumb'] = TRUE; the name of the small image will be suffixed _thumb. If you need to overwrite the uploaded image, do $resize_config['create_thumb'] = FALSE;. See also the thumb_marker and new_image configs in the official documentation.

Creating thumbnails of multiple images?

I am uploading max 5 images and i want to create the thumbnails of those 5 images i am successful in uploading and saving image name in database but can't able to make thumbnails.
//controller
$files = $_FILES;
$cpt = count($_FILES['uploadfile']['name']);
for($i=0; $i<$cpt; $i++)
{
$_FILES['uploadfile']['name']= $files['uploadfile']['name'][$i];
$_FILES['uploadfile']['type']= $files['uploadfile']['type'][$i];
$_FILES['uploadfile']['tmp_name']= $files['uploadfile']['tmp_name'][$i];
$_FILES['uploadfile']['error']= $files['uploadfile']['error'][$i];
$_FILES['uploadfile']['size']= $files['uploadfile']['size'][$i];
$this->upload->initialize($this->set_upload_options());
$this->upload->do_upload('uploadfile');
$upload_data = $this->upload->data();
$name_array[] = $upload_data['file_name']; //success till here now inserting in database and creating thumbnails
$fileName = $upload_data['file_name'];
$images[] = $fileName;
}
$fileName = $images;
var_dump($images);
#$form['picture1']=$images[0];
#$form['picture2']=$images[1];
#$form['picture3']=$images[2];
#$form['picture4']=$images[3];
#$form['picture5']=$images[4];
//
private function set_upload_options()
{
// upload an image options
$config = array();
$config['upload_path'] = LARGEPATH; //give the path to upload the image in folder
$config['remove_spaces']=TRUE;
$config['encrypt_name'] = TRUE; // for encrypting the name
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '78000';
$config['overwrite'] = FALSE;
return $config;
}
now i want as 5 images are uploaded in folder at the same time thumbnails of those 5 images are created.
You can use CodeIgniter's Image Manipulation Class for thumbnails creation: check CodeIgniter 3 manual
i found my solution :
private function _makeThumb($source, $filename, $num = 5)
{
$config2['image_library'] = 'gd2';
$config2['source_image'] = $source;
$config2['new_image'] = THUMBPATH.$filename;
$config2['create_thumb'] = TRUE;
$config2['maintain_ratio'] = TRUE;
$config2['width'] = 75;
$config2['height'] = 50;
return $config2;
}
then i can call this function in a loop to create a thumnails

How to make CI image to thumbs

again I Throw a problems. How Can i find new_image (Thumb) name.
In Controller
//Insert image .............................
$config['upload_path'] = './img/placeholders/blog';
$config['allowed_types'] = 'gif|jpg|png';
$config['encrypt_name'] = TRUE;
$config['remove_spaces'] = TRUE;
$this->upload->initialize($config);
$this->upload->do_upload('blog_images');
//Thumb Image-----------------------------
$image_des = $this->upload->data();
$config['image_library'] = 'gd2';
$config['source_image'] = './img/placeholders/blog/' . $image_des['file_name'];
$config['maintain_ratio'] = TRUE;
$config['create_thumb'] = TRUE;
$config['new_image'] = './img/placeholders/blog/thumb/' . $image_des['file_name'];
$config['width'] = 220;
$config['height'] = 140;
$this->image_lib->initialize($config);
$this->image_lib->resize();
$image_dev = $this->upload->data();
// How Can i find new image name.
$data['blog_img'] = 'img/placeholders/' . $image_des['file_name'];
$data['blog_thumbs'] = ?
after convert,
blog_img name is "e7da891c90586f7c4e89ef5724891a15.jpg"
blog_thumbs is "e7da891c90586f7c4e89ef5724891a15_thumb.jpg"
How Can i find new_image (Thumb) name. like $image_des['file_name']
But you already have it right!
$image_des['raw_name'] = 'e7da891c90586f7c4e89ef5724891a15'; //File name without extension.
$image_des['file_ext'] = '.jpg'; //The file extension with period
$data['blog_thumbs'] = 'your/preceding/path/'.
$image_des['raw_name'].'_thumb'.$image_des['file_ext'];
Refer file uploading class for explanation about return array value of $this->upload->data().

Differentiating between video and image upload in CodeIgniter

I can upload either of image or video from my file. I've set the configuration file for image as follows :
$config['upload_path'] = './docs/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '200';
$config['max_width'] = '250';
$config['max_height'] = '250';
$config['file_name'] = md5(rand().time());
$this -> load -> library('upload', $config);
But when I'll upload video, this configuration will not work as I need to be able to upload much bigger size file ( video ). Is there any way after submitting to find out what I've uploaded ( img/video ) and based on that I can enable the configuration file.
The configuration for video is as follows :
$configVideo['upload_path'] = './docs/';
$configVideo['max_size'] = '50240';
$configVideo['allowed_types'] = 'avi|flv|wmv|mp3|wma';
$configVideo['overwrite'] = FALSE;
$configVideo['remove_spaces'] = TRUE;
$video_name = $date.$_FILES['video']['name'];
$configVideo['file_name'] = $video_name;
$this->load->library('upload', $configVideo);
$this->upload->initialize($configVideo);
You can use $_FILES to find out the type first.
untested
$type = $_FILES['userfile']['type'];
switch ($type) {
case 'gif':
case 'jpg':
case 'png':
// do img config setup
break;
case 'avi':
case 'flv':
case 'wmv':
case 'mp3':
case 'wma':
// do video config
break;
}
Or if you use different names for the files in the form then why not try;
[updated]
$config['upload_path'] = './docs/';
if (is_array($_FILES) && isset($_FILES['image']['name'])) {
// is an image
$config['upload_path'] = './docs/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '200';
$config['max_width'] = '250';
$config['max_height'] = '250';
$config['file_name'] = md5(rand().time());
} elseif (is_array($_FILES) && isset($_FILES['video']['name'])) {
// is a video
$config['max_size'] = '50240';
$config['allowed_types'] = 'avi|flv|wmv|mp3|wma';
$config['overwrite'] = FALSE;
$config['remove_spaces'] = TRUE;
$video_name = $date.$_FILES['video']['name'];
$config['file_name'] = $video_name;
}
$this->load->library('upload', $config);
$this->upload->initialize($config);

How can I modify this image manipulation function done in codeigniter to be more efficient

I think this function isn't as efficient as it should be. I'd appreciate some suggestions on how I can structure it to be faster and take less memory. This is what the code does:
checks to see if the image was uploaded
add details about it (tags, name, details) to the database
if the variable $orientation was set, rotate the image
if the image is wider than 600px, resize it
create a thumbnail
I think the inefficiencies come from having steps 3,4,5 all separate. Is there any way to consolidate them? Thanks!
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/');
}
Well, I can answer part of your question - you can rotate images which carry the exif rotation using Codeigniter.
Firstly:
Detect the uploaded images exif data.
Look at the $exif array and handle the Orientation item.
Pass the required rotation degrees to codeigniters image_lib->rotate() function.
EG:
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();
}
}
}
}
For all the work required to do your image manipulation using the objects that code igniter exposes for you, you may want to look into doing this yourself strictly with the gd functions in php. I have not used code igniter, but I've done a lot of stuff with image manipulation in php, and it's not terribly difficult.
Just by looking at your code, and I'm assuming this is the code igniter way, I see you calling image_lib->initialize() and load->library() for each individual manipulation. I'd have a look inside those methods and the rotate() and resize() methods you're using and see if they're creating and destroying the image resource with each manipulation. If you use the gd library, you can reuse the same image resource for each step and then just write it to file when you're ready (re-use the same image resource to create the thumbnail). This would probably make a huge performance gain in both execution speed and memory used.
GD Functions in PHP
You could store your configs elsewhere per codeigniter documentation, that would tighten the code up. I don't know how you could chain those events.

Resources