public static function resize($source, $dest, $width, $height)
    {
        if (false === extension_loaded('gd')) {
            throw new \BackBee\Exception\BBException('gd extension is required');
        }

        if (false === is_readable($source)) {
            throw new InvalidArgumentException('Enable to read source file');
        }

        if (false === $size = getimagesize($source)) {
            throw new InvalidArgumentException('Unsupported picture type');
        }

        $source_width = $size[0];
        $source_height = $size[1];
        $mime_type = MimeType::getInstance()->guess($source);

        switch ($mime_type) {
            case 'image/jpeg':
                $source_img = imagecreatefromjpeg($source);
                break;
            case 'image/png':
                $source_img = imagecreatefrompng($source);
                break;
            case 'image/gif':
                $source_img = imagecreatefromgif($source);
                break;
            default:
                throw new InvalidArgumentException('Unsupported picture type');
        }

        if ($source_width < $width && $source_height < $height) {
            // Picture to small, no resize
            return @copy($source, $dest);
        }

        $ratio = min($width / $source_width, $height / $source_height);
        $width = $source_width * $ratio;
        $height = $source_height * $ratio;

        $target_img = imagecreatetruecolor($width, $height);

        if ('image/jpeg' !== $mime_type) {
            // Preserve alpha
            imagecolortransparent($target_img, imagecolorallocatealpha($target_img, 0, 0, 0, 127));
            imagealphablending($target_img, false);
            imagesavealpha($target_img, true);
        }

        imagecopyresampled($target_img, $source_img, 0, 0, 0, 0, $width, $height, $source_width, $source_height);

        switch ($mime_type) {
            case 'image/jpeg':
                return imagejpeg($target_img, $dest);
                break;
            case 'image/png':
                return @imagepng($target_img, $dest);
                break;
            case 'image/gif':
                return @imagegif($target_img, $dest);
                break;
        }
    }
public static function resize($source, $dest, $width, $height)
    {
        if (false === extension_loaded('gd')) {
            throw new \BackBee\Exception\BBException('gd extension is required');
        }

        if (false === is_readable($source)) {
            throw new InvalidArgumentException('Enable to read source file');
        }

        if (false === $size = getimagesize($source)) {
            throw new InvalidArgumentException('Unsupported picture type');
        }

        $source_width = $size[0];
        $source_height = $size[1];
        $mime_type = MimeType::getInstance()->guess($source);

        switch ($mime_type) {
            case 'image/jpeg':
                $source_img = imagecreatefromjpeg($source);
                break;
            case 'image/png':
                $source_img = imagecreatefrompng($source);
                break;
            case 'image/gif':
                $source_img = imagecreatefromgif($source);
                break;
            default:
                throw new InvalidArgumentException('Unsupported picture type');
        }

        if ($source_width < $width && $source_height < $height) {
            // Picture to small, no resize
            return @copy($source, $dest);
        }

        $ratio = min($width / $source_width, $height / $source_height);
        $width = $source_width * $ratio;
        $height = $source_height * $ratio;

        $target_img = imagecreatetruecolor($width, $height);

        if ('image/jpeg' !== $mime_type) {
            // Preserve alpha
            imagecolortransparent($target_img, imagecolorallocatealpha($target_img, 0, 0, 0, 127));
            imagealphablending($target_img, false);
            imagesavealpha($target_img, true);
        }

        imagecopyresampled($target_img, $source_img, 0, 0, 0, 0, $width, $height, $source_width, $source_height);

        switch ($mime_type) {
            case 'image/jpeg':
                return imagejpeg($target_img, $dest);
                break;
            case 'image/png':
                return @imagepng($target_img, $dest);
                break;
            case 'image/gif':
                return @imagegif($target_img, $dest);
                break;
        }
    }
public static function resize ( $ source , $ dest , $ width , $ height ) { if ( false === extension_loaded ( 'gd' ) ) { throw new \ BackBee \ Exception \ BBException ( 'gd extension is required' ) ; } if ( false === is_readable ( $ source ) ) { throw new InvalidArgumentException ( 'Enable to read source file' ) ; } if ( false === $ size = getimagesize ( $ source ) ) { throw new InvalidArgumentException ( 'Unsupported picture type' ) ; } $ source_width = $ size [ 0 ] ; $ source_height = $ size [ 1 ] ; $ mime_type = MimeType :: getInstance ( ) -> guess ( $ source ) ; switch ( $ mime_type ) { case 'image/jpeg' : $ source_img = imagecreatefromjpeg ( $ source ) ; break ; case 'image/png' : $ source_img = imagecreatefrompng ( $ source ) ; break ; case 'image/gif' : $ source_img = imagecreatefromgif ( $ source ) ; break ; default : throw new InvalidArgumentException ( 'Unsupported picture type' ) ; } if ( $ source_width < $ width && $ source_height < $ height ) { // Picture to small, no resize return @ copy ( $ source , $ dest ) ; } $ ratio = min ( $ width / $ source_width , $ height / $ source_height ) ; $ width = $ source_width * $ ratio ; $ height = $ source_height * $ ratio ; $ target_img = imagecreatetruecolor ( $ width , $ height ) ; if ( 'image/jpeg' !== $ mime_type ) { // Preserve alpha imagecolortransparent ( $ target_img , imagecolorallocatealpha ( $ target_img , 0 , 0 , 0 , 127 ) ) ; imagealphablending ( $ target_img , false ) ; imagesavealpha ( $ target_img , true ) ; } imagecopyresampled ( $ target_img , $ source_img , 0 , 0 , 0 , 0 , $ width , $ height , $ source_width , $ source_height ) ; switch ( $ mime_type ) { case 'image/jpeg' : return imagejpeg ( $ target_img , $ dest ) ; break ; case 'image/png' : return @ imagepng ( $ target_img , $ dest ) ; break ; case 'image/gif' : return @ imagegif ( $ target_img , $ dest ) ; break ; } }
Resizes an image and saves it to the provided file path.

@param string $source The filepath of the source image
@param string $dest   The filepath of the target image
@param int    $width
@param int    $height

@return boolean Returns TRUE on success, FALSE on failure

@throws \BackBee\Exception\BBException Occures if gd extension is not loaded
@throws InvalidArgumentException       Occures on unsupported file type or unreadable file source
Resizes an image and saves it to the provided file path .