Simple image cropping tool, how to crop after making selection?

Hi,

I am working on a simple image cropping tool, in which I place a picture within a div and user can zoom in/out, move picture left/right/up/down. Upon pressing the respective buttons i display the pic by adjusting width, height, margin-top, margin-left of the pic. Seems to be working fine.

How do I crop the selected area using php ? Below is my code, please help.

[code]<?php
session_start();
session_id();

if (empty($_SESSION[“w”]) or empty($_SESSION[“h”]))
{
// No parameters available, set default
$_SESSION[“w”] = 400;
$_SESSION[“h”] = 267;
$_SESSION[“mt”] = 0;
$_SESSION[“ml”] = 0;
$_SESSION[“z”] = 0;
}

if (isset($_POST[“btnRe”]))
{
$_SESSION[“w”] = 400;
$_SESSION[“h”] = 267;
$_SESSION[“mt”] = 0;
$_SESSION[“ml”] = 0;
$_SESSION[“z”] = 0;
}
if (isset($_POST[“btnZoomIn”]))
{
// No parameters available, set default
$_SESSION[“w”] = $_SESSION[“w”] + 8;
$_SESSION[“h”] = $_SESSION[“h”] + 5;
$_SESSION[“z”] = $_SESSION[“z”] + 1;
}
else if (isset($_POST[“btnZoomOut”]))
{
$_SESSION[“w”] = $_SESSION[“w”] - 8;
$_SESSION[“h”] = $_SESSION[“h”] - 5;
$_SESSION[“z”] = $_SESSION[“z”] - 1;
}
else if (isset($_POST[“btnUp”]))
{
$_SESSION[“mt”] = $_SESSION[“mt”] - 5;
}
else if (isset($_POST[“btnDown”]))
{
$_SESSION[“mt”] = $_SESSION[“mt”] + 5;
}

else if (isset($_POST[“btnLeft”]))
{
$_SESSION[“ml”] = $_SESSION[“ml”] - 5;
}
else if (isset($_POST[“btnRight”]))
{
$_SESSION[“ml”] = $_SESSION[“ml”] + 5;
}

$btnZoomOut = “”;
$btnUp = “”;
$btnDown = “”;
$btnLeft = “”;
$btnRight = “”;

// Buttons enable /disable
/*if ($_SESSION[“w”] < 400 or $_SESSION[“h”] < 270) { $btnZoomOut = " disabled"; } else { $btnZoomOut = “”; }
if (empty($_SESSION[“z”]) and $_SESSION[“mt”] == 0) { $btnUp = " disabled"; } else { $btnUp = “”; }
if (empty($_SESSION[“z”]) and $_SESSION[“mt”] <= 0) { $btnDown = " disabled"; } else { $btnDown = “”; } */
?>

Crop
<style>
#imgContainer {
    width:400px;
    height:270px;
    overflow:hidden; /* part of image outside this div should be hidden */
    border-width:1px;
    border-style:solid;
    border-color:#000000
}
</style>

px; height:<?=$_SESSION["h"]?>px; margin-top:<?=$_SESSION["mt"]?>px; margin-left:<?=$_SESSION["ml"]?>px" id="imgMy" />

<p align="center">
<form method="POST" action="index.php">
	
	<p>
		<input type="submit" name="btnZoomIn" value="+" />
		<input type="submit" name="btnZoomOut" value="-" <?=$btnZoomOut?>/>

		<input type="submit" name="btnUp" value="Up" <?=$btnUp?>/>
		<input type="submit" name="btnDown" value="Down" <?=$btnDown?>/>

		<input type="submit" name="btnLeft" value="Left" <?=$btnLeft?>/>
		<input type="submit" name="btnRight" value="Right" <?=$btnRight?>/>

		<input type="submit" name="btnRe" value="Re-start" />
	</p>
	
	Width: <input type="text" name="w" value="<?=$_SESSION["w"]?>" /><br />
	Height: <input type="text" name="h" value="<?=$_SESSION["h"]?>" /><br />
	MT: <input type="text" name="mt" value="<?=$_SESSION["mt"]?>" /><br />
	ML: <input type="text" name="ml" value="<?=$_SESSION["ml"]?>" /><br />
	Zoom: <input type="text" name="z" value="<?=$_SESSION["z"]?>" /><br />
	
	<p><input type="submit" value="Save" /></p>
