I am trying make it so when the user uploads their profile picture it resizes it to a size of 180x180 to fit in the profile picture box. I then want it to also create a thumbnail version of that image so I can use it for posts etc. This is the code I have right now:
function do_upload_profilepicture()
{
$this->load->model('model_users');
$userID = $this->model_users->getUserID($this->session->userdata('username'));
$config['upload_path'] = './img/profilepictures/';
$config['allowed_types'] = 'jpg|png';
$config['overwrite'] = TRUE;
$config['file_name'] = $userID;
$config['max_size'] = '500';
$config['max_width'] = '1920';
$config['max_height'] = '1028';
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload())
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('upload_profilepic_form', $error);
}
else
{
$upload_data = $this->upload->data();
$resize['image_library'] = 'gd2';
$resize['source_image'] = $upload_data['full_path'];
$resize['maintain_ratio'] = FALSE;
$resize['width'] = 180;
$resize['height'] = 180;
$this->load->library('image_lib', $resize);
$this->image_lib->resize();
$this->model_users->setProfilePic($userID, $upload_data['orig_name']);
redirect('upload/create_thumb/' . $upload_data['orig_name']);
}
}
function create_thumb() {
$this->load->model('model_users');
$userID = $this->model_users->getUserID($this->session->userdata('username'));
$imgname = $this->model_users->parseURL($_SERVER['REQUEST_URI'], 1);
$source_path = $imgsrc;
$config_manip = array(
'image_library' => 'gd2',
'source_image' => 'img/profilepictures/' . $imgname,
'new_image' => base_url() . 'img/profilepictures/thumbs/' . $imgname,
'maintain_ratio' => TRUE,
'width' => 50,
'height' => 50
);
$this->load->library('image_lib', $config_manip);
if (!$this->image_lib->resize()) {
echo $this->image_lib->display_errors();
}
// clear //
$this->image_lib->clear();
redirect('userprofile/home/' . $userID);
}
}
What happens is it isnt creating the new file, why is this? Is there an easier way to do this?
one problem which i found in your code you are redirecting user just for creating thumbnail you can do in on step just calling that method in do_upload_profilepicture method and pass image name no need to redirect again and again.
function do_upload_profilepicture()
{
$this->load->model('model_users');
$userID = $this->model_users->getUserID($this->session->userdata('username'));
$config['upload_path'] = './img/profilepictures/';
$config['allowed_types'] = 'jpg|png';
$config['overwrite'] = TRUE;
$config['file_name'] = $userID;
$config['max_size'] = '500';
$config['max_width'] = '1920';
$config['max_height'] = '1028';
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload())
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('upload_profilepic_form', $error);
}
else
{
$upload_data = $this->upload->data();
$resize['image_library'] = 'gd2';
$resize['source_image'] = $upload_data['full_path'];
$resize['maintain_ratio'] = FALSE;
$resize['width'] = 180;
$resize['height'] = 180;
$this->load->library('image_lib', $resize);
$this->image_lib->resize();
$this->image_lib->clear();
$this->model_users->setProfilePic($userID, $upload_data['orig_name']);
$this->create_thumb( $upload_data['orig_name']);
redirect('userprofile/home/' . $userID);
}
}
function create_thumb($imgname) {
$source_path = $imgsrc;
$config_manip = array(
'image_library' => 'gd2',
'source_image' => 'img/profilepictures/' . $imgname,
'new_image' => 'img/profilepictures/thumbs/' . $imgname,
'maintain_ratio' => TRUE,
'width' => 50,
'height' => 50
);
$this->load->library('image_lib', $config_manip);
if (!$this->image_lib->resize()) {
echo $this->image_lib->display_errors();
}
// clear //
$this->image_lib->clear();
}
}
you also using base_url in new_image for thumb you need to give file path not http path. i will suggest you to use this library for creating image on fly in codeigniter codeigniter-advanced-images
Related
currently I'm working with codeigniter framework but I'm confused how to resize the image. I have followed the documentation but I'm still confused. This is my code
public function create(){
//Check login
if(!$this->session->userdata('logged_in')){
redirect('users/login');
}
$data['title'] = 'Create Post';
$data['categories'] = $this->BlogModel->get_categories();
$this->form_validation->set_rules('title','title','required');
$this->form_validation->set_rules('content','content','required');
if($this->form_validation->run() === FALSE){
$this->load->view('frontend/header');
$this->load->view('frontend/navbar');
$this->load->view('frontend/blog/create',$data);
$this->load->view('frontend/footer');
} else{
//Upload Image
$config['upload_path'] = './assets/images/posts/blog';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '2048';
$config['max_width'] = '0';
$config['max_height'] = '0';
$this->load->library('upload', $config);
if(!$this->upload->do_upload()){
$errors = array('error' => $this->upload->display_errors());
$post_image = 'noimage.jpg';
}else{
$data = array('upload_data' => $this->upload->data());
$post_image = $_FILES['userfile']['name'];
}
$this->BlogModel->create_post($post_image);
//set message
$this->session->set_flashdata('post_created','Your post has been created');
redirect('blog');
}
}
Please advice how to resize the image.
Thanks!
If you want to do different things like resize the original image directly, rather than creating a thumb set create_thumb to false, and do not use new_image. The only difference between create_thumb and new_image is that new_image allows you to specify your own path and name. Whereas the most you can do with create_thumb is change the thumb_marker. Which is all in the docs...
//Upload Image
$config['upload_path'] = './assets/images/posts/blog';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '2048';
$config['max_width'] = '0';
$config['max_height'] = '0';
$this->load->library('upload', $config);
if (!$this->upload->do_upload()) {
//$errors = array('error' => $this->upload->display_errors());
exit($this->upload->display_errors());
$post_image = 'noimage.jpg';
} else {
$up_data = $this->upload->data();
$post_image = $up_data['name'];
$config = array(); // clear prev config
$config['image_library'] = 'gd2';
$config['source_image'] = $up_data['full_path'];
$config['create_thumb'] = TRUE;
$config['thumb_marker'] = '_thumbnail';
$config['maintain_ratio'] = TRUE;
$config['width'] = 75;
$config['height'] = 50;
$this->load->library('image_lib', $config);
if(!$this->image_lib->resize()) {
exit($this->image_lib->display_errors());
}
$thumb_path = $up_data['file_path'] . $up_data['raw_name'] . $config['thumb_marker'] . $up_data['file_ext'];
}
The exits you can remove later, but leave them for debugging until you figure out a way to properly message your errors (I suggest session vars). These functions should also be moved to a model.
I have added code like below to add watermark in center of image:
if (!is_dir('assets/uploads_image/')) {
mkdir('assets/uploads_image/', 0777, true);
}
if (!is_dir('assets/uploads_watermark_image/')) {
mkdir('assets/uploads_watermark_image/', 0777, true);
}
$config1['upload_path'] = 'assets/uploads_image/';
$config1['allowed_types'] = 'jpeg|png|jpg|svg';
$this->load->library('upload', $config1);
$this->upload->initialize($config1);
if ( ! $this->upload->do_upload('image')) {
$error = array('error' => $this->upload->display_errors());
$this->session->set_flashdata('error', $error['error']);
redirect('photographer/uploadimage');
}
$config['upload_path'] = 'assets/uploads_watermark_image/';
$config['allowed_types'] = 'jpeg|png|jpg|svg';
$this->load->library('upload', $config);
$this->upload->initialize($config);
$config['image_library'] = 'GD2';
$_FILES['file']['name'] = $_FILES['image']['name'];
$_FILES['file']['type'] = $_FILES['image']['type'];
$_FILES['file']['tmp_name'] = $_FILES['image']['tmp_name'];
$_FILES['file']['error'] = $_FILES['image']['error'];
$_FILES['file']['size'] = $_FILES['image']['size'];
$config['source_image'] = $_FILES['image']['tmp_name'];
$config['wm_type'] = 'overlay';
$config['wm_overlay_path'] = 'assets/img/overlay_watermark.png';
$config['wm_vrt_alignment'] = 'middle';
$config['wm_hor_alignment'] = 'center';
$config['wm_opacity'] = '100';
$this->load->library('image_lib', $config);
$this->image_lib->initialize($config);
$this->image_lib->watermark();
Now I want to modify this code and need to add watermark image into all corners and center of image. Please help me to resolve this issue.
Thanks in advance for your time.
It looks like you are using CodeIgniter 2: https://codeigniter.com/userguide2/libraries/image_lib.html
So it would appear you need to add each watermark one at a time.
Here is your code sample slightly revised to first define all the locations you want watermarks and then a loop to add the watermark to each location (based on the idea that you want the same watermark in each location).
// YOUR SETUP CODE
if (!is_dir('assets/uploads_image/')) {
mkdir('assets/uploads_image/', 0777, true);
}
if (!is_dir('assets/uploads_watermark_image/')) {
mkdir('assets/uploads_watermark_image/', 0777, true);
}
$config1['upload_path'] = 'assets/uploads_image/';
$config1['allowed_types'] = 'jpeg|png|jpg|svg';
$this->load->library('upload', $config1);
$this->upload->initialize($config1);
if ( ! $this->upload->do_upload('image')) {
$error = array('error' => $this->upload->display_errors());
$this->session->set_flashdata('error', $error['error']);
redirect('photographer/uploadimage');
}
$config['upload_path'] = 'assets/uploads_watermark_image/';
$config['allowed_types'] = 'jpeg|png|jpg|svg';
$this->load->library('upload', $config);
$this->upload->initialize($config);
// YOUR WATERMARK SETUP CODE
$config['image_library'] = 'GD2';
$_FILES['file']['name'] = $_FILES['image']['name'];
$_FILES['file']['type'] = $_FILES['image']['type'];
$_FILES['file']['tmp_name'] = $_FILES['image']['tmp_name'];
$_FILES['file']['error'] = $_FILES['image']['error'];
$_FILES['file']['size'] = $_FILES['image']['size'];
$config['source_image'] = $_FILES['image']['tmp_name'];
$config['wm_type'] = 'overlay';
$config['wm_overlay_path'] = 'assets/img/overlay_watermark.png';
$config['wm_opacity'] = '100';
$this->load->library('image_lib', $config);
// DEFINE YOUR WATERMARK LOCATIONS
$watermark_array = array(
array('horizontal' => 'center', 'vertical' => 'middle'),
array('horizontal' => 'left', 'vertical' => 'top'),
array('horizontal' => 'right', 'vertical' => 'top'),
array('horizontal' => 'left', 'vertical' => 'bottom'),
array('horizontal' => 'right', 'vertical' => 'bottom')
);
// ADD YOUR WATERMARKS
foreach ($watermark_array as $row) {
$config['wm_vrt_alignment'] = $row['vertical'];
$config['wm_hor_alignment'] = $row['horizontal'];
$this->image_lib->initialize($config);
$this->image_lib->watermark();
}
I want to upload large images on my website. I want to reduce the size of those using codeigniter. So I am doing this code
function upload_image($data) {
$config['upload_path'] = './temp/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 10000;
$this->load->library('upload', $config);
if (!$this->upload->do_upload('image')) {
$error = array('error' => $this->upload->display_errors());
pre($error);
} else {
$config = array();
$data = array('upload_data' => $this->upload->data());
$config['image_library'] = 'gd';
$config['source_image'] = './temp/' . $data['upload_data']['file_name'];
$config['create_thumb'] = FALSE;
$config['maintain_ratio'] = TRUE;
$config['quality'] = 50;
$config['new_image'] = './temp/' . $data['upload_data']['file_name'];
$this->load->library('image_lib', $config);
$this->image_lib->resize();
pre($data);
}
}
But images are not being compressed. Original and uploaded size are the same. Where am I wrong?
Maybe exists an error. Check for errors:
if (!$this->image_lib->resize()){
echo $this->image_lib->display_errors();
}
Note: Use gd2 as library (default value is gd2):
$config['image_library'] = 'gd2';
I have this controller which inserts the images to upload folder without resizing them.
public Function Upload() {
$this->load->library('upload');
$config['upload_path'] = FCPATH . 'uploads/property-images';
$config['allowed_types'] = 'gif|jpg|png';
$config['file_name'] = 'property_image_1';
$config['max_size'] = '0';
$config['overwrite'] = FALSE;
for ($i = 0; $i < $number_of_files; $i++) {
$_FILES['uploadedimage']['name'] = $files['name'][$i];
$_FILES['uploadedimage']['type'] = $files['type'][$i];
$_FILES['uploadedimage']['tmp_name'] = $files['tmp_name'][$i];
$_FILES['uploadedimage']['error'] = $files['error'][$i];
$_FILES['uploadedimage']['size'] = $files['size'][$i];
$this->upload->initialize($config);
if ($this->upload->do_upload('uploadedimage', $i)) {
$data['uploadedimage'] = $this->upload->data();
$image_name[$i] = $data['uploadedimage']['file_name'];
//$this->_property_images_resize($data); // Private Function For Resize
$data['images'] = implode(',',$image_name);
$this->model_users->insert_property_details($data))
redirect('view');
} else {
$this->form_validation->set_message('fileupload_check', $this->upload->display_errors());
return FALSE;
}
}
}
I further needed to resize the images with the help of a private function in controller:
private function _property_images_resize($data) {
$this->load->library('image_lib');
$config = array(
'image_library' => 'gd2',
'source_image' => 'uploads/property-images/'.$data['uploadedimage']['file_name'],
'new_image' => 'uploads/profile/test1/',
'create_thumb' => TRUE,
'maintain_ratio' => TRUE,
'width' => 10,
'height' => 10
);
$this->image_lib->initialize($config);
if(!$this->image_lib->resize()) echo $this->image_lib->display_errors();
}
After adding this second function, it changes nothing. Images uploads the way they were before without resizing.
Looking for someone's help as can't figure it out.
For validation Callback
function if_image_was_selected(){
$this->form_validation->set_message('if_image_was_selected', 'Please select at least 1 image in jpg/jpeg/png/gif format.');
$number_of_files = count($_FILES['uploadedimages']['tmp_name']);
$files = $_FILES['uploadedimages'];
for($i=0;$i<$number_of_files;$i++) {
if($_FILES['uploadedimages']['error'][$i] != 0) {
return false;
}else{
return true;
}
}
}
Are you sure your not suppose to call:
$this->load->library('image_lib', $config);
instead of
$this->image_lib->initialize($config);
Does your webserver user have write permissions to the uploads/profile/test1/ directory?
Do you get any error message? Have you checked the PHP log or Apache log?
Try adding this code below, above your code to see the error echo'd out to the screen if you can.
error_reporting(E_ALL);
ini_set("display_errors", 1);
I am trying to create a profile thumbnail for user when he upload a profile picture. I amlooking from some assistance here as recently put my hand on Codeiniter and I am new to php too.
Currently this inserts the profile image to 'temp' folder but doesn't resize it. I may be doing it wrong. Do I have to create a new function for thumbnail or I can include it along with the one I have?
I have no problem with adding a new profile picture. replacing the picture and deleting the profile picture automatically when new one is added. Just the resizing(thumbnail) of image.
Here is controller:
public function profile_image() {
if($this->session->userdata('is_logged_in')){
$username = $this->session->userdata('v_member_username');
$url1 = $this->my_profile_model->see_if_old_image_exists($username);
if (empty($_FILES['profile_image']['tmp_name'])) {
return true;
}else{
$url2 = $this->do_upload();
$this->my_profile_model->update_profile_image($url2, $username);
if(!empty($url1)){
$this->my_profile_model->delete_old_profile_image($url1);
}
}
}
}
private function do_upload() {
$type = explode('.', $_FILES['profile_image']['name']);
$type = $type[count($type)-1];
$filename = uniqid(rand()).'.'.$type;
$url2 = './uploads/temp/'.$filename;
if(in_array($type, array('jpeg', 'gif', 'png', 'jpg')))
if (empty($_FILES['profile_image']['tmp_name'])) {
return TRUE;
}else{
if(is_uploaded_file($_FILES['profile_image']['tmp_name']))
if(move_uploaded_file($_FILES['profile_image']['tmp_name'], $url2));
return $url2;
return '';
// do_thumb
$this->load->library('image_lib');
$source_path = $_SERVER['DOCUMENT_ROOT'] . 'uploads/temp/' . $filename;
$target_path = $_SERVER['DOCUMENT_ROOT'] . 'uploads/profile/';
$config_manip = array(
'image_library' => 'gd2',
'source_image' => $source_path,
'new_image' => $target_path,
'maintain_ratio' => TRUE,
'create_thumb' => TRUE,
'thumb_marker' => '_thumb',
'width' => 270,
'height' => 263
);
$this->load->library('image_lib', $config_manip);
if (!$this->image_lib->resize()) {
echo $this->image_lib->display_errors();
}
// clear //
$this->image_lib->clear();
}
}
And my model is:
// Update profile Image
function update_profile_image($url2, $username){
$this->db->set('profile_image', $url2);
$this->db->where('v_member_username', $username);
$this->db->update('vbc_registered_members');
}
// Look If There Was Any Old Image Earlier
function see_if_old_image_exists($username) {
$this->db->select('profile_image');
$this->db->from('vbc_registered_members');
$this->db->where('v_member_username', $username);
$query = $this->db->get();
$query_result = $query->result();
$row = $query_result[0];
return $row->profile_image;
}
// Auto Delete profile Image From Upload Folder On Updating New Image
function delete_old_profile_image($url1) {
unlink($url1);
return TRUE;
}
Please advise.
Codeigniter provides a library for uploading data.
see File Upload Library and Image Library
This is the code I use for uploading images, creating a thumbnail + resizing the image
/*
* This function returns the path of imagemagick on your system
*/
public static function getLibPath()
{
if(strlen(self::$_lib_path)==0)
{
self::$_lib_path=exec("/bin/bash -lc '/usr/bin/which convert'",$out,$rcode);
}
return self::$_lib_path;
}
/*
* This function handles the upload and calls the resizeImage function
* to generate the thumbnail and the resized image
*/
public function update()
{
$config['upload_path'] = 'your upload path';
$config['allowed_types'] = 'jpg|png|bmp';
$config['max_size'] = '8192';//8mb //4096kb - 4mb
$config['max_width'] = '0';
$config['max_height'] = '0';
$config['overwrite'] = false;
$config['encrypt_name']=true;//this generates a filename for you
$this->load->library('upload', $config);
$result = $this->upload->do_upload();
if($result) {
$fileInfo=$this->upload->data();
$this->resizeImage($fileInfo['full_path']);
$data = array(
'filename' => $fileInfo['file_name'],
'orig_file_name' =>$fileInfo['orig_name'],
);
//pseudo function for storing the file info
save_metadata_to_db($data);
}
else
{
echo $this->upload->display_errors());
}
}
/*
* This function creates a thumbnail + the resized image
*/
private function resizeImage($filename)
{
//This function creates a file with the orig. filename + _thumb
$config['image_library'] = 'ImageMagick';
$config['source_image'] = $filename;
$config['create_thumb'] = TRUE;
$config['maintain_ratio'] = FALSE;
$config['width'] = 60;
$config['height'] = 60;
$config['library_path'] = $this->getLibPath();
$config['quality'] = '100%';
$this->load->library('image_lib', $config);
if ( ! $this->image_lib->resize())
{
echo $this->image_lib->display_errors());
}
$config['create_thumb'] = False;
$config['maintain_ratio'] = TRUE;
$config['width'] = 1080;
$config['height'] = 1920;
$this->image_lib->initialize($config);
if ( ! $this->image_lib->resize())
{
echo $this->image_lib->display_errors());
}
}