After uploading image,create thumb images

newsurt

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

Pratik

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;
    }
}
?>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

PHP foreach($images as $image) not uploading images

From Dev

Php code to create thumb not working after update

From Dev

how to reload page after uploading images

From Dev

How to add a another image to Thumb of a slider or How to add two thumb images?

From Dev

How to Optimize images after uploading all images to Wordpress Site?

From Dev

Image permissions after uploading for resize

From Dev

Unable to create thumb, image is black

From Dev

Thumb image is slightly on left of thumb

From Dev

wordpress images and fonts not loading after uploading site to production

From Dev

Unable to display images after uploading them in react component

From Dev

Image file size increases after uploading

From Dev

get downloadURL in react native after uploading image

From Dev

Empty image file after uploading to a server

From Dev

livereload - Image is not resize immediately after uploading

From Dev

How to get thumb images as individual slide images?

From Dev

Uploading image using pyimgur to imgur, using images link

From Dev

How to auto-compress Images while uploading image in angularjs

From Dev

Create JPEG thumb image with general fixed header

From Dev

Laravel Image thumb

From Dev

UISlider Thumb Image Pixelated

From Dev

Uploading multiple images with volley?

From Dev

Uploading images to Firebase

From Dev

cakePHP 3.0 uploading images

From Dev

images are uploading on localhost but not on server?

From Dev

Uploading multiple images in codeigniter?

From Dev

Uploading multiple images with paperclip?

From Dev

Uploading multiple images with Django

From Dev

Uploading multiple images in codeigniter?

From Dev

Parse + Jquery uploading images

Related Related

HotTag

Archive