简体   繁体   中英

Image Skimming with php & gd;

I have seen tons of examples of Cropping images with php & the gd lib however I have never seen any posts on Skimming or Shaving an image. What I mean by this is say you have pictures from a digital camera that places the date on picture. It is always in a constant place on the picture. So how would I do this? All examples I have come across deal with maintaining an aspect ratio which i just want those 75px or so off the bottom. how could this be done the easiest ? I found this example somewhat enlightening! imagecopyresampled in PHP, can someone explain it?

To create what you need just read this page: http://us.php.net/imagecopy

You can use something like this and just change the $left and $top to correspond to where the date stamp is on the image.

<?php
// Original image
$filename = 'someimage.jpg';

// Get dimensions of the original image
list($current_width, $current_height) = getimagesize($filename);

// The x and y coordinates on the original image where we
// will begin cropping the image
$left = 50;
$top = 50;

// This will be the final size of the image (e.g. how many pixels
// left and down we will be going)
$crop_width = 200;
$crop_height = 200;

// Resample the image
$canvas = imagecreatetruecolor($crop_width, $crop_height);
$current_image = imagecreatefromjpeg($filename);
imagecopy($canvas, $current_image, 0, 0, $left, $top, $current_width, $current_height);
imagejpeg($canvas, $filename, 100);
?>

A similar example is:

Basic way to implement a "crop" feature : given an image (src), an offset (x, y) and a size (w, h).

crop.php :

<?php
$w=$_GET['w'];
$h=isset($_GET['h'])?$_GET['h']:$w;    // h est facultatif, =w par défaut
$x=isset($_GET['x'])?$_GET['x']:0;    // x est facultatif, 0 par défaut
$y=isset($_GET['y'])?$_GET['y']:0;    // y est facultatif, 0 par défaut
$filename=$_GET['src'];
header('Content-type: image/jpg');
header('Content-Disposition: attachment; filename='.$src);
$image = imagecreatefromjpeg($filename); 
$crop = imagecreatetruecolor($w,$h);
imagecopy ( $crop, $image, 0, 0, $x, $y, $w, $h );
imagejpeg($crop);
?>

Call it like this :

<img src="crop.php?x=10&y=20&w=30&h=40&src=photo.jpg">

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM