0

i have done image uploading .in my webroot folder.

how to upload image in cakephp2.5+ and store it in webroot folder

 echo $this->Form->input('varbigimg', array('type' => 'file'));

this is my add image in view file if i write same in edit.ctp then it will not display name. it will ask browse image again.

so i want if image is uploaded then it display image in form. in edit page as well as in add page thanks

Community
  • 1
  • 1
newsurt
  • 43
  • 1
  • 9
  • You want to make thumb image After clicking on SAVE ? or just aftr uploading ??? – Pratik Joshi Oct 28 '14 at 07:20
  • i want it on also in edit page as well as current page . ya after uploading. it will display below the field. and i also change it or delete it. easily. thanks.. and same also want when i edit that page. – newsurt Oct 28 '14 at 07:29

1 Answers1

0

First inside your controller class use:

var $components=array('ImageResize');

Second inside Controller/Component Folder paste ImageResizeComponent.php file.

Now imageResize component. as per your Old code in other question. Just after Uploading.

//Your old code , Regular image upload.

move_uploaded_file(
      $this->request->data['Tour']['varbigimg']['tmp_name'],
      $oldFile
);

//Thumb code (add After your previous image upload code as above)

define('ROOT_PATH','../../app/');

$newFile = ROOT_PATH.'webroot/courseImages/thumb/'.$fileName; 

$image = new ImageResizeComponent();
$quality = 100; // image resize for thumb
$height = 40;
$width = 60;
$this->ImageResize->resize($oldFile, $newFile, 60,60,$quality);

Make folder here webroot/courseImages/thumb/ And check in that folder if small image is there

//Update To show image on Edit screen :

<img src="app/webroot/courseImages/thumb/<?php echo $this->request->data['Tour']['varbigimg'];?>"

As $this->request->data['Tour']['varbigimg']; stores image name .And small image is having same name And uploaded to just differend folder , You just need to append Thumb image path to img and at the end just append image name in Table for that ID of tour.

Just in case if u dnt hav ImageResizeComponent.php file CREATE it

<?php 
class ImageResizeComponent extends Object {
    public $name = 'Image';
    private $__errors = array();


    function initialize(){}
    function beforeRedirect(){}
    function startup(){}
    function beforeRender(){}
    function shutdown(){}
    // The above functions should be mandatory while using a component. 


    /**
     * Determines image type, calculates scaled image size, and returns resized image. If no width or height is
     * specified for the new image, the dimensions of the original image will be used, resulting in a copy
     * of the original image.
     *
     * @param string $original absolute path to original image file
     * @param string $new_filename absolute path to new image file to be created
     * @param integer $new_width (optional) width to scale new image (default 0)
     * @param integer $new_height (optional) height to scale image (default 0)
     * @param integer $quality quality of new image (default 100, resizePng will recalculate this value)
     *
     * @access public
     *
     * @return returns new image on success, false on failure. use ImageComponent::getErrors() to get an array
     * of errors on failure
     */
    public function resize($original, $new_filename, $new_width = 0, $new_height = 0, $quality = 100) {
        if(!($image_params = getimagesize($original))) {
            $this->__errors[] = 'Original file is not a valid image: ' . $orignal;
            return false;
        }

        $width = $image_params[0];
        $height = $image_params[1];

        if(0 != $new_width && 0 == $new_height) {
            $scaled_width = $new_width;
            $scaled_height = floor($new_width * $height / $width);
        } elseif(0 != $new_height && 0 == $new_width) {
            $scaled_height = $new_height;
            $scaled_width = floor($new_height * $width / $height);
        } elseif(0 == $new_width && 0 == $new_height) { //assume we want to create a new image the same exact size
            $scaled_width = $width;
            $scaled_height = $height;
        } else { //assume we want to create an image with these exact dimensions, most likely resulting in distortion

                if ($width > $height) {
                $percentage = ($new_width / $width);
                } else {
                $percentage = ($new_width / $height);
                }
                //gets the new value and applies the percentage, then rounds the value
                $scaled_width = round($width * $percentage);
                $scaled_height = round($height * $percentage);




            /*$scaled_width = $width;
            $scaled_height = $height;


            if ($width == 0 || $height == 0) {
                $scaled_width= $new_width;
                $scaled_height = $new_width;
            }
            else if ($width > $height) {
                if ($width > $new_width) $scaled_width = $new_width;
            }
            else {
                if ($height > $new_height) $scaled_height = $new_height;
            }*/





        }

        //create image        
        $ext = $image_params[2];
        switch($ext) {
            case IMAGETYPE_GIF:
                $return = $this->__resizeGif($original, $new_filename, $scaled_width, $scaled_height, $width, $height, $quality);
                break;
            case IMAGETYPE_JPEG:
                $return = $this->__resizeJpeg($original, $new_filename, $scaled_width, $scaled_height, $width, $height, $quality);
                break;
            case IMAGETYPE_PNG:
                $return = $this->__resizePng($original, $new_filename, $scaled_width, $scaled_height, $width, $height, $quality);
                break;    
            default:
                $return = $this->__resizeJpeg($original, $new_filename, $scaled_width, $scaled_height, $width, $height, $quality);
                break;
        }

        return $return;
    }

/* Function getErrors
* @param void
* @return error 
*/

