Jump to content

[SOLVED] Image Cropping / Cutting function


nathanblogs

Recommended Posts

Hey,

 

I'm currently trying to make a image cutting/cropping function. How it works is theres a basic page load with an image on it on that image contains some JavaScript which allows you to on-the-fly select a rectangle box of the picture, you can then hit the crop button which will post back to the page the details of the page, for example ((x1,y1),(x2,y2),...,... co-ordinates, width and height of the rectangle box). To crop the image I then run this code

 

        $file = "test.jpeg";

// make local variables from $_POST to make them more readable
$x1 = $_POST['x1'];
$y1 = $_POST['y1'];
$x2 = $_POST['x2'];
$y2 = $_POST['y2'];
$width = $_POST['width'];
$height = $_POST['height'];
        $info = getimagesize($file);
        list($width_old, $height_old) = $info;

$dest_image = "test-2.jpg"; // make sure the directory is writeable
		            
        switch ( $info[2] ) {
            case IMAGETYPE_GIF:
                $image = imagecreatefromgif($file);
            break;
            case IMAGETYPE_JPEG:
                $image = imagecreatefromjpeg($file);
            break;
            case IMAGETYPE_PNG:
                $image = imagecreatefrompng($file);
            break;
            default:
                return false;
        } 
        // creates a new image object with dimensions 300x210            	
        $image_resized = imagecreatetruecolor( 300, 210 );
        imagecolortransparent($image_resized, imagecolorallocate($image_resized, 0, 0, 0) );
        imagealphablending($image_resized, false);
        imagesavealpha($image_resized, true);
        
        imagecopyresampled($image_resized, $image, 0, 0, $x1, $y1, $width, $height, $width_old , $height_old ); 
        
        // write to the file
            switch ( $info[2] ) {
            case IMAGETYPE_GIF:
                imagegif($image_resized, $dest_image);
            break;
            case IMAGETYPE_JPEG:
                imagejpeg($image_resized, $dest_image);
            break;
            case IMAGETYPE_PNG:
                imagepng($image_resized, $dest_image);
            break;
            default:
                return false;
        }
        
        
        //imagejpeg($image_resized,$dest_image,90);
        //imagedestroy($image);

	$try_again_html = "?section=files&mode=display_resize_image&id=" . $id;
  	    echo '
  	    <img src="'. $dest_image.'" BORDER=1><p>
	    <a href="' . $try_again_html . '">try again</a>';
	exit;

 

my understanding is that

 imagecopyresampled($image_resized, $image, 0, 0, $x1, $y1, $width, $height, $width_old , $height_old );  

will take the old image and copy from (x1,y1) to (1+width, y1+height) into the new image. However this doesn't seem to be working

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.