After uploading image,create thumb images [closed]

半腔热情 提交于 2019-12-14 04:22:51

问题


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


回答1:


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


来源:https://stackoverflow.com/questions/26601570/after-uploading-image-create-thumb-images

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!