user888864
user888864

Reputation: 25

Show cropped portion of jpeg in PHP/GD?

I have lots of jpeg images that are oversized (height, width), all with different sizes and different aspect ratios.

I'm showing them from a database like this:

<img src="<?php echo $img1; ?>" width="300" height="400">

What I'd like to have is some kind of php function to display a cropped version of the image like this:

cropjpeg('$img1');

For example, if the image is 600 W, 700 H, have php or GD show a cropped version around 300 W, 400 H starting from the top left hand of the image.

Some caveats:

  1. I can't use CSS to do faux cropping. If you download this cropped image, it needs to be just that, the smaller cropped version.

  2. I don't really want to create and save a new image, so I guess I need this to work "on the fly"

  3. I tried imagecreatefromjpeg and imagejpeg, but it seems as though header('Content-Type: image/jpeg'); is not the right answer since there is more on the html page than just this 1 image

  4. There is a lot of traffic on the site, so it has to tread lightly

Here's the best I found so far, but it uses the header('Content-Type: image/jpeg');

<?php
    function cropjpeg($img, $x, $y, $width, $height,$grade=5)
    {
        // Create image instances
        $src = imagecreatefromjpeg($img);
        $dest = imagecreatetruecolor(400, 300);

        // Copy
        imagecopy($dest, $src, 0, 0, 20, 13, 400, 300);

        // Output and free from memory
        header('Content-Type: image/jpeg');
        imagejpeg($dest);

        imagedestroy($dest);
        imagedestroy($src);
    }
    cropjpeg('images/bikini.jpg');

?>

Any ideas?

Upvotes: 1

Views: 1759

Answers (3)

Mihai Iorga
Mihai Iorga

Reputation: 39704

You cannot crop an image in the actual html page as it needs to be processed, that's if you don't want to save it on disk.

You can dump the crop script in a single PHP file with headers and call from HTML the script with path eg:

<img src="http://www.example.org/crop.php?i=bikini.jpg&x=13&y=20&w=400&h=300" alt="" />

and in script you can use

<?php

     header('Content-Type: image/jpeg');
     function cropjpeg($img, $x, $y, $width, $height,$grade=5) {
         //......................
         imagecopy($dest, $src, 0, 0, $x, $y, $width, $height);
         //......................
     }

     cropjpeg('images/'.$_GET['i'], (int)$_GET['x'], (int)$_GET['y'], (int)$_GET['w'], (int)$_GET['h']);
?>

You should consider the aspect ratio, and search for a better crop script, you don't have to reinvent the wheel.

Upvotes: 2

Here is a page with a very complete tutorial with everything you need to do this using ajax included

crop with jquery and php

[http://www.skillcorp.com.ve][2]

[2]: http://www.skillcorp.com.ve here also more info relational

Upvotes: 0

Dan Grossman
Dan Grossman

Reputation: 52372

Use mod_rewrite to send requests for the relevant image file paths to a PHP script, which does the cropping and sends the image data back in response. It looks like a normal image tag on the page, but the images are really being served by your separate script.

The only issue is that you simultaneously don't want to save the cropped files, but have a lot of traffic so want to "tread lightly". You can't have both ways. If you don't want to re-crop the images on every request, you'll need to save the cropped versions so you can re-serve them on future requests.

Upvotes: 0

Related Questions