Live Support, Chat, Upload Control and Rich Text Editor
Welcome to Support forums Sign in | Join | Help |Client Center
in Search

How to resize images upon upload?

Last post 08-07-2012, 7:42 AM by Minisuit. 2 replies.
Sort Posts: Previous Next
  •  07-05-2012, 5:12 AM 74082

    How to resize images upon upload?

    How do you set the gallery to resize images while uploading?
    Using Explorer Layout and Explorer Editor
     
    Thanks for your help,

    JBWebWorks
  •  07-09-2012, 1:28 PM 74106 in reply to 74082

    Re: How to resize images upon upload?

    hi JBWebWorks,
     
    Open file "phpgallery\phpuploader\ajaxuploaderhandler.php", remove all the code of it. Then use the code below in this file.
     
     Setting "$maxwidth" and "$maxheight" are the max values which use to resize the image.
     
    $maxwidth=600;
    $maxheight=400;
     
    <?php require_once "include_phpuploader.php" ?>
    <?php
    set_time_limit(3600);
    $uploader=new PhpUploader();
    $uploader->PreProcessRequest();
    $mvcfile=$uploader->GetValidatingFile();
    if($mvcfile->FileName=="thisisanotvalidfile")
    {
    $uploader->WriteValidationError("My custom error : Invalid file name. ");
    exit(200);
    }
    if( $uploader->SaveDirectory )
    {
    if(!$uploader->AllowedFileExtensions)
    {
    $uploader->WriteValidationError("When using SaveDirectory property, you must specify AllowedFileExtensions for security purpose.");
    exit(200);
    }
    $cwd=getcwd();
    chdir( dirname($uploader->_SourceFileName) );
    if( ! is_dir($uploader->SaveDirectory) )
    {
    $uploader->WriteValidationError("Invalid SaveDirectory ! not exists.");
    exit(200);
    }
    chdir( $uploader->SaveDirectory );
    $wd=getcwd();
    chdir($cwd);
    $targetfilepath=  "$wd/" . $mvcfile->FileName;
    if( file_exists ($targetfilepath) )
    unlink($targetfilepath);
    $mvcfile->CopyTo( $targetfilepath );
    }
    $img=null;
    $ext=pathinfo($mvcfile->FileName,PATHINFO_EXTENSION);
    switch(strtolower($ext))
    {
    case "png":
    $img= imagecreatefrompng($mvcfile->FilePath);
    break;
    case "gif":
    $img= imagecreatefromgif($mvcfile->FilePath);
    break;
    case "jpg":
    case "jpeg":
    default:
    $img= imagecreatefromjpeg($mvcfile->FilePath);
    break;
    }
    $width=imagesx($img);
    $height=imagesy($img);
    $maxwidth=600;
    $maxheight=400;
    if($width>$maxwidth || $height>$maxheight)
    {
    $ww=$width/$maxwidth;
    $hh=$height/$maxheight;
    $factor=max($ww,$hh);
    $wnew=floor($width/$factor);
    $hnew=floor($height/$factor);
    $thumb=imagecreatetruecolor($wnew,$hnew);
    imagecopyresized($thumb,$img,0,0,0,0,$wnew,$hnew,$width,$height);
    imagedestroy($img);
    imagejpeg($thumb,$mvcfile->FilePath);
    imagedestroy($thumb);
    }
    else
    {
    imagedestroy($img);
    }
    $uploader->WriteValidationOK();
    ?>
     
    Regards,
     
    Ken
  •  08-07-2012, 7:42 AM 74355 in reply to 74106

    Re: How to resize images upon upload?

    <?php
    function thumbnail($inputFileName, $maxSize = 100) {
        $info = getimagesize($inputFileName);
        $type = isset($info['type']) ? $info['type'] : $info[2];
        if (!(imagetypes() & $type)) {
            return false;
        }
        $width = isset($info['width']) ? $info['width'] : $info[0];
        $height = isset($info['height']) ? $info['height'] : $info[1];
        // Calculate aspect ratio
        $wRatio = $maxSize / $width;
        $hRatio = $maxSize / $height;
        // Using imagecreatefromstring will automatically detect the file type
        $sourceImage = imagecreatefromstring(file_get_contents($inputFileName));
        // Calculate a proportional width and height no larger than the max size.
        if (($width <= $maxSize) && ($height <= $maxSize)) {
            // Input is smaller than thumbnail, do nothing
            return $sourceImage;
        } elseif (($wRatio * $height) < $maxSize) {
            // Image is horizontal
            $tHeight = ceil($wRatio * $height);
            $tWidth = $maxSize;
        } else {
            // Image is vertical
            $tWidth = ceil($hRatio * $width);
            $tHeight = $maxSize;
        }
        $thumb = imagecreatetruecolor($tWidth, $tHeight);
        if ($sourceImage === false) {
            // Could not load image
            return false;
        }
        // Copy resampled makes a smooth thumbnail
        imagecopyresampled($thumb, $sourceImage, 0, 0, 0, 0, $tWidth, $tHeight, $width, $height);
        imagedestroy($sourceImage);
        return $thumb;
    }
    function imageToFile($im, $fileName, $quality = 80) {
        if (!$im || file_exists($fileName)) {
            return false;
        }
        $ext = strtolower(substr($fileName, strrpos($fileName, '.')));
        switch ($ext) {
            case '.gif':
                imagegif($im, $fileName);
                break;
            case '.jpg':
            case '.jpeg':
                imagejpeg($im, $fileName, $quality);
                break;
            case '.png':
                imagepng($im, $fileName);
                break;
            case '.bmp':
                imagewbmp($im, $fileName);
                break;
            default:
                return false;
        }
        return true;
    }
    ?>
    We Can Also try this code to Resize the Image and upload it.

    Best Regards,
    Navin Patel-Affiliate Manager
    Minisuit Affiliate Program
    Minisuit DOT com
View as RSS news feed in XML
Powered by Community Server (Commercial Edition), by Telligent Systems