</form>	
</p>
[/code]

Thanks.

Hi,

I now have the following which does the job but due to 2 time processing, the quality of image is lost. Can anyone help me fix this and do the cropping in 1 single go ?

[code]<?php
session_start();
session_id();

if (empty($_SESSION[“w”]) or empty($_SESSION[“h”]))
{
// No parameters available, set default
$_SESSION[“w”] = 400;
$_SESSION[“h”] = 267;
$_SESSION[“mt”] = 0;
$_SESSION[“ml”] = 0;
$_SESSION[“z”] = 0;
}
if (isset($_POST[“btnRe”]))
{
$_SESSION[“w”] = 400;
$_SESSION[“h”] = 267;
$_SESSION[“mt”] = 0;
$_SESSION[“ml”] = 0;
$_SESSION[“z”] = 0;
}
if (isset($_POST[“btnZoomIn”]))
{
// No parameters available, set default
$_SESSION[“w”] = $_SESSION[“w”] + 8;
$_SESSION[“h”] = $_SESSION[“h”] + 5;
$_SESSION[“z”] = $_SESSION[“z”] + 1;
}
else if (isset($_POST[“btnZoomOut”]))
{
$_SESSION[“w”] = $_SESSION[“w”] - 8;
$_SESSION[“h”] = $_SESSION[“h”] - 5;
$_SESSION[“z”] = $_SESSION[“z”] - 1;
}
else if (isset($_POST[“btnSave”]))
{
list($src_w, $src_h) = getimagesize(“foto.jpg”);

// Resize the image to new width / height
$src = imagecreatefromjpeg('foto.jpg');
$dest = imagecreatetruecolor($_POST["w"], $_POST["h"]);
imagecopyresampled ($dest, $src, 0, 0, 0, 0, $_POST["w"], $_POST["h"], $src_w, $src_h);
$tempfile = "tmp_" . time() . ".jpg";
imagejpeg($dest, $tempfile);
imagedestroy($dest);
imagedestroy($src);

// Get coords to crop from newly temp file
$x = str_replace ("-", "", $_POST["x"]);
$y = str_replace ("-", "", $_POST["y"]);
$new_x = str_replace ("-", "", $_POST["x"]) + 400;
$new_y = str_replace ("-", "", $_POST["y"]) + 270;

// Crop from the newly created temp picture and save to final
$src = imagecreatefromjpeg($tempfile);
$dest = imagecreatetruecolor(400, 270);
imagecopyresampled ($dest, $src, 0, 0, $x, $y, $new_x, $new_y, $new_x, $new_y);
imagejpeg($dest, "final" . time() . ".jpg");
imagedestroy($dest);
imagedestroy($src);

exit();

}
?>

Crop
<style>
#imgContainer {
    width:400px;
    height:270px;
    overflow:hidden; /* part of image outside this div should be hidden */
    border-width:1px;
    border-style:solid;
    border-color:#000000
}
</style>

px; height:<?=$_SESSION["h"]?>px;" id="imgMy" />

<p align="center">
	<form method="POST">
		<p>
			<input type="submit" name="btnZoomIn" value="+" />
			<input type="submit" name="btnZoomOut" value="-" />

			<input type="submit" name="btnRe" value="Re-start" />
		</p>
		
		Width: <input type="text" name="w" value="<?=$_SESSION["w"]?>" /><br />
		Height: <input type="text" name="h" value="<?=$_SESSION["h"]?>" /><br />
		Zoom: <input type="text" name="z" value="<?=$_SESSION["z"]?>" /><br />
		X: <input type="text" value="0px" name="x" id="x" />
		Y: <input type="text" value="0px" name="y" id="y" />		

		<p><input name="btnSave" type="submit" value="Save" /></p>
	</form>	

	<script type="text/javascript">
	    jQuery(document).ready(function ($) {
		$( "#imgMy" ).draggable({
		    drag: function( event, ui ) {
			$("#x").val($("#imgMy").css('left'));
			$("#y").val($("#imgMy").css('top'));
		    }
		});
	    });           
	</script>	
</p>
[/code]

I have not done this or tested this code but I think it would be something like this.

