【问题标题】:After uploading image,create thumb images [closed]上传图片后,创建拇指图像[关闭]
【发布时间】:2014-12-23 11:00:15
【问题描述】:

我已经在我的 webroot 文件夹中上传了图片。

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

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

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

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

【问题讨论】:

  • 您想在点击保存后制作缩略图?或者只是在上传之后???
  • 我希望它也出现在编辑页面和当前页面中。雅上传后。它将显示在字段下方。我也更改或删除它。容易地。谢谢..当我编辑该页面时也想要。

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


【解决方案1】:

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

var $components=array('ImageResize');

第二个在Controller/Component文件夹里面粘贴ImageResizeComponent.php文件。

现在 imageResize 组件。根据您在其他问题中的旧代码。上传后。

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

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

//Thumb 代码(在你之前的图片上传代码之后添加如上)

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'];?>"

As $this->request->data['Tour']['varbigimg'];存储图像名称。并且小图像具有相同的名称并上传到不同的文件夹,您只需将拇指图像路径附加到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;
    }
}
?>

【讨论】:

  • 没有得到任何解决方案。现在我的数据不会保存..!根据您的介绍,我在添加操作中添加了代码。在下面的移动文件夹链接中。之后,我还创建了控制器组件。但什么也没发生/谢谢
  • 您是否在为拇指创建的文件夹中获得小图像?文件夹有777权限吗?
  • 哦,在文件夹中获取缩略图,谢谢 :) 现在该做什么
  • 您在 ADD 操作中这样做对吗?是的图像已添加到文件夹。完毕 ! :) 现在还需要什么?您需要显示拇指图像吗?
  • 是的。需要显示拇指图像。当上传完成时。以及编辑页面中的拇指图像。也帮助我编辑页面:) 谢谢先生
猜你喜欢
  • 2011-03-18
  • 2012-03-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-20
  • 2014-11-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多