Jump to content

Save cropped image in different width and height


ara

Recommended Posts

I am using jcrop to crop images.

 

**This is the form that i upload the image and crop.**

 



                    <form id="upload_form" enctype="multipart/form-data" method="post" action="upload.php" onsubmit="return checkForm()">
                    <!-- hidden crop params -->
                    <input type="hidden" id="x1" name="x1" />
                    <input type="hidden" id="y1" name="y1" />
                    <input type="hidden" id="x2" name="x2" />
                    <input type="hidden" id="y2" name="y2" />


                    <div><input type="file" name="image_file" id="image_file" onchange="fileSelectHandler()" /></div>


                    <div class="error"></div>


                    <div class="step2">
                        <h2>Step2: Please select a crop region</h2>
                        <img id="preview" />


                        <div class="info">
                            <label>File size</label> <input type="text" id="filesize" name="filesize" />
                            <label>Type</label> <input type="text" id="filetype" name="filetype" />
                            <label>Image dimension</label> <input type="text" id="filedim" name="filedim" />
                            <label>W</label> <input type="text" id="w" name="w" />
                            <label>H</label> <input type="text" id="h" name="h" />
                        </div>


                        <input type="submit" value="Upload" />
                    </div>
                </form>


 

**upload.php file which upload cropped image to *avatar* directory.**

 



    <?php function uploadImageFile() { // Note: GD library is required for this function


    if ($_SERVER['REQUEST_METHOD'] == 'POST') {
        $iWidth = $iHeight = 200; // desired image result dimensions
        $iJpgQuality = 90;


        if ($_FILES) {


            // if no errors and size less than 250kb
            if (! $_FILES['image_file']['error'] && $_FILES['image_file']['size'] < 250 * 1024) {
                if (is_uploaded_file($_FILES['image_file']['tmp_name'])) {


                    // new unique filename
                    $sTempFileName = 'avatar/' . md5(time().rand());


                    // move uploaded file into cache folder
                    move_uploaded_file($_FILES['image_file']['tmp_name'], $sTempFileName);


                    // change file permission to 644
                    @chmod($sTempFileName, 0644);


                    if (file_exists($sTempFileName) && filesize($sTempFileName) > 0) {
                        $aSize = getimagesize($sTempFileName); // try to obtain image info
                        if (!$aSize) {
                            @unlink($sTempFileName);
                            return;
                        }


                        // check for image type
                        switch($aSize[2]) {
                            case IMAGETYPE_JPEG:
                                $sExt = '.jpg';


                                // create a new image from file 
                                $vImg = @imagecreatefromjpeg($sTempFileName);
                                break;
                            /*case IMAGETYPE_GIF:
                                $sExt = '.gif';


                                // create a new image from file 
                                $vImg = @imagecreatefromgif($sTempFileName);
                                break;*/
                            case IMAGETYPE_PNG:
                                $sExt = '.png';


                                // create a new image from file 
                                $vImg = @imagecreatefrompng($sTempFileName);
                                break;
                            default:
                                @unlink($sTempFileName);
                                return;
                        }


                        // create a new true color image
                        $vDstImg = @imagecreatetruecolor( $iWidth, $iHeight );


                        // copy and resize part of an image with resampling
                        imagecopyresampled($vDstImg, $vImg, 0, 0, (int)$_POST['x1'], (int)$_POST['y1'], $iWidth, $iHeight, (int)$_POST['w'], (int)$_POST['h']);


                        // define a result image filename
                        $sResultFileName = $sTempFileName . $sExt;


                        // output image to file
                        imagejpeg($vDstImg, $sResultFileName, $iJpgQuality);
                        @unlink($sTempFileName);


                        return $sResultFileName;
                    }
                }
            }
        }
    }
    }


      $sImage = uploadImageFile();
      echo '<img src="'.$sImage.'" />';
      ?>


 

My Question:

 

Right now it just upload the cropped image in avatar directory with width and height of 200px.

 

I want to also upload that cropped image in to two other directories 

 

 1. avatar1 with width and height of 500px

 

 2. avatar2 with width and height of 700px

 

Any help will be appreciated.

 

Link to comment
Share on other sites

the following lines from that code are the relevant ones that you would need to call with the three different sets of $iWidth and $iHeight values, along with the appropriate path where you want the imagejpeg() to save the file -

$vDstImg = @imagecreatetruecolor( $iWidth, $iHeight );

// copy and resize part of an image with resampling
imagecopyresampled($vDstImg, $vImg, 0, 0, (int)$_POST['x1'], (int)$_POST['y1'], $iWidth, $iHeight, (int)$_POST['w'], (int)$_POST['h']);

// define a result image filename
$sResultFileName = $sTempFileName . $sExt;

// output image to file
imagejpeg($vDstImg, $sResultFileName, $iJpgQuality);
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.