// Get new dimensions
list($width, $height) = getimagesize($filename);

// Resample
$image_p = imagecreatetruecolor($_SESSION['w'], $_SESSION['h']);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p, $image, $_SESSION['mt'], $_SESSION['ml'], 0, 0, $_SESSION['w'], $_SESSION['h'], $width, $height);

// Output
imagejpeg($image_p, null, 100);    

Hi,

Please see my revised code in which i am not using the mt and ml values instead they have now x,y coordinates of the image. I am not able to do the cropping on the original image without first re-sizing it to the new width/height and then doing the crop. Any help is appreciated.

Thanks.

So… you have a picture.
Why do you resize the picture to crop it? I’m missing something.

I am making a simple crop tool to add in the website, user will upload picture then they can crop. I am resizing it coz i do not find any other way to crop the pic. without resizing it. The user can upload the pic zoom/unzoom it. When they zoom/unzoom it the pic’s dimenssions differ from the original source pic, and to crop I must resize the original pic to the newly zoomed/unzoomed size and then I crop the pic. I am also missing on how to directly crop on the original pic. :frowning:

So you know an X and a Y. You lock a W and an H (400 and 270). Why can you not just go straight from X and Y to the original picture?

This seems to be working.
Had to remove px from input value for X and Y.
Added quality of 100% to imagejpeg($dest, $tempfile,100);
Removed double processing. (If you use the Left, Top fields again you might need to go back)

<?php
session_start();
session_id();
if (empty($_SESSION["w"]) or empty($_SESSION["h"]))
{
    // No parameters available, set default
    $_SESSION["w"] = 400;
    $_SESSION["h"] = 267;
    $_SESSION["mt"] = 0;
    $_SESSION["ml"] = 0;
    $_SESSION["z"] = 0;
}
if (isset($_POST["btnRe"]))
{
    $_SESSION["w"] = 400;
    $_SESSION["h"] = 267;
    $_SESSION["mt"] = 0;
    $_SESSION["ml"] = 0;
    $_SESSION["z"] = 0;
}
if (isset($_POST["btnZoomIn"]))
{
    // No parameters available, set default
    $_SESSION["w"] = $_SESSION["w"] + 8;
    $_SESSION["h"] = $_SESSION["h"] + 5;
    $_SESSION["z"] = $_SESSION["z"] + 1;
}
else if (isset($_POST["btnZoomOut"]))
{
    $_SESSION["w"] = $_SESSION["w"] - 8;
    $_SESSION["h"] = $_SESSION["h"] - 5;
    $_SESSION["z"] = $_SESSION["z"] - 1;
}
else if (isset($_POST["btnSave"]))
{
    list($src_w, $src_h) = getimagesize("foto.jpg");
    // Resize the image to new width / height
    $src = imagecreatefromjpeg('foto.jpg');
    $dest = imagecreatetruecolor($_POST["w"], $_POST["h"]);
    imagecopyresampled ($dest, $src, 0, 0, 0, 0, $_POST["w"], $_POST["h"], $src_w, $src_h);
    $tempfile = "final" . time() . ".jpg";
    imagejpeg($dest, $tempfile,100);
    imagedestroy($dest);
    imagedestroy($src);
    exit();
}
?>
<html>
<head>
    <title>Crop</title>
    <script src="//code.jquery.com/jquery-1.10.2.js"></script>
    <script src="//code.jquery.com/ui/1.11.2/jquery-ui.js"></script>    
    <style>
    #imgContainer {
        width:400px;
        height:270px;
        overflow:hidden; /* part of image outside this div should be hidden */
        border-width:1px;
        border-style:solid;
        border-color:#000000
    }
    </style>