    public function getErrors() {
        return $this->__errors;
                }


/*Function __resizeGif
 * @param $original
    * @param $new_filename
    * @param $scaled_width
    * @param $scaled_height
    * @param $width
    * @param $height
    * @return bool
    */
    private function __resizeGif($original, $new_filename, $scaled_width, $scaled_height, $width, $height) {
        $error = false;

        if(!($src = imagecreatefromgif($original))) {
            $this->__errors[] = 'There was an error creating your resized image (gif).';
            $error = true;
        }

        if(!($tmp = imagecreatetruecolor($scaled_width, $scaled_height))) {
            $this->__errors[] = 'There was an error creating your true color image (gif).';
            $error = true;
        }

        if(!imagecopyresampled($tmp, $src, 0, 0, 0, 0, $scaled_width, $scaled_height, $width, $height)) {
            $this->__errors[] = 'There was an error creating your true color image (gif).';
            $error = true;
        }

        if(!($new_image = imagegif($tmp, $new_filename))) {
            $this->__errors[] = 'There was an error writing your image to file (gif).';
            $error = true;
        }

        imagedestroy($tmp);

        if(false == $error) {
            return $new_image;
        }

        return false;
    }

/*Function __resizeJpeg
 * @param $original
    * @param $new_filename
    * @param $scaled_width
    * @param $scaled_height
    * @param $width
    * @param $height
    * @param $quality
    * @return bool
    */
    private function __resizeJpeg($original, $new_filename, $scaled_width, $scaled_height, $width, $height, $quality) {
        $error = false;

        //echo ">>>".$scaled_width.">>>>".$scaled_height.">>>".$width.">>>".$height; die;

        if(!($src = imagecreatefromjpeg($original))) {
            $this->__errors[] = 'There was an error creating your resized image (jpg).';
            $error = true;
        }

        if(!($tmp = imagecreatetruecolor($scaled_width, $scaled_height))) {
            $this->__errors[] = 'There was an error creating your true color image (jpg).';
            $error = true;
        }

        if(!imagecopyresampled($tmp, $src, 0, 0, 0, 0, $scaled_width, $scaled_height, $width, $height)) {
            $this->__errors[] = 'There was an error creating your true color image (jpg).';
            $error = true;
        }

        if(!($new_image = imagejpeg($tmp, $new_filename, $quality))) {
            $this->__errors[] = 'There was an error writing your image to file (jpg).';
            $error = true;
        }

        imagedestroy($tmp);

        if(false == $error) {
            return $new_image;
        }

        return false;
    }

/* Function __resizePng - resize a png image
* @param $original
* @param $new_filename
* @param $scaled_width
* @param $scaled_height
* @param $width
* @param $height
* @param $quality
* @return bool
*/
    private function __resizePng($original, $new_filename, $scaled_width, $scaled_height, $width, $height, $quality) {
        $error = false;
        /**
         * we need to recalculate the quality for imagepng()
         * the quality parameter in imagepng() is actually the compression level, 
         * so the higher the value (0-9), the lower the quality. this is pretty much
         * the opposite of how imagejpeg() works.
         */
        $quality = ceil($quality / 10); // 0 - 100 value
        if(0 == $quality) {
            $quality = 9;
        } else {
            $quality = ($quality - 1) % 9;
        }


        if(!($src = imagecreatefrompng($original))) {
            $this->__errors[] = 'There was an error creating your resized image (png).';
            $error = true;
        }

        if(!($tmp = imagecreatetruecolor($scaled_width, $scaled_height))) {
            $this->__errors[] = 'There was an error creating your true color image (png).';
            $error = true;
        }

        imagealphablending($tmp, false);

        if(!imagecopyresampled($tmp, $src, 0, 0, 0, 0, $scaled_width, $scaled_height, $width, $height)) {
            $this->__errors[] = 'There was an error creating your true color image (png).';
            $error = true;
        }

        imagesavealpha($tmp, true);

        if(!($new_image = imagepng($tmp, $new_filename, $quality))) {
            $this->__errors[] = 'There was an error writing your image to file (png).';
            $error = true;
        }

        imagedestroy($tmp);

        if(false == $error) {
            return $new_image;
        }

        return false;
    }
}
?>
Pratik Joshi
  • 11,485
  • 7
  • 41
  • 73
  • not getting any solution. now my data will not save.. ! as per your intro i added code in add action. in below move folder link. after that i als0 crate controller-component. but nothing happen/ thnks – newsurt Oct 28 '14 at 09:32
  • Are you getting Small image in Folder you created for thumb ?? Does the folder have 777 permission? – Pratik Joshi Oct 28 '14 at 10:19
  • oh k get the thumb image in folder thanks :) now what to do – newsurt Oct 28 '14 at 10:46
  • You are doing it on ADD operation correct? yes image is Added to Folder . Done ! :) now what else is needed ? You need to display thumb image ? – Pratik Joshi Oct 28 '14 at 11:29
  • ya. need to display thumb image. when uploading complete in form. as well as that thumb image in edit page also. help me in edit page too :) thanks sir – newsurt Oct 28 '14 at 11:34
  • do u hav thumb url Field in DBase? – Pratik Joshi Oct 28 '14 at 11:36
  • ya i created field but till not i didnt inserted it . field name is. varthumimg but i also need to add url in field also ok .varthumimg – newsurt Oct 28 '14 at 11:41
  • @newsurt , well i think u dont need Thumb field in table.You already know image name and path to reach ` Thumb` – Pratik Joshi Oct 28 '14 at 11:50
  • k . then what to do next. – newsurt Oct 28 '14 at 11:51
  • @newsurt check **UPDATE** in answer – Pratik Joshi Oct 28 '14 at 11:51
  • k got thumb image in editing. but in this field echo $this->Form->input('varbigimg',array('type' => 'file'));?> it will display that no file selected. so there should name should be must display..!! so. need file name in varbigimagefield and as well as. what in add page.. if image uploaded then it must also on the spot display on add page too..! – newsurt Oct 28 '14 at 12:01
  • `but in this field` Means? Are you getting it properly as thumb in Edit? Is EDIT problem of displaying solved ? IS there issue pending for ADD only? correct? – Pratik Joshi Oct 28 '14 at 12:06
  • edit display thumb image. but it will not display name of current file. like new.jpeg but it display brwose->no file seleted. .so what to write there in view file. – newsurt Oct 28 '14 at 12:11
  • Dude ! :) Check the snapshot https://www.dropbox.com/s/zygwrdfzjl5gn8r/sample.png?dl=0 – Pratik Joshi Oct 28 '14 at 12:17
  • There will be File upload Section . But just besides it , Show OLD upoloaded image , Getting ? – Pratik Joshi Oct 28 '14 at 12:19
  • https://www.dropbox.com/s/63u7il2mhbuixu1/Selection_042.png?dl=0 in image here why it display no image selected. there must be name of that image. rather then no image selected – newsurt Oct 28 '14 at 12:24
  • @newsurt :) haha , You are editing the image.You can change the image , So you have OPTION to change Uploaded image,So it asks you to `choose image file` , if there is no image UPDATE option then how wil you CHANGE image uploaded ?? – Pratik Joshi Oct 28 '14 at 12:58
  • k. got it.i show your image its right :D and plz tell me about edit action in controller. – newsurt Oct 28 '14 at 13:02
  • it will be similar like ADD . Check if image is uploaded , If yes then update DB field for image AND upload New thumb resized image u just uploaded .If no then update all DB fields Except image – Pratik Joshi Oct 28 '14 at 13:10
  • k thanks:) check mail plz. help me on edit function too :) thank you for support :) – hi.... – newsurt Oct 29 '14 at 08:37
  • @newsurt , m in office , see ya in the evening. – Pratik Joshi Oct 29 '14 at 08:39
  • hi in edit. if i select image. then its ok its updated. but in form if i edit other and not image. then it will say..error.. array to string convert. so plz help in edit image thsnk – newsurt Oct 30 '14 at 06:18
  • Then think little logically ,Look in CODE ,and solve issue.I know u can do it. – Pratik Joshi Oct 30 '14 at 06:24
  • i check it in funtion at every where we already used this->request->data so why .i face error `varbigimg` = Array, if i update other but not image. array to string error plz help on it. thanks sir – newsurt Oct 30 '14 at 08:58
  • @newsurt which Cakephp version u use? – Pratik Joshi Oct 30 '14 at 09:03
  • I ANSWERED same Question before Check http://stackoverflow.com/questions/26234304/array-to-string-error-while-uploading-an-image – Pratik Joshi Oct 30 '14 at 09:04
  • @newsurt : Paste your Controller code Here Or in Another Question – Pratik Joshi Oct 30 '14 at 09:08
  • i am using cake 2.5.4 ...and i already set ,array('enctype'=>'multipart/form-data')); but still error – newsurt Oct 30 '14 at 09:26
  • @newsurt : Paste your Controller code Here Or in Another Question – Pratik Joshi Oct 30 '14 at 09:35
  • http://stackoverflow.com/questions/26649845/if-i-upload-image-then-edit-work-fine-but-if-i-update-other-field-and-not-image – newsurt Oct 30 '14 at 09:55