上传图像后,创建拇指图像

时间:2014-10-28 05:41:34

标签: image image-processing file-upload cakephp-2.3

我在我的webroot文件夹中完成了图片上传。

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

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

这是我在视图文件中的添加图片 如果我在edit.ctp中写相同,那么它将不会显示名称。它会再次询问浏览图像。

所以我想如果上传图像然后它以表格形式显示图像。在编辑页面以及添加页面感谢

1 个答案:

答案 0 :(得分:0)

首先在你的控制器类中使用:

var $components=array('ImageResize');

第二个内部控制器/组件文件夹粘贴ImageResizeComponent.php文件。

现在是imageResize组件。根据你的旧代码在其他问题。刚上传后。

//您的旧代码,常规图片上传。

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

//拇指代码(如上所述添加上一个图像上传代码后)

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);

制作文件夹here webroot/courseImages/thumb/ 如果有小图片,请检查该文件夹

// <强>更新 要在“编辑”屏幕上显示图像:

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

作为$ this-&gt; request-&gt; data [&#39; Tour&#39;] [&#39; varbigimg&#39;];存储图像名称。小图像具有相同名称并上传到仅差异文件夹,您只需要将Thumb图像路径追加到img,最后只需在表格中附加图像名称即可获得该游览ID。

以防万一你有什么想法,你可以创建一个ImageResizeComponent.php文件

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