</head>
<body>
    <p align="center">
        <div id="imgContainer">
            <img src="foto.jpg" style="position:relative; top:0px; left:0px; width:<?php echo $_SESSION["w"];?>px; height:<?php echo $_SESSION["h"];?>px;" id="imgMy" />
        </div>
    </p>
    <p align="center">
        <form method="POST">
            <p>
                <input type="submit" name="btnZoomIn" value="+" />
                <input type="submit" name="btnZoomOut" value="-" />
                <input type="submit" name="btnRe" value="Re-start" />
            </p>
            Width: <input type="text" name="w" value="<?php echo $_SESSION["w"];?>" /><br />
            Height: <input type="text" name="h" value="<?php echo $_SESSION["h"];?>" /><br />
            Zoom: <input type="text" name="z" value="<?php echo $_SESSION["z"];?>" /><br />
            X: <input type="text" value="0" name="x" id="x" />
            Y: <input type="text" value="0" name="y" id="y" />        
            <p><input name="btnSave" type="submit" value="Save" /></p>
        </form>    
        <script type="text/javascript">
            jQuery(document).ready(function ($) {
            $( "#imgMy" ).draggable({
                drag: function( event, ui ) {
                $("#x").val($("#imgMy").css('left'));
                $("#y").val($("#imgMy").css('top'));
                }
            });
            });           
        </script>    
    </p>
</body>
</html>

Revision2
When talking about cropping, it seems you would want have the image fixed and a crop outline overlay. The cropped image doesn’t quite match what you see and I haven’t figured out why… it’s close. Anyway, see what you think of this version.

<?php
session_start();
session_id();
if (empty($_SESSION["w"]) or empty($_SESSION["h"]))
{
    // No parameters available, set default
    $_SESSION["w"] = 400;
    $_SESSION["h"] = 270;
    $_SESSION["mt"] = 0;
    $_SESSION["ml"] = 0;
    $_SESSION["z"] = 0;
}
if (isset($_POST["btnRe"]))
{
    $_SESSION["w"] = 400;
    $_SESSION["h"] = 270;
    $_SESSION["mt"] = 0;
    $_SESSION["ml"] = 0;
    $_SESSION["z"] = 0;
}
if (isset($_POST["btnZoomIn"]))
{
    // No parameters available, set default
    $_SESSION["w"] = $_SESSION["w"] + 8;
    $_SESSION["h"] = $_SESSION["h"] + 5;
    $_SESSION["z"] = $_SESSION["z"] + 1;
}
else if (isset($_POST["btnZoomOut"]))
{
    $_SESSION["w"] = $_SESSION["w"] - 8;
    $_SESSION["h"] = $_SESSION["h"] - 5;
    $_SESSION["z"] = $_SESSION["z"] - 1;
}
else if (isset($_POST["btnUp"]))
{
    $_SESSION["mt"] = $_SESSION["mt"] - 5;
}
else if (isset($_POST["btnDown"]))
{
    $_SESSION["mt"] = $_SESSION["mt"] + 5;
}
else if (isset($_POST["btnLeft"]))
{
    $_SESSION["ml"] = $_SESSION["ml"] - 5;
}
else if (isset($_POST["btnRight"]))
{
    $_SESSION["ml"] = $_SESSION["ml"] + 5;
}
else if (isset($_POST["btnSave"]))
{
    list($src_w, $src_h) = getimagesize("foto.jpg");
    
    $src = imagecreatefromjpeg('foto.jpg');                
    $dest = imagecreatetruecolor(400, 270);
    imagecopyresampled ($dest, $src, 0, 0, 0, 0, 400, 270, $src_w, $src_h);
    $tempfile = "tmp_" . time() . ".jpg";
    imagejpeg($dest, $tempfile,100);
    imagedestroy($dest);
    imagedestroy($src);      
    
    // Get coords to crop from newly temp file
    $x = str_replace ("-", "", $_POST["x"]);
    $y = str_replace ("-", "", $_POST["y"]);
    $new_x = str_replace ("-", "", $_POST["x"]) + 400;
    $new_y = str_replace ("-", "", $_POST["y"]) + 270;
        
    // Crop from the newly created temp picture and save to final                                        
    list($src_w, $src_h) = getimagesize($tempfile);
    $src = imagecreatefromjpeg($tempfile); 
    $dest = imagecreatetruecolor($_POST["w"], $_POST["h"]);
    imagecopyresampled ($dest, $src, 0, 0, $x, $y, $new_x, $new_y, $src_w, $src_h);
    imagejpeg($dest, "final" . time() . ".jpg",100);
    imagedestroy($dest);
    imagedestroy($src); 
    //exit();
}

