Exemple #1
0
/**
 * [add_images description]
 * @param [array] $file [prend la valeur de $_FILES['poster']]
 */
function add_images($file)
{
    /*on regarde le type MIME de l'image et on fait le traitement si le type est jpeg pjpeg ou png*/
    if ($file['type'] == 'image/jpeg' || $file['type'] == 'image/pjpeg' || $file['type'] == 'image/png') {
        /*création de dir2save qui reférence le répertoir de sauvegarde des images*/
        $dir2save = ROOT_DIR . '/assets/img/films';
        /*nom temporel de l'image*/
        $image_source = $file['tmp_name'];
        /*lister les dimensions de l'image*/
        list($width, $height) = getimagesize($image_source);
        /*si l'image est un jpeg ou un pjpeg*/
        if ($file['type'] == 'image/jpeg' || $file['type'] == 'image/pjpeg') {
            // lire l'image d'origine
            $src_image = imagecreatefromjpeg($image_source);
        } else {
            //si l'image est un png
            $src_image = imagecreatefrompng($image_source);
        }
        // définir une nouvelle image avec les dimensions autorisés new_width et new_height
        $new_width = 800;
        //new height= 800/(width/height)
        $new_height = $new_width / ($width / $height);
        //Retourne un identifiant de ressource d'image $dst_image
        $dst_image = ImageCreateTrueColor($new_width, $new_height);
        //retaillement de l'image
        imagecopyResampled($dst_image, $src_image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
        //création d'une image jpeg
        imagejpeg($dst_image, $image_source);
        // effacer les zones mémoire
        imagedestroy($src_image);
        imagedestroy($dst_image);
        //decomposition du nom de l'image et son extension
        $path_parts = pathinfo($file["name"]);
        //ajout du timestamp
        $filename = $path_parts['filename'] . "_" . microtime(true) . '.' . $path_parts['extension'];
        //destination de l'image
        $image_dest = "{$dir2save}/{$filename}";
        //vérification que le téléchargement c'est bien réalisé
        if (move_uploaded_file($image_source, $image_dest)) {
            //debug( "Le fichier est valide; il a été téléchargé avec succès. \n" );
            $error = false;
        } else {
            //debug( " PROBLÈME pendant le téléchargement du fichier!!\n" );
            $error = true;
        }
    } else {
        //debug( " PROBLÈME le fichier n'est pas un png!!\n" );
        $error = true;
        $filename = "";
    }
    return array($error, $filename);
}
Exemple #2
0
 public static function createThumbnail($file)
 {
     if (is_int(EncodeExplorer::getConfig('thumbnails_width'))) {
         $max_width = EncodeExplorer::getConfig('thumbnails_width');
     } else {
         $max_width = 200;
     }
     if (is_int(EncodeExplorer::getConfig('thumbnails_height'))) {
         $max_height = EncodeExplorer::getConfig('thumbnails_height');
     } else {
         $max_height = 200;
     }
     if (File::isPdfFile($file)) {
         $image = ImageServer::openPdf($file);
     } else {
         $image = ImageServer::openImage($file);
     }
     if ($image == null) {
         return;
     }
     imagealphablending($image, true);
     imagesavealpha($image, true);
     $width = imagesx($image);
     $height = imagesy($image);
     $new_width = $max_width;
     $new_height = $max_height;
     if ($width / $height > $new_width / $new_height) {
         $new_height = $new_width * ($height / $width);
     } else {
         $new_width = $new_height * ($width / $height);
     }
     if ($new_width >= $width && $new_height >= $height) {
         $new_width = $width;
         $new_height = $height;
     }
     $new_image = ImageCreateTrueColor($new_width, $new_height);
     imagealphablending($new_image, true);
     imagesavealpha($new_image, true);
     $trans_colour = imagecolorallocatealpha($new_image, 0, 0, 0, 127);
     imagefill($new_image, 0, 0, $trans_colour);
     imagecopyResampled($new_image, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
     return $new_image;
 }
Exemple #3
0
function uploadContent()
{
    $upload_dir = $siteRoot . 'uploads/';
    $allowed_ext = array('jpg', 'jpeg', 'JPG', 'JPEG', 'png', 'PNG', 'gif', 'GIF');
    $all_thumbs = json_decode(@file_get_contents($siteRoot . 'pages/all_pics.json'), true);
    if (strtolower($_SERVER['REQUEST_METHOD']) != 'post') {
        exit_status('Error! Wrong HTTP method!');
    }
    if (array_key_exists('pic', $_FILES) && $_FILES['pic']['error'] == 0) {
        $pic = $_FILES['pic'];
        $picNameFix = array("+", "(", ")", "|", "'", "?", " ");
        $picName = str_replace($picNameFix, '', $pic['name']);
        if (!in_array(get_extension($picName), $allowed_ext)) {
            exit_status('Only ' . implode(',', $allowed_ext) . ' files are allowed!');
        }
        // Ensure that file name is unique
        if ($all_thumbs[$picName]) {
            for ($i = 1; $i <= 20; $i++) {
                if ($all_thumbs[$i . '_' . $picName]) {
                } else {
                    $picName = $i . '_' . $picName;
                    break;
                }
            }
        }
        // Move the uploaded file from the temporary directory to the uploads folder,
        // create a thumbnail, and log all pics in json file:
        if (move_uploaded_file($pic['tmp_name'], $upload_dir . $picName)) {
            setContent($picName, $upload_dir . $picName, 'all_pics');
            $type = false;
            function open_image($file)
            {
                //detect type and process accordinally
                global $type;
                $size = getimagesize($file);
                switch ($size["mime"]) {
                    case "image/jpeg":
                        $im = imagecreatefromjpeg($file);
                        //jpeg file
                        break;
                    case "image/gif":
                        $im = imagecreatefromgif($file);
                        //gif file
                        break;
                    case "image/png":
                        $im = imagecreatefrompng($file);
                        //png file
                        break;
                    default:
                        $im = false;
                        break;
                }
                return $im;
            }
            $thumbnailImage = open_image($upload_dir . $picName);
            $w = imagesx($thumbnailImage);
            $h = imagesy($thumbnailImage);
            //calculate new image dimensions (preserve aspect)
            if (isset($_GET['w']) && !isset($_GET['h'])) {
                $new_w = $_GET['w'];
                $new_h = $new_w * ($h / $w);
            } elseif (isset($_GET['h']) && !isset($_GET['w'])) {
                $new_h = $_GET['h'];
                $new_w = $new_h * ($w / $h);
            } else {
                $new_w = isset($_GET['w']) ? $_GET['w'] : 60;
                $new_h = isset($_GET['h']) ? $_GET['h'] : 60;
                if ($w / $h > $new_w / $new_h) {
                    $new_h = $new_w * ($h / $w);
                } else {
                    $new_w = $new_h * ($w / $h);
                }
            }
            $im2 = ImageCreateTrueColor($new_w, $new_h);
            $imageFormat = strtolower(substr(strrchr($picName, "."), 1));
            if ($imageFormat == "gif" || $imageFormat == "png") {
                imagecolortransparent($im2, imagecolorallocatealpha($im2, 0, 0, 0, 127));
                imagealphablending($im2, false);
                imagesavealpha($im2, true);
            }
            imagecopyResampled($im2, $thumbnailImage, 0, 0, 0, 0, $new_w, $new_h, $w, $h);
            //effects
            if (isset($_GET['blur'])) {
                $lv = $_GET['blur'];
                for ($i = 0; $i < $lv; $i++) {
                    $matrix = array(array(1, 1, 1), array(1, 1, 1), array(1, 1, 1));
                    $divisor = 9;
                    $offset = 0;
                    imageconvolution($im2, $matrix, $divisor, $offset);
                }
            }
            if (isset($_GET['sharpen'])) {
                $lv = $_GET['sharpen'];
                for ($i = 0; $i < $lv; $i++) {
                    $matrix = array(array(-1, -1, -1), array(-1, 16, -1), array(-1, -1, -1));
                    $divisor = 8;
                    $offset = 0;
                    imageconvolution($im2, $matrix, $divisor, $offset);
                }
            }
            imagejpeg($im2, $upload_dir . 'thumbs/' . $picName);
            imagegif($im2, $upload_dir . 'thumbs/' . $picName);
            imagepng($im2, $upload_dir . 'thumbs/' . $picName);
            //move_uploaded_file($thumbnailImage, $upload_dir.'thumbs/'.$picName);
            //copy($upload_dir.$picName, $upload_dir.'thumbs/'.$picName);
            exit_status('File was uploaded successfuly!');
        }
    }
    exit_status('Something went wrong with your upload!');
}
 $SourceImageName = $SourceImage['name'];
 $SrcImageUrl = JRoute::_($SrcImageUrlPath . $SourceImageName);
 //		echo '<img src="'.$SrcImageUrl.'">';
 //         $url = 'images/rsgallery/original/'
 //$SourceImagePath = 'images/rsgallery/original/' . $SourceImage['name'];
 $FullPath_original = JPATH_ROOT . $rsgConfig->get('imgPath_original') . '/';
 $SourceImagePath = $FullPath_original . $SourceImage['name'];
 //		echo '$SourceImagePath: ' . $SourceImagePath . '<br>';
 if (!file_exists($SourceImagePath)) {
     continue;
 }
 list($SourceWidth, $SourceHeight) = getimagesize($SourceImagePath);
 $SourceImage = imagecreatefromjpeg($SourceImagePath);
 // image with new size
 $ImgWithNewSize = ImageCreateTrueColor($InsertWidth, $InsertHeight);
 imagecopyResampled($ImgWithNewSize, $SourceImage, 0, 0, 0, 0, $InsertWidth, $InsertHeight, $SourceWidth, $SourceHeight);
 //--- Merge image into target ---------------------------------
 $InsertOffsetX = $ColIdx * $InsertBaseWidth;
 $InsertOffsetY = $RowIdx * $InsertBaseHeight;
 /*
 		echo '$ColIdx: ' . $ColIdx . '<br>';
 		echo '$RowIdx: ' . $RowIdx . '<br>';
 		echo '$ItemIdx: ' . $ItemIdx . '<br>';
 
 		echo '$InsertOffsetX: '.$InsertOffsetX.'<br>';
 		echo '$InsertOffsetY: '.$InsertOffsetY.'<br>';
 */
 //          return GD2::createSquareThumb( $source, $target, $rsgConfig->get('thumb_width') );
 //          return GD2::resizeImage($source, $target, $targetWidth);
 //			img.utils.php
 imagecopymerge($TargetImage, $ImgWithNewSize, $InsertOffsetX, $InsertOffsetY, 0, 0, $InsertWidth, $InsertHeight, 99);
 public function render()
 {
     $destname = $this->destination;
     $constraint = $this->constraint;
     $new_side = $this->size;
     $cropw = $this->cropwidth;
     $croph = $this->cropheight;
     $quality = $this->quality;
     if ($constraint == "h") {
         $new_w = $new_side / $this->height * $this->width;
         $new_h = $new_side;
         $canvasw = $cropw != false ? $cropw : $new_w;
         $canvash = $croph != false ? $croph : $new_h;
         $canvas = imagecreatetruecolor($canvasw, $canvash);
         $x = $cropw == false ? 0 : -(($new_w - $cropw) / 2);
         $y = $croph == false ? 0 : -(($new_h - $croph) / 2);
     } else {
         if ($constraint == "w") {
             $new_h = $new_side / $this->width * $this->height;
             $new_w = $new_side;
             $canvasw = $cropw != false ? $cropw : $new_w;
             $canvash = $croph != false ? $croph : $new_h;
             $canvas = imagecreatetruecolor($canvasw, $canvash);
             $x = $cropw == false ? 0 : -(($new_w - $cropw) / 2);
             $y = $croph == false ? 0 : -(($new_h - $croph) / 2);
         } else {
             if ($constraint == "t") {
                 if ($this->height > $this->width) {
                     $new_h = $new_side / $this->width * $this->height;
                     $new_w = $new_side;
                     $x = 0;
                     $y = -(($new_h - $new_side) / 2);
                 } else {
                     if ($this->height <= $this->width) {
                         $new_w = $new_side / $this->height * $this->width;
                         $new_h = $new_side;
                         $x = -(($new_w - $new_side) / 2);
                         $y = 0;
                     }
                 }
                 $canvas = imagecreatetruecolor($new_side, $new_side);
             } else {
                 $new_h = $this->height;
                 $new_w = $this->width;
                 $x = $y = 0;
                 $canvas = imagecreatetruecolor($new_w, $new_h);
             }
         }
     }
     switch ($this->type) {
         case 'image/jpeg':
             $ic = imagecreatefromjpeg($this->tmp_name);
             break;
         case 'image/gif':
             $ic = imagecreatefromgif($this->tmp_name);
             break;
         case 'image/png':
             $ic = imagecreatefrompng($this->tmp_name);
             break;
     }
     imagecopyResampled($canvas, $ic, $x, $y, 0, 0, $new_w, $new_h, $this->width, $this->height);
     imagejpeg($canvas, $destname, $quality);
     imagedestroy($canvas);
 }
Exemple #6
0
     }
     if ($check_img != 1) {
         //縮圖比例
         $percent = getResizePercent($w, $h, $new_w, $new_h);
         //比預設縮圖小就不用縮
         if ($percent == 1) {
             imagejpeg($image, $save_filename . strtolower(basename($url)));
             imageinterlace($image, 1);
             //--將圖片轉換為漸進式模
             imagejpeg($image);
         } else {
             $new_w = $w * $percent;
             $new_h = $h * $percent;
             //建立縮圖圖片
             $im2 = ImageCreateTrueColor($new_w, $new_h);
             imagecopyResampled($im2, $image, 0, 0, 0, 0, $new_w, $new_h, $w, $h);
             $image = $im2;
             imagejpeg($image, $save_filename . strtolower(basename($url)));
             imageinterlace($image, 1);
             //--將圖片轉換為漸進式模
             imagejpeg($image);
         }
     }
     break;
 case "watermark":
     // 浮水印
     $img_info = getimagesize($_GET["markurl"]);
     $w_width = $img_info['0'];
     $w_height = $img_info['1'];
     $w_mime = $img_info['mime'];
     $pi_w = $_GET["pw"] ? $_GET["pw"] : 0.4;
Exemple #7
0
function ResizeImage($inputFilename, $new_side)
{
    $exten = array_reverse(explode(".", $inputFilename));
    $exten = $exten[0];
    $imagedata = @getimagesize($inputFilename);
    $w = $imagedata[0];
    $h = $imagedata[1];
    if (($h > $w || $h == $w) && $new_side < $h) {
        $new_w = $new_side / $h * $w;
        $new_h = $new_side;
    } elseif ($w > $h && $new_side < $w) {
        $new_h = $new_side / $w * $h;
        $new_w = $new_side;
    } else {
        $new_w = $w;
        $new_h = $h;
    }
    $im2 = @ImageCreateTrueColor($new_w, $new_h);
    $ErrorArray = error_get_last();
    if ($ErrorArray['type'] == 2) {
        $im2 = @ImageCreateFromJpeg("uploadfail.jpg");
    } else {
        if ($exten == "gif" || $exten == "GIF") {
            $image = @ImageCreateFromGif($inputFilename);
        } elseif ($exten == "png" || $exten == "PNG") {
            $image = @ImageCreateFromPng($inputFilename);
        } else {
            $image = @ImageCreateFromJpeg($inputFilename);
        }
        @imagecopyResampled($im2, $image, 0, 0, 0, 0, $new_w, $new_h, $imagedata[0], $imagedata[1]);
    }
    return $im2;
}
 /**
  * The filename must have an extension and it must be one of the following: jpg, gif, png, jpeg, or bmp.
  * If a thumbnail already exists in the source files' directory, this function automatically appends an
  * incremental numeric suffix.
  *
  * If the output filename already exists on disk, this function will revert to auto-creating a thumbnail filename.
  *
  * @param string  $filename        The filename to process
  * @param string  $outputFilename  The name of the file to write to
  * @param string  $outputDirectory The name of the directory that holds our outputfile
  * @param integer $width           The width in pixels of the created thumbnail
  * @param integer $height          The height in pixels of the created thumbnail
  * @param boolean $max             If set to true then the image aspect ratio is preserved
  *                                   with the max length of either width or height being {@link $width} or {@link $height}
  * @param boolean $crop            If set to true, the image will be cropped to the dimensions specified.
  *                                 If set to false, the image will be resized.
  *
  * @return string the name of the file where the thumbnail can be found
  * @throws ThumbnailsException If any of the parameters are invalid.
  *
  * @see generateThumbnail(...)
  */
 protected function generateThumbnail($filename, $outputFilename, $outputDirectory, $width = 0, $height = 0, $max = false, $crop = false, $ext = null, $quality = null)
 {
     if (!is_file($filename)) {
         throw new ThumbnailsException("Source file does not exist '" . $filename . "'");
     }
     $path = pathinfo($filename);
     if (empty($path['extension'])) {
         throw new ThumbnailsException("Source file does not have an extension '" . $filename . "'");
     }
     $originalExt = strtolower(trim($path['extension']));
     if (empty($ext)) {
         $ext = $originalExt;
     }
     if (!in_array($ext, $this->supportedExtensions)) {
         throw new ThumbnailsException("Thumbnail file extension [{$ext}] is not supported");
     }
     if ($outputDirectory != null && !is_dir($outputDirectory)) {
         throw new ThumbnailsException("Output directory does not exist or is not a valid directory '{$outputDirectory}'");
     }
     //parameter validation
     if ($width === 0 && $height === 0) {
         throw new ThumbnailsException("Width and/or height must be specified");
     }
     if (!is_int($width)) {
         throw new ThumbnailsException("Width [{$width}] is not a valid integer");
     }
     if (!is_int($height)) {
         throw new ThumbnailsException("Height [{$height}] is not a valid integer");
     }
     if ($width < 0) {
         throw new ThumbnailsException("Width cannot be negative");
     }
     if ($height < 0) {
         throw new ThumbnailsException("Height cannot be negative");
     }
     if ($max === true && ($width === 0 || $height === 0)) {
         throw new ThumbnailsException("If max is true then width and height must be positive");
     }
     if ($crop === true && ($width === 0 || $height === 0 || $max === true)) {
         throw new ThumbnailsException("If crop is true then width and height must be positive and max must be false");
     }
     if ($outputDirectory == null) {
         $outputDirectory = $path['dirname'];
     } else {
         $outputDirectory = rtrim($outputDirectory, '/');
     }
     //check for existing thumbnail in same directory, increment filename
     $outfile = $outputFilename == null ? "{$path['filename']}{$this->autoNameSuffix}.{$ext}" : $outputFilename;
     $inc = 1;
     while (is_file("{$outputDirectory}/{$outfile}")) {
         $outfile = "{$path['filename']}{$this->autoNameSuffix}-{$inc}.{$ext}";
         $inc++;
     }
     //list($origwidth, $origheight) = getimagesize($filename);
     //build ImageMagick operation
     if ($this->convertMode == 'exec_imagick') {
         if ($max === true) {
             $op = "-resize {$width}x{$height}\\>";
         } else {
             if ($crop === true) {
                 if ($this->imageMagickCompatMode == false) {
                     // As of IM v6.3.8-3 the special resize option flag '^' was added
                     // to make cutting the image to fit easier.
                     $op = "-resize {$width}x{$height}\\>^ -gravity {$this->cropGravity} -crop {$width}x{$height}+0+0 +repage";
                 } else {
                     // Complex trickiness to perform the cut to fit resize
                     // Calculate the thumbnail aspect ratio.
                     // > 1 is a wide thumb
                     // < 1 is a tall thumb
                     // 1 is a square thumb
                     $thumb_aspect_ratio = $width / $height;
                     // Get the dimensions of the image
                     $dimensions = getimagesize($filename);
                     $image_aspect_ratio = $dimensions[0] / $dimensions[1];
                     // Definitions:
                     //  width-crop = Resize the image to the full width of the thumbnail and trim the top and bottom
                     //  height-crop = Resize the image to the full height of the thumbnail and trip the sides
                     // Behavior:
                     // If image_aspect_ratio < thumb_aspect_ratio perform a width-crop
                     // If image_aspect_ratio >= thumb_aspect_ratio perform a height-crop
                     if ($image_aspect_ratio < $thumb_aspect_ratio) {
                         $op = "-resize {$width}x\\> -gravity {$this->cropGravity} -crop {$width}x{$height}+0+0 +repage";
                     } else {
                         $op = "-resize x{$height}\\> -gravity {$this->cropGravity} -crop {$width}x{$height}+0+0 +repage";
                     }
                 }
             } else {
                 if ($height === 0) {
                     $op = "-resize {$width}x\\>";
                 } else {
                     if ($width === 0) {
                         $op = "-resize x{$height}\\>";
                     } else {
                         $op = "-resize {$width}x{$height}!\\>";
                     }
                 }
             }
         }
         $qualityArg = $quality ? '-quality ' . escapeshellarg($quality) : ($qualityArg = '');
         $outPath = escapeshellarg("{$outputDirectory}/{$outfile}");
         //full ImageMagick command; redirect STDERR to STDOUT
         $filename = escapeshellarg($filename);
         $cmd = "{$this->pathToImageMagickConvert} {$filename} {$op} {$qualityArg} {$outPath} 2>&1";
         $retval = 1;
         $output = array();
         $this->Logger->debug("Excecuting [{$cmd}]");
         exec($cmd, $output, $retval);
         if ($retval > 0) {
             throw new ThumbnailsException("Generation failed '" . $cmd . "\n" . implode("\n", $output) . "'");
         }
     } elseif ($this->convertMode == 'pecl_imagick') {
         $image = new Imagick($filename);
         if ($max == true) {
             $image->scaleImage($width, $height, true);
         } elseif ($crop === true) {
             // Because Imagick::cropThumbnailImage() doesn't support different gravities,
             // we need to expand the functionality out here. PITA!
             if ($this->cropGravity == 'center') {
                 $image->cropThumbnailImage($width, $height);
             } else {
                 // Resize full image by default so the smallest edge is
                 // the max width/height
                 if ($image->getImageWidth() > $image->getImageHeight()) {
                     $image->scaleImage(0, $height);
                 } else {
                     $image->scaleImage($width, 0);
                 }
                 // Then crop out the needed section.
                 $image_width = $image->getImageWidth();
                 $image_height = $image->getImageHeight();
                 switch (strtolower($this->cropGravity)) {
                     case 'northwest':
                         $x = $image_width - $width;
                         $y = 0;
                         break;
                     case 'north':
                         $x = $image_width / 2 - $width / 2;
                         $y = 0;
                         break;
                     case 'northeast':
                         $x = 0;
                         $y = 0;
                         break;
                     case 'west':
                         $x = 0;
                         $y = $image_height / 2 - $height / 2;
                         break;
                     case 'east':
                         $x = $image_width - $width;
                         $y = $image_height / 2 - $height / 2;
                         break;
                     case 'southwest':
                         $x = 0;
                         $y = $image_height - $height;
                         break;
                     case 'south':
                         $x = $image_width / 2 - $width / 2;
                         $y = $image_height - $height;
                         break;
                     case 'southeast':
                         $x = $image_width - $width;
                         $y = $image_height - $height;
                         break;
                     default:
                         throw new ThumbnailsException("Unsupported crop gravity: {$this->cropGravity}");
                 }
                 $x = floor($x);
                 $y = floor($y);
                 $image->cropImage($width, $height, $x, $y);
             }
         } elseif ($height === 0) {
             $image->scaleImage($width, $height);
         } elseif ($width === 0) {
             $image->scaleImage($width, $height);
         } else {
             $image->resizeImage($width, $height, Imagick::FILTER_LANCZOS, 1);
         }
         if ($quality) {
             $image->setImageCompressionQuality($quality);
         }
         // Imagick will infer the file format from the filename extension
         $image->writeImage("{$outputDirectory}/{$outfile}");
         $image->clear();
         $image->destroy();
     } elseif ($this->convertMode == 'gd') {
         $origImage = null;
         switch ($originalExt) {
             case 'jpg':
             case 'jpeg':
                 $origImage = imagecreatefromJPEG($filename);
                 break;
             case 'gif':
                 $origImage = imagecreatefromGIF($filename);
                 break;
             case 'png':
                 $origImage = imagecreatefromPNG($filename);
                 break;
             case 'bmp':
                 $origImage = imagecreatefromWBMP($filename);
                 break;
             default:
                 throw new ThumbnailsException('GD does not know how to handle .' . $originalExt . ' files.');
         }
         if (function_exists('imageantialias')) {
             imageantialias($origImage, true);
         }
         $image_attr = getimagesize($filename);
         $image_width = $image_attr[0];
         $image_height = $image_attr[1];
         $dst_x = 0;
         $dst_y = 0;
         $src_x = null;
         $src_y = null;
         $dst_w = null;
         $dst_h = null;
         $src_w = null;
         $src_h = null;
         if ($max === true) {
             // resize to dimensions, preserving aspect ratio
             $src_x = 0;
             $src_y = 0;
             $src_w = $image_width;
             $src_h = $image_height;
             if ($image_width > $image_height) {
                 $dst_w = $width;
                 $dst_h = (int) floor($image_height * ($width / $image_width));
             } else {
                 $dst_h = $height;
                 $dst_w = (int) floor($image_width * ($height / $image_height));
             }
         } else {
             if ($crop === true) {
                 // crop the image with cropGravity
                 $dst_w = $width;
                 $dst_h = $height;
                 // By default, resize the whole image
                 $src_w = $image_attr[0];
                 $src_h = $image_attr[1];
                 $thumb_aspect_ratio = $width / $height;
                 $image_aspect_ratio = $image_attr[0] / $image_attr[1];
                 if ($image_aspect_ratio < $thumb_aspect_ratio) {
                     // width-crop
                     $src_w = $image_attr[0];
                     // original-width
                     $resize_ratio = $image_attr[0] / $width;
                     // original-width / thumbnail-width
                     $src_h = floor($height * $resize_ratio);
                     // thumbnail-height * original-width / thumbnail-width
                 } else {
                     // height-crop
                     $src_h = $image_attr[1];
                     // original-height
                     $resize_ratio = $image_attr[1] / $height;
                     // original-height / thumbnail-height
                     $src_w = floor($width * $resize_ratio);
                     // thumbnail-width * original-height / thumbnail-height
                 }
                 $dst_x = 0;
                 $dst_y = 0;
                 $dst_w = $width;
                 $dst_h = $height;
                 switch (strtolower($this->cropGravity)) {
                     case 'center':
                         $src_x = floor(($image_attr[0] - $src_w) / 2);
                         $src_y = floor(($image_attr[1] - $src_h) / 2);
                         break;
                     case 'northeast':
                         $src_x = 0;
                         $src_y = 0;
                         break;
                     case 'north':
                         $src_x = floor(($image_attr[0] - $src_w) / 2);
                         $src_y = 0;
                         break;
                     case 'south':
                         $src_x = floor($image_attr[0] - $image_width);
                         $src_y = floor($image_attr[1] - $image_height);
                         break;
                     default:
                         throw new ThumbnailsException("Unsupported cropGravity for GD: {$this->cropGravity}");
                 }
             } else {
                 if ($height === 0) {
                     // resize to max width, preserving aspect ratio
                     $src_x = 0;
                     $src_y = 0;
                     $src_w = $image_width;
                     $src_h = $image_height;
                     $dst_w = $width;
                     $dst_h = $image_height * ($width / $image_width);
                 } else {
                     if ($width === 0) {
                         // resize to max height, preserving aspect ratio
                         $src_x = 0;
                         $src_y = 0;
                         $src_w = $image_width;
                         $src_h = $image_height;
                         $dst_h = $height;
                         $dst_w = $image_width * ($height / $image_height);
                     } else {
                         // resize, ignoring aspect ratio
                         $src_x = 0;
                         $src_y = 0;
                         $src_w = $image_width;
                         $src_h = $image_height;
                         $dst_w = $width;
                         $dst_h = $height;
                     }
                 }
             }
         }
         $newImage = imagecreateTrueColor($dst_w, $dst_h);
         //preserve transparency
         $transindex = -1;
         if ($ext == 'gif') {
             imagealphablending($newImage, false);
             $transindex = imagecolortransparent($origImage);
             if ($transindex >= 0) {
                 $transcol = imagecolorsforindex($origImage, $transindex);
                 $transindex = imagecolorallocatealpha($newImage, $transcol['red'], $transcol['green'], $transcol['blue'], 127);
                 imagefill($newImage, 0, 0, $transindex);
             }
         }
         @imagecopyResampled($newImage, $origImage, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h);
         //preserve transparency
         if ($ext == 'gif') {
             if ($transindex >= 0) {
                 imagecolortransparent($newImage, $transindex);
                 for ($y = 0; $y < $dst_h; ++$y) {
                     for ($x = 0; $x < $dst_w; ++$x) {
                         if ((imagecolorat($newImage, $x, $y) >> 24 & 0x7f) >= 100) {
                             imagesetpixel($newImage, $x, $y, $transindex);
                         }
                     }
                 }
                 imagetruecolortopalette($newImage, true, 255);
                 imagesavealpha($newImage, false);
             }
         }
         // echo "<pre>"; var_dump($dst_h); die("</pre>");
         $outfilepath = "{$outputDirectory}/{$outfile}";
         switch ($ext) {
             case 'jpg':
             case 'jpeg':
                 imageJPEG($newImage, $outfilepath, $quality);
                 break;
             case 'gif':
                 imageGIF($newImage, $outfilepath);
                 break;
             case 'png':
                 imagePNG($newImage, $outfilepath, $quality);
                 break;
             case 'bmp':
                 imageWBMP($newImage, $outfilepath);
                 break;
         }
         imagedestroy($newImage);
         imagedestroy($origImage);
     }
     return $outfile;
 }