protected function uploadImage(UploadedFile $image)
    {
        $DS = DIRECTORY_SEPARATOR;
        // Max width for uploaded original image
        $maxWidth = 1500;
        $namePart = uniqid();
        $name = $namePart . '.' . $image->extension; // New filename
        $imageFolder = Yii::getAlias($this->_savePathAlias); // Куда загружать изображение
        // Creates new random subdir for images
        $rnddir = static::getRandomDir($imageFolder);
        $fullImagePath = $imageFolder . $DS . $rnddir . $DS . $name; // Full image path
        if ($image->saveAs($fullImagePath)) {
            // Reduce image if image is very large
            $imageComponent = static::getImageComponent();
            $imageInfo = getimagesize($fullImagePath);
            $img_width = $imageInfo[0];
            if ($img_width > $maxWidth) {
                /* @var $image_o Image_GD|Image_Imagick */
                $image_o = $imageComponent->load($fullImagePath);
                $image_o->resize($maxWidth, static::getMaxHeight($maxWidth));
                $image_o->save($fullImagePath);
            }

            // Save original file
            $originalImage = $imageFolder . $DS . $rnddir . $DS . $namePart . '_original.' . $image->extension;
            @copy($fullImagePath, $originalImage);
            // If image successfully saved - make resized copies
            $sizes = $this->getImageSizes();
            $imageInfo = getimagesize($fullImagePath);
            $img_width = $imageInfo[0];
            $img_height = $imageInfo[1];

            // Crop image if set aspectRatio value
            if ($this->_aspectRatio) {
                $isVertical = $img_width < $img_height;

                if (!$isVertical) {
                    $width = $img_height * $this->_aspectRatio;
                    $height = $width / $this->_aspectRatio;
                } else {
                    $height = $img_width / $this->_aspectRatio;
                    $width = $height * $this->_aspectRatio;
                }
                /* @var $image_c Image_GD|Image_Imagick */
                $image_c = $imageComponent->load($fullImagePath);
                $image_c->crop($width, $height);
                $img_width = $width;
                $image_c->save($fullImagePath);
            }

            // If the image is NOT wider than it should be - remove main size from the list of resize sizes
            if ($img_width <= $sizes['']) {
                unset($sizes['']);
            }

            // Do resize
            $rez = static::resizeAndSave($imageFolder . $DS . $rnddir, $name, $sizes);
            // Successfull
            if ($rez === true) {
                return $rnddir . '/' . $name;
            } else {
                // Error - remove resized copies
                foreach ($this->getImageSizes() as $size) {
                    $file_name = $imageFolder . $DS . $rnddir . $DS . static::addPrefixToFile($name, $size);
                    @unlink($file_name);
                }

                return null;
            }
        }

        return null;
    }
protected function uploadImage(UploadedFile $image)
    {
        $DS = DIRECTORY_SEPARATOR;
        // Max width for uploaded original image
        $maxWidth = 1500;
        $namePart = uniqid();
        $name = $namePart . '.' . $image->extension; // New filename
        $imageFolder = Yii::getAlias($this->_savePathAlias); // Куда загружать изображение
        // Creates new random subdir for images
        $rnddir = static::getRandomDir($imageFolder);
        $fullImagePath = $imageFolder . $DS . $rnddir . $DS . $name; // Full image path
        if ($image->saveAs($fullImagePath)) {
            // Reduce image if image is very large
            $imageComponent = static::getImageComponent();
            $imageInfo = getimagesize($fullImagePath);
            $img_width = $imageInfo[0];
            if ($img_width > $maxWidth) {
                /* @var $image_o Image_GD|Image_Imagick */
                $image_o = $imageComponent->load($fullImagePath);
                $image_o->resize($maxWidth, static::getMaxHeight($maxWidth));
                $image_o->save($fullImagePath);
            }

            // Save original file
            $originalImage = $imageFolder . $DS . $rnddir . $DS . $namePart . '_original.' . $image->extension;
            @copy($fullImagePath, $originalImage);
            // If image successfully saved - make resized copies
            $sizes = $this->getImageSizes();
            $imageInfo = getimagesize($fullImagePath);
            $img_width = $imageInfo[0];
            $img_height = $imageInfo[1];

            // Crop image if set aspectRatio value
            if ($this->_aspectRatio) {
                $isVertical = $img_width < $img_height;

                if (!$isVertical) {
                    $width = $img_height * $this->_aspectRatio;
                    $height = $width / $this->_aspectRatio;
                } else {
                    $height = $img_width / $this->_aspectRatio;
                    $width = $height * $this->_aspectRatio;
                }
                /* @var $image_c Image_GD|Image_Imagick */
                $image_c = $imageComponent->load($fullImagePath);
                $image_c->crop($width, $height);
                $img_width = $width;
                $image_c->save($fullImagePath);
            }

            // If the image is NOT wider than it should be - remove main size from the list of resize sizes
            if ($img_width <= $sizes['']) {
                unset($sizes['']);
            }

            // Do resize
            $rez = static::resizeAndSave($imageFolder . $DS . $rnddir, $name, $sizes);
            // Successfull
            if ($rez === true) {
                return $rnddir . '/' . $name;
            } else {
                // Error - remove resized copies
                foreach ($this->getImageSizes() as $size) {
                    $file_name = $imageFolder . $DS . $rnddir . $DS . static::addPrefixToFile($name, $size);
                    @unlink($file_name);
                }

                return null;
            }
        }

        return null;
    }