$btnUp = "";
$btnDown = "";
$btnLeft = "";
$btnRight = "";
?>
<html>
<head>
    <title>Crop</title>
    <script src="//code.jquery.com/jquery-1.10.2.js"></script>
    <script src="//code.jquery.com/ui/1.11.2/jquery-ui.js"></script>    
    <style>
    #imgContainer {
        width:400px;
        height:270px;
        overflow:hidden; /* part of image outside this div should be hidden */
        border-width:1px;
        border-style:solid;
        border-color:#000000;
        z-index:1;
    }
    #imgContainer img{
        z-index:1;
    }
    .cropContainer {
        border-width:1px;
        border-style:solid;
        border-color:#F8F8F8;
        z-index:9999;
    }
    </style>
</head>
<body>
    <p align="center">
        <div id="imgContainer">
            <img src="foto.jpg" style="position:relative; top:0px; left:0px; width:400px; height:270px;" />
            <div class="cropContainer" style="position:relative; top:-270px; left:0px; width:<?php echo $_SESSION["w"];?>px; height:<?php echo $_SESSION["h"];?>px; margin-top:<?php echo $_SESSION["mt"];?>px; margin-left:<?php echo $_SESSION["ml"];?>px" id="imgMy">&nbsp;</div>
        </div>
    </p>
    <p align="center">
        <form method="POST">
            <p>
                <input type="submit" name="btnZoomIn" value="+" />
                <input type="submit" name="btnZoomOut" value="-" />
                <input type="submit" name="btnUp" value="Up" <?php echo $btnUp;?> />
                <input type="submit" name="btnDown" value="Down" <?php echo $btnDown;?> />
                <input type="submit" name="btnLeft" value="Left" <?php echo $btnLeft;?> />
                <input type="submit" name="btnRight" value="Right" <?php echo $btnRight;?> />
                <input type="submit" name="btnRe" value="Re-start" />
            </p>
            Width: <input type="text" name="w" value="<?php echo $_SESSION["w"];?>" /><br />
            Height: <input type="text" name="h" value="<?php echo $_SESSION["h"];?>" /><br />
            Top: <input type="text" name="mt" value="<?php echo $_SESSION["mt"];?>" /><br />
            Left: <input type="text" name="ml" value="<?php echo $_SESSION["ml"];?>" /><br />
            Zoom: <input type="text" name="z" value="<?php echo $_SESSION["z"];?>" /><br />
            X: <input type="text" value="<?php echo $_SESSION["ml"];?>" name="x" id="x" />
            Y: <input type="text" value="<?php echo $_SESSION["mt"];?>" name="y" id="y" />        
            <p><input name="btnSave" type="submit" value="Save" /></p>
        </form>    
        <script type="text/javascript">
            jQuery(document).ready(function ($) {
            $( "#imgMy" ).draggable({
                drag: function( event, ui ) {
                $("#x").val($("#imgMy").css('left'));
                $("#y").val($("#imgMy").css('top'));
                }
            });
            });           
        </script>    
    </p>
</body>
</html>

Revision3
Scales edit window and image keeping the ratio intact.
Also fixed some markup.

<?php
session_start();
session_id(); 
 
$image = "foto.jpg";

$max_w = 400;
$max_h = 400;

list($image_w, $image_h) = getimagesize($image);
$scale = min($max_w/$image_w, $max_h/$image_h);
         
    if ($scale < '1') {
        $image_width = floor($scale * $image_w);
        $image_height = floor($scale * $image_h);
    } else {
        $image_width = $image_w;
        $image_height = $image_h;
    }
        
