Jump to content

Recommended Posts

<a href="http://nodstrum.com/2006/12/09/image-manipulation-using-php/">Click here for a tutorial</a>

 

There are 4 image manipulations. U can use the 'resize' for your needs, modifying the last two parameters imagecopyresized() which tell where the crop will start. U may consider using imagecopysampled() instead of that to mantain image quality. Hope it helped.

Link to comment
https://forums.phpfreaks.com/topic/63439-possible/#findComment-316165
Share on other sites

I have edited the script I have referred to to resize the image uploaded.

<?php

if(isset($_POST['Submit']))
{
    // define the max width and height of uploaded images
    $max_width  = 600;
    $max_height = 600;

    // define folder in which the images get uploaded to
    $upload_dir = './pics/';

    // get uploaded image data from form
    $file_name = $_FILES['image']['name'];
    $file_tmp  = $_FILES['image']['tmp_name'];
    $file_size = $_FILES['image']['size'];
    $file_type = $_FILES['image']['type'];

    // get info about uploaded image
    list($width, $height, $type, $attr) = getimagesize($file_tmp);

    // set the full upload path for where to upload the image to.
    $img_path = $upload_dir . $file_name;

    // check to see if the uploaded image is bigger tha max width an height
    // if it is we'll crop the image
    if($width > $max_width && $height > $max_height)
    {
        // image is too big so we'll crop it

        // get the aspect ratio of the image
        // we do this so we can keep the aspect ratio when the image gets cropped.
        $aspect_ratio = $height / $width;
        $new_width    = $max_height / $aspect_ratio;

        // create a blank canvas for the size of the image
        // this is what the image will get cropped to
        $img_rs = imagecreatetruecolor($new_width, $max_height);

        // setup a image resource to the uploaded file
        $img = imageCreateFromJPEG($file_tmp);

        // crop the image
        imagecopyresampled($img_rs, $img, 0, 0, 0, 0, $new_width, $max_height, ImageSX($img), ImageSY($img));

        // create the image
        ImageJPEG($img_rs, $img_path);

        // destroy the temporary image
        imagedestroy($img_rs);
    }
    else
    {
        // uploaded image is under the max width and hieght
        // we'll just upload it straight away
        move_uploaded_file($file_tmp, $img_path);
    }

    // display the image
    echo "<h1>Upload Image:</h1>\n\n<img src=\"$img_path\">";
}
else
{
    echo '<form method="POST" action="' . $_SERVER['PHP_SELF'] . '" enctype="multipart/form-data">
    <input type="file" name="image"><p>
<input type="Submit" name="Submit" value="Submit">
</form>';
}

?>

 

That will resize the image if the width and height is greater than 600px. You can change this if you want by editing the following two variables:

$max_width  = 600;
$max_height = 600;

Link to comment
https://forums.phpfreaks.com/topic/63439-possible/#findComment-317027
Share on other sites

Thanks again, But this resizes the image to a set pixel size, I want to be able to crop part of that image:

 

eg: 800x600 px - crop certain area to 300 x 100 pixels with distortion the image.

 

http://www.sephiroth.it/file_detail.php?id=109#

 

This crops the area but using flas, is this possiable just in php?

 

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/63439-possible/#findComment-317334
Share on other sites

Thanks again, But this resizes the image to a set pixel size, I want to be able to crop part of that image:

 

eg: 800x600 px - crop certain area to 300 x 100 pixels with distortion the image.

 

http://www.sephiroth.it/file_detail.php?id=109#

 

This crops the area but using flas, is this possiable just in php?

 

The script you linked to uses php to crop the image, but uses flash to provide a interface so the user can select where to crop the image to. You wont be able to prove an interface, like the flash interface, with just PHP alone.

Link to comment
https://forums.phpfreaks.com/topic/63439-possible/#findComment-317422
Share on other sites

I'd say so yes. Or you can go the javascript way. There is probably a script out there like the flash script but in javascript.

 

However, if the user has javascript disabled or hasn't got flash installed then they wont be able to resize their images. You'll want to detect where they javascript or flash enabled before you allow them to upload.

Link to comment
https://forums.phpfreaks.com/topic/63439-possible/#findComment-319637
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.