最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - how to save cropped image - Stack Overflow

programmeradmin5浏览0评论

Downloaded the code for cropping image from the following link:

.html

This is the code in crop.php page:

<?php

/**
 * Jcrop image cropping plugin for jQuery
 * Example cropping script
 * @copyright 2008-2009 Kelly Hallman
 * More info: .html
 */

if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
    $targ_w = $targ_h = 150;
    $jpeg_quality = 90;

    $src = 'demo_files/pool.jpg';
    $img_r = imagecreatefromjpeg($src);
    $dst_r = ImageCreateTrueColor( $targ_w, $targ_h );

    imagecopyresampled($dst_r,$img_r,0,0,$_POST['x'],$_POST['y'],
    $targ_w,$targ_h,$_POST['w'],$_POST['h']);

    header('Content-type: image/jpeg');
    imagejpeg($dst_r,null,$jpeg_quality);

    exit;
}

// If not a POST request, display page below:

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ".dtd">
<html xmlns="">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
        <script src="js/jquery.min.js"></script>
        <script src="js/jquery.Jcrop.js"></script>
        <link rel="stylesheet" href="css/jquery.Jcrop.css" type="text/css" />
        <script language="Javascript">

            $(function(){

                $('#cropbox').Jcrop({
                    aspectRatio: 1,
                    onSelect: updateCoords
                });

            });

            function updateCoords(c)
            {
                $('#x').val(c.x);
                $('#y').val(c.y);
                $('#w').val(c.w);
                $('#h').val(c.h);
            };

            function checkCoords()
            {
                if (parseInt($('#w').val())) return true;
                alert('Please select a crop region then press submit.');
                return false;
            };

        </script>

</head>

<body>
<!-- This is the image we're attaching Jcrop to -->
        <img src="demo_files/pool.jpg" id="cropbox" />

        <!-- This is the form that our event handler fills -->
        <form action="crop.php" method="post" onsubmit="return checkCoords();">
            <input type="hidden" id="x" name="x" />
            <input type="hidden" id="y" name="y" />
            <input type="hidden" id="w" name="w" />
            <input type="hidden" id="h" name="h" />
            <input type="submit" value="Crop Image" />
        </form>
</body>
</html>

The cropping is done perfectly and displayed the cropped image successfully.

But i want to save the cropped image into a directory .

How can i do this?

Downloaded the code for cropping image from the following link:

http://deepliquid./content/Jcrop.html

This is the code in crop.php page:

<?php

/**
 * Jcrop image cropping plugin for jQuery
 * Example cropping script
 * @copyright 2008-2009 Kelly Hallman
 * More info: http://deepliquid./content/Jcrop_Implementation_Theory.html
 */

if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
    $targ_w = $targ_h = 150;
    $jpeg_quality = 90;

    $src = 'demo_files/pool.jpg';
    $img_r = imagecreatefromjpeg($src);
    $dst_r = ImageCreateTrueColor( $targ_w, $targ_h );

    imagecopyresampled($dst_r,$img_r,0,0,$_POST['x'],$_POST['y'],
    $targ_w,$targ_h,$_POST['w'],$_POST['h']);

    header('Content-type: image/jpeg');
    imagejpeg($dst_r,null,$jpeg_quality);

    exit;
}

// If not a POST request, display page below:

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
        <script src="js/jquery.min.js"></script>
        <script src="js/jquery.Jcrop.js"></script>
        <link rel="stylesheet" href="css/jquery.Jcrop.css" type="text/css" />
        <script language="Javascript">

            $(function(){

                $('#cropbox').Jcrop({
                    aspectRatio: 1,
                    onSelect: updateCoords
                });

            });

            function updateCoords(c)
            {
                $('#x').val(c.x);
                $('#y').val(c.y);
                $('#w').val(c.w);
                $('#h').val(c.h);
            };

            function checkCoords()
            {
                if (parseInt($('#w').val())) return true;
                alert('Please select a crop region then press submit.');
                return false;
            };

        </script>

</head>

<body>
<!-- This is the image we're attaching Jcrop to -->
        <img src="demo_files/pool.jpg" id="cropbox" />

        <!-- This is the form that our event handler fills -->
        <form action="crop.php" method="post" onsubmit="return checkCoords();">
            <input type="hidden" id="x" name="x" />
            <input type="hidden" id="y" name="y" />
            <input type="hidden" id="w" name="w" />
            <input type="hidden" id="h" name="h" />
            <input type="submit" value="Crop Image" />
        </form>
</body>
</html>

The cropping is done perfectly and displayed the cropped image successfully.

But i want to save the cropped image into a directory .

How can i do this?

Share Improve this question asked Jun 29, 2012 at 4:31 galtech.Mariyagaltech.Mariya 2232 gold badges3 silver badges8 bronze badges 5
  • 1 Have you read the manual on imagejpeg? Change the second parameter from null to the desired filename. – Josh Commented Jun 29, 2012 at 4:36
  • +1 @Josh, do it like this ImageJpeg($dst_r, 'images/' . 'Desired_name'; To save the file into images directory. Read more here php/manual/en/function.imagejpeg.php – Prasenjit Kumar Nag Commented Jun 29, 2012 at 4:39
  • @Joy,I changed the code as imagejpeg($dst_r,'crop_images/',$jpeg_quality);.Then i got this error The image “http://localhost/crop/index.php” cannot be displayed because it contains errors. – galtech.Mariya Commented Jun 29, 2012 at 4:44
  • @galtech.Mariya, Do you still want to show the image, if you do you Can Just link to the newly saved image from images dir. Remove the header('Content-type: image/jpeg'); It will remove the error. – Prasenjit Kumar Nag Commented Jun 29, 2012 at 4:47
  • +1@Joy,ok i got my mistake , only put the disrectory path not included the name of the image – galtech.Mariya Commented Jun 29, 2012 at 4:50
Add a ment  | 

2 Answers 2

Reset to default 3

Try this:

imagejpeg($dst_r, 'yourfilename.jpg', $jpeg_quality);

// Remove from memory - don't forget this part
imagedestroy($dst_r);

More info http://www.php/manual/en/function.imagejpeg.php

Here is how I did it.

Change this:

imagejpeg($dst_r,$src,$jpeg_quality);

to

imagejpeg($dst_r);// Output


imagedestroy($dst_r);// Free the memory
发布评论

评论列表(0)

  1. 暂无评论