if (empty($_SESSION["w"]) or empty($_SESSION["h"]))
{
    // No parameters available, set default
    $_SESSION["w"] = $image_width;
    $_SESSION["h"] = $image_height;
    $_SESSION["mt"] = 0;
    $_SESSION["ml"] = 0;
    $_SESSION["z"] = 0;
}
if (isset($_POST["btnRe"]))
{
    $_SESSION["w"] = $image_width;
    $_SESSION["h"] = $image_height;
    $_SESSION["mt"] = 0;
    $_SESSION["ml"] = 0;
    $_SESSION["z"] = 0;
}
if (isset($_POST["btnZoomIn"]))
{
    // No parameters available, set default
    $_SESSION["w"] = $_SESSION["w"] + 8;
    $_SESSION["h"] = $_SESSION["h"] + 5;
    $_SESSION["z"] = $_SESSION["z"] + 1;
}
else if (isset($_POST["btnZoomOut"]))
{
    $_SESSION["w"] = $_SESSION["w"] - 8;
    $_SESSION["h"] = $_SESSION["h"] - 5;
    $_SESSION["z"] = $_SESSION["z"] - 1;
}
else if (isset($_POST["btnUp"]))
{
    $_SESSION["mt"] = $_SESSION["mt"] - 5;
}
else if (isset($_POST["btnDown"]))
{
    $_SESSION["mt"] = $_SESSION["mt"] + 5;
}
else if (isset($_POST["btnLeft"]))
{
    $_SESSION["ml"] = $_SESSION["ml"] - 5;
}
else if (isset($_POST["btnRight"]))
{
    $_SESSION["ml"] = $_SESSION["ml"] + 5;
}
else if (isset($_POST["btnSave"]))
{
    list($src_w, $src_h) = getimagesize($image);
    
    $src = imagecreatefromjpeg($image);                
    $dest = imagecreatetruecolor($image_width, $image_height);
    imagecopyresampled ($dest, $src, 0, 0, 0, 0, $image_width, $image_height, $src_w, $src_h);
    $tempfile = "tmp_" . time() . ".jpg";
    imagejpeg($dest, $tempfile,100);
    imagedestroy($dest);
    imagedestroy($src);      
    
    // Get coords to crop from newly temp file                                     
    list($src_w, $src_h) = getimagesize($tempfile);
    $x = str_replace ("-", "", $_POST["x"]);
    $y = str_replace ("-", "", $_POST["y"]);
    $new_x = str_replace ("-", "", $_POST["x"]) + $src_w;
    $new_y = str_replace ("-", "", $_POST["y"]) + $src_h;
        
    // Crop from the newly created temp picture and save to final    
    $src = imagecreatefromjpeg($tempfile); 
    $dest = imagecreatetruecolor($_POST["w"], $_POST["h"]);
    imagecopyresampled ($dest, $src, 0, 0, $x, $y,  $src_w, $src_h, $src_w, $src_h);
    imagejpeg($dest, "final" . time() . ".jpg",100);
    imagedestroy($dest);
    imagedestroy($src);
    unlink($tempfile); 
    //exit();
}

$btnUp = "";
$btnDown = "";
$btnLeft = "";
$btnRight = "";
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
    <title>Crop</title>
    <script type="text/javascript" src="//code.jquery.com/jquery-1.10.2.js"></script>
    <script type="text/javascript" src="//code.jquery.com/ui/1.11.2/jquery-ui.js"></script>    
    <style type="text/css">
    #imgContainer {
        padding:0;
        overflow:hidden; /* part of image outside this div should be hidden */
        border-width:1px;
        border-style:solid;
        border-color:#000000;
        z-index:1;
    }
    #imgContainer img{
        z-index:1;
    }
    
    #imgContainer div{
        padding:0; 
        margin:0;
    }
    .cropContainer { 
        margin:0;
        border-width:1px;
        border-style:solid;
        border-color:#F8F8F8;
        z-index:9999;
    }
    .label {
        width:50px;
        display:inline-block;
        }
    </style>
