Save a cropped image?

I figured (or threw together) a pretty nice image cropping tool, please go to
http://miamiproviders.com/providers/test_feature.php?id=1
select an image and submit the form
it takes yu to the cropping thing which is great. But, whenI try to save the cropped image, it doesn’t, heres what I have so far but is not working


$dirname = "images/".$_GET['id']."/";
$max_width = "500";                            // Max width allowed for the large image
$thumb_width = "100";                        // Width of thumbnail image
$thumb_height = "100";                        // Height of thumbnail image
//Image Locations
$large_image_location = "{$dirname}temp.jpg";
$thumb_image_location = "{dirfname}feature.jpg";

function resizeThumbnailImage($thumb_image_name, $image, $width, $height, $start_width, $start_height, $scale){
    
    $newImageWidth = ceil($width * $scale);
    $newImageHeight = ceil($height * $scale);
    $newImage = imagecreatetruecolor($newImageWidth,$newImageHeight);
    $source=imagecreatefromjpeg($image); 

    imagecopyresampled($newImage,$source,0,0,$start_width,$start_height,$newImageWidth,$newImageHeight,$width,$height);

    imagejpeg($newImage,$thumb_image_name,90); 

}

if (isset($_POST["upload_thumbnail"])) {
    //echo "<pre>";print_r($_POST);echo "</pre>";
    //Get the new coordinates to crop the image.
    $x1 = $_POST["x1"];
    $y1 = $_POST["y1"];
    $x2 = $_POST["x2"];
    $y2 = $_POST["y2"];
    $w = $_POST["w"];
    $h = $_POST["h"];
    //Scale the image to the thumb_width set above
    $scale = $thumb_width/$w;
    $cropped = resizeThumbnailImage($thumb_image_location, $large_image_location,$w,$h,$x1,$y1,$scale);
}

Any ideas on how to save it?

Thanks…

You have missed off two lines:


// Save the image as a jpg
ImageJPEG( $cropped, "resized.jpg" );
// Clear the memory of the tempory image 
ImageDestroy( $cropped );

I think you had some typos (capatilized letters) in there, is this right


if (isset($_POST["upload_thumbnail"])) {
    //echo "<pre>";print_r($_POST);echo "</pre>";
    //Get the new coordinates to crop the image.
    $x1 = $_POST["x1"];
    $y1 = $_POST["y1"];
    $x2 = $_POST["x2"];
    $y2 = $_POST["y2"];
    $w = $_POST["w"];
    $h = $_POST["h"];
    //Scale the image to the thumb_width set above
    $scale = $thumb_width/$w;
    $cropped = resizeThumbnailImage($thumb_image_location, $large_image_location,$w,$h,$x1,$y1,$scale);
echo $cropped;    
// Save the image as a jpg
imagejpeg( $cropped, $dirname."feature.jpg" );
// Clear the memory of the tempory image 
imagedestroy( $cropped );  
}

I do not think the capital letters make a difference but its a long time since I used GD and it looks right to me.

oh yea, thanks

Do you know why, when I echo $cropped, nothing appears.
Shouldn’t the path of the new cropped image appear?

i tried it again (am trying to understand the php
I created another cropping tool,
http://miamiproviders.com/cropimages/?id=1
So I guess everything is working except the PHP part which saves the new image, heres that script)


$t_width = 300;    // Maximum thumbnail width
$t_height = 300;    // Maximum thumbnail height
$new_name = "feature.jpg"; // Thumbnail image name
$path = "images/{$_GET['id']}/";

if(isset($_GET['t']) and $_GET['t'] == "ajax")
    {
        extract($_GET);
        $ratio = ($t_width/$w); 
        $nw = ceil($w * $ratio);
        $nh = ceil($h * $ratio);
        $nimg = imagecreatetruecolor($nw,$nh);
        $im_src = imagecreatefromjpeg($path.$img);
        imagecopyresampled($nimg,$im_src,0,0,$x1,$y1,$nw,$nh,$w,$h);
        imagejpeg($nimg,$path.$new_name,90);
        echo $new_name;
        exit;
    }

I like this method cause this allows me to crop an ikmagfe on the fly (the other way I had to make a duplicate of the image to be cropped which was a mess for me.