protected function uploadImage ( UploadedFile $ image ) { $ DS = DIRECTORY_SEPARATOR ; // Max width for uploaded original image $ maxWidth = 1500 ; $ namePart = uniqid ( ) ; $ name = $ namePart . '.' . $ image -> extension ; // New filename $ imageFolder = Yii :: getAlias ( $ this -> _savePathAlias ) ; // Куда загружать изображение // Creates new random subdir for images $ rnddir = static :: getRandomDir ( $ imageFolder ) ; $ fullImagePath = $ imageFolder . $ DS . $ rnddir . $ DS . $ name ; // Full image path if ( $ image -> saveAs ( $ fullImagePath ) ) { // Reduce image if image is very large $ imageComponent = static :: getImageComponent ( ) ; $ imageInfo = getimagesize ( $ fullImagePath ) ; $ img_width = $ imageInfo [ 0 ] ; if ( $ img_width > $ maxWidth ) { /* @var $image_o Image_GD|Image_Imagick */ $ image_o = $ imageComponent -> load ( $ fullImagePath ) ; $ image_o -> resize ( $ maxWidth , static :: getMaxHeight ( $ maxWidth ) ) ; $ image_o -> save ( $ fullImagePath ) ; } // Save original file $ originalImage = $ imageFolder . $ DS . $ rnddir . $ DS . $ namePart . '_original.' . $ image -> extension ; @ copy ( $ fullImagePath , $ originalImage ) ; // If image successfully saved - make resized copies $ sizes = $ this -> getImageSizes ( ) ; $ imageInfo = getimagesize ( $ fullImagePath ) ; $ img_width = $ imageInfo [ 0 ] ; $ img_height = $ imageInfo [ 1 ] ; // Crop image if set aspectRatio value if ( $ this -> _aspectRatio ) { $ isVertical = $ img_width < $ img_height ; if ( ! $ isVertical ) { $ width = $ img_height * $ this -> _aspectRatio ; $ height = $ width / $ this -> _aspectRatio ; } else { $ height = $ img_width / $ this -> _aspectRatio ; $ width = $ height * $ this -> _aspectRatio ; } /* @var $image_c Image_GD|Image_Imagick */ $ image_c = $ imageComponent -> load ( $ fullImagePath ) ; $ image_c -> crop ( $ width , $ height ) ; $ img_width = $ width ; $ image_c -> save ( $ fullImagePath ) ; } // If the image is NOT wider than it should be - remove main size from the list of resize sizes if ( $ img_width <= $ sizes [ '' ] ) { unset ( $ sizes [ '' ] ) ; } // Do resize $ rez = static :: resizeAndSave ( $ imageFolder . $ DS . $ rnddir , $ name , $ sizes ) ; // Successfull if ( $ rez === true ) { return $ rnddir . '/' . $ name ; } else { // Error - remove resized copies foreach ( $ this -> getImageSizes ( ) as $ size ) { $ file_name = $ imageFolder . $ DS . $ rnddir . $ DS . static :: addPrefixToFile ( $ name , $ size ) ; @ unlink ( $ file_name ) ; } return null ; } } return null ; }
Save uploaded image

@param UploadedFile $image

@return string Image field value or NULL if error
Save uploaded image