</head>
<body>
        <div id="imgContainer" style="width:<?php echo $image_width;?>px; height:<?php echo $image_height;?>px;">
            <img src="<?php echo $image;?>" style="position:relative; top:0px; left:0px; width:<?php echo $image_width;?>px; height:<?php echo $image_height;?>px;" alt="" />
            <div class="cropContainer" style="position:relative; top:-<?php echo $image_height+5;?>px; left:-1px; width:<?php echo $_SESSION["w"];?>px; height:<?php echo $_SESSION["h"];?>px; margin-top:<?php echo $_SESSION["mt"];?>px; margin-left:<?php echo $_SESSION["ml"];?>px" id="imgMy"></div>
        </div>
        <form action="" method="post">
            <p>
                <input type="submit" name="btnZoomIn" value="+" />
                <input type="submit" name="btnZoomOut" value="-" />
                <input type="submit" name="btnUp" value="Up" <?php echo $btnUp;?> />
                <input type="submit" name="btnDown" value="Down" <?php echo $btnDown;?> />
                <input type="submit" name="btnLeft" value="Left" <?php echo $btnLeft;?> />
                <input type="submit" name="btnRight" value="Right" <?php echo $btnRight;?> />
                <input type="submit" name="btnRe" value="Re-start" />
            </p>
            <div class="label">Width: </div><input type="text" name="w" value="<?php echo $_SESSION["w"];?>" /><br />
            <div class="label">Height: </div><input type="text" name="h" value="<?php echo $_SESSION["h"];?>" /><br />
            <div class="label">Top: </div><input type="text" name="mt" value="<?php echo $_SESSION["mt"];?>" /><br />
            <div class="label">Left: </div><input type="text" name="ml" value="<?php echo $_SESSION["ml"];?>" /><br />
            <div class="label">Zoom: </div><input type="text" name="z" value="<?php echo $_SESSION["z"];?>" /><br />
            <div class="label">X: </div><input type="text" value="<?php echo $_SESSION["ml"];?>" name="x" id="x" />
            Y: <input type="text" value="<?php echo $_SESSION["mt"];?>" name="y" id="y" />        
            <p><input name="btnSave" type="submit" value="Save" /></p>
        </form>    
        <script type="text/javascript">
            jQuery(document).ready(function ($) {
            $( "#imgMy" ).draggable({
                drag: function( event, ui ) {
                $("#x").val($("#imgMy").css('left'));
                $("#y").val($("#imgMy").css('top'));
                }
            });
            });           
        </script>    
</body>
</html>

@StarLion How to calculate the zoom factor ? The x,y are correct if i re-size the pic to the zoom level selected by user, but they are not correct on the original picture, that is why I am first re-sizing it and then cropping it. Hope that makes sense. Its weird but its working.

Thanks.

@Drummin Thanks for trying but zoom / unzoom is not working in your code revision #3. Also i don’t want to do it via the overlay way. There are already readymade solutions like that available. I am all fine with the way it is now, just wanted to reduce the 2 steps i am doing to crop the image to 1 single step (like directly cropping from original image, instead of first resizing it and then cropping it).

Thanks.

Well you code is not really cropping it, just resizing the image.
Post #8 does that in one step.

No the code in post #8 is not working. It is not cropping at all, its just copying the whole picture at a new size. My code in post #2 though using 2 time imagecopy but in 2nd step it does the cropping. Basically I just need to calculate the zoom factor and then crop, just don’t know how to do that :frowning:

Thanks.

Never used this function myself, is imagecrop() what you’re looking for?

Then why are you resizing the image with the interface? The overlay at least lets you know where you are “cropping”. Did you try adding the “,100” to the imagejpeg() line to keep the quality at 100%? I believe that was the issue you were having from the start.

The last revision does crop the image.

And the final 336 x 227

Hi,

Yes I added 100 to imagejpeg, I am trying to replicate this canvasbuddy.co.uk. Try for yourself. Its made in ASP, I am trying to made in PHP but I used jquery to move the image instead of putting left/right/up/down arrow buttons which refreshes the page everytime.

Anyways when you select the crop area in canvasbuddy what they are doing is instead of cropping the image, they mark the area on the original pic, which needs cropping instead of cropping the real image. That way they are able to retain the original image uploaded by user without losing the quality. But I am doing the crop coz I am not able to figure out how they are doing it.

Hope this will show what I am trying to achieve.

Thanks.

@StarLion @Drummin Finally I did it, got the difference between the original w,h and new w,h and then i calculated the rest. Works great.

[code]<?php
session_start();
session_id();

function solve($val1, $val2)
{
$orignum = $val1;
$secnum = $val2;

if($orignum - $secnum == 0) { $difference = 0; }

if($orignum - $secnum > 0) {
	$difference = $orignum - $secnum;
	$percentchange = ($difference / $orignum) * 100;
}

if ($orignum - $secnum < 0) {
	$difference = $secnum - $orignum;
	$percentchange = ($difference / $orignum) * 100;
}

if($orignum > $secnum) {
	$direction = "sub";
}

if($secnum > $orignum) {
	$direction = "add";
}

return (number_format( $percentchange, 2));

}

if (empty($_SESSION[“w”]) or empty($_SESSION[“h”]))
{
$_SESSION[“x”] = 0;
$_SESSION[“y”] = 0;
$_SESSION[“w”] = 400;
$_SESSION[“h”] = 267;
$_SESSION[“z”] = 0;
}
if (isset($_POST[“btnRe”]))
{
$_SESSION[“x”] = 0;
$_SESSION[“y”] = 0;
$_SESSION[“w”] = 400;
$_SESSION[“h”] = 267;
$_SESSION[“z”] = 0;
}
if (isset($_POST[“btnZoomIn”]))
{
// No parameters available, set default
$_SESSION[“x”] = $_POST[“x”];
$_SESSION[“y”] = $_POST[“y”];
$_SESSION[“w”] = $_SESSION[“w”] + 16;
$_SESSION[“h”] = $_SESSION[“h”] + 10;
$_SESSION[“z”] = $_SESSION[“z”] + 1;
}
else if (isset($_POST[“btnZoomOut”]))
{
$_SESSION[“x”] = $_POST[“x”];
$_SESSION[“y”] = $_POST[“y”];
$_SESSION[“w”] = $_SESSION[“w”] - 16;
$_SESSION[“h”] = $_SESSION[“h”] - 10;
$_SESSION[“z”] = $_SESSION[“z”] - 1;
}
else if (isset($_POST[“btnSave”]))
{
list($src_w, $src_h) = getimagesize(“foto.jpg”);

$p_width = solve ($_POST["w"], $src_w);
$p_height = solve ($_POST["h"], $src_h);
$x = intval(str_replace ("-", "", $_POST["x"]));
$y = intval(str_replace ("-", "", $_POST["y"]));
$new_x = floor($x * $p_width / 100 + $x);
$new_y = floor($y * $p_height / 100 + $y);
$width = ($x + 400);
$height = ($y + 270);
$new_width = floor($width * $p_width / 100 + $width);
$new_height = floor($height * $p_height / 100 + $height);
$pic_width = $new_width - $new_x;
$pic_height = $new_height - $new_y;

$src = imagecreatefromjpeg('foto.jpg');
$dest = imagecreatetruecolor($pic_width, $pic_height);
imagecopyresampled ($dest, $src, 0, 0, $new_x, $new_y, $pic_width, $pic_height, $pic_width, $pic_height);
$tempfile = "tmp_" . time() . ".png";
imagepng($dest, "final" . time() . ".png", 0);
imagedestroy($dest);
imagedestroy($src); 

exit();

}

if (empty($_SESSION[“z”])) {
$btnZoomOut = " disabled";
} else {
$btnZoomOut = “”;
}
?>

Crop
<style>
#imgContainer {
    width:400px;
    height:270px;
    overflow:hidden; /* part of image outside this div should be hidden */
    border-width:1px;
    border-style:solid;
    border-color:#000000
}
</style>

px; height:<?=$_SESSION["h"]?>px;" id="imgMy" />

<form method="POST">
	<p>
		<input class="sub" type="submit" name="btnZoomIn" value="+" />
		<input class="sub" type="submit" name="btnZoomOut" value="-"<?=$btnZoomOut?> />
		<input class="sub" type="submit" name="btnRe" value="Re-start" />
	</p>
	
	<input type="hidden" name="w" value="<?=$_SESSION["w"]?>" />
	<input type="hidden" name="h" value="<?=$_SESSION["h"]?>" />
	<input type="hidden" name="z" value="<?=$_SESSION["z"]?>" />
	<input type="hidden" value="<?=$_SESSION["x"]?>" name="x" id="x" />
	<input type="hidden" value="<?=$_SESSION["y"]?>" name="y" id="y" />		

	<p><input name="btnSave" type="submit" value="Save" /></p>
</form>	

<script type="text/javascript">
jQuery(document).ready(function ($) {
	$( "#imgMy" ).draggable({
		drag: function( event, ui ) {
			$("#x").val($("#imgMy").css('left'));
			$("#y").val($("#imgMy").css('top'));
		}
	});
});           
</script>	
[/code]

Thanks for the help.

Well I’m glad it’s working for you. I tried it and it didn’t work as the image created was larger but had black where the zoomed image should be but I am seeing what you are trying to do.

Edit: Is the image you are testing with 400 x 267? I was thinking the script might work if the image is the same size as the frame you are putting it in. As I was testing with a large image, I ended up with black around the final image.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.