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
https://forums.phpfreaks.com/topic/78373-solved-image-cropping-cutting-function/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.