0

I need to rename image name after upload because my requirement is I need to add id with it so it's possible after store image name in DB.

I have done code for uploading image with and without watermark into different folder but now I want to change it's name like:

$imageName = $imgId.''.randomstring.''.$ext;

I have tried with below code but same image stored in both folder with watermark. I want to store original image in original_image folder and with watermark image in watermark_folder. Please help me to resolve this issue.

$getImgs = $this->photographer_model->get_uploaded_image($result);
                    $getExt = substr($getImgs[0]['img_name'], strrpos($getImgs[0]['img_name'],'.')+0);
                    $randomStr = $this->generate_random_string(20);
                    $fileName  = $result."-".$randomStr."".$getExt;
                    $originalImgPath  = base_url()."assets/original_image/".$fileName;
                    $watermarkImgPath = base_url()."assets/watermark_image/".$fileName;
                    $uploadOrgImg   = 'assets/original_image/'.$fileName;
                    $uploadWaterImg = 'assets/watermark_image/'.$fileName;
                    $updateImg = $this->photographer_model->update_img_path($result, $fileName, $originalImgPath, $watermarkImgPath);
                    if(file_exists('assets/original_image/'.$getImgs[0]['img_name']) || file_exists('assets/watermark_image/'.$getImgs[0]['img_name'])) {
                        unlink('assets/original_image/'.$getImgs[0]['img_name']);
                        unlink('assets/watermark_image/'.$getImgs[0]['img_name']);
                        $config1['upload_path']   = 'assets/original_image/'; 
                        $config1['allowed_types'] = 'jpeg|png|jpg|svg';
                        $config1['file_name']     = $fileName; // set the name here
                        $this->load->library('upload', $config1);
                        $this->upload->initialize($config1);
                        $this->upload->do_upload('image');
                        $config['upload_path']   = 'assets/watermark_image/'; 
                        $config['allowed_types'] = 'jpeg|png|jpg|svg';
                        $config['file_name']     = $fileName; // set the name here
                        $this->load->library('upload', $config);
                        $this->upload->initialize($config);
                        $this->upload->do_upload('image');
                    }
deepak
  • 83
  • 2
  • 7
  • Please refer to this link [ https://stackoverflow.com/questions/35710385/image-missing-and-required-wordpress-amp-structure-doesnt-add-image-attribute ] I hope it will be helpful – Anand Choudhary Sep 18 '17 at 05:03
  • ```$config['encrypt_name']``` use this to rename file automatically while uploading. – kishor10d Sep 18 '17 at 05:24

1 Answers1

0

Update Your Code Below With this

$config1['upload_path']   = 'assets/original_image/'; 
$config1['allowed_types'] = 'jpeg|png|jpg|svg';
$config1['encrypt_name']  = TRUE;

And Update for the second one

$config['upload_path']   = 'assets/watermark_image/'; 
$config['allowed_types'] = 'jpeg|png|jpg|svg';
$config['encrypt_name']  = TRUE;
Vinoth Smart
  • 383
  • 3
  • 13
  • Thanks for your time and suggestion. But my question is something different. My requirement is I want to save image like: imageId.''.randomstring.''.ext. I think image id will come after storing image name into DB. So how we can rename uploaded image ? I have done code to rename image but issue is in both directory save watermark image. – deepak Sep 18 '17 at 16:01
  • Okay fine then Remove $config['encrypt_name'] = TRUE; Then like: imageId.''.randomstring.''.ext. For this concatenate image with the .now(). This will produce current timestamp and diff results – Vinoth Smart Sep 19 '17 at 13:03