tags:

views:

288

answers:

3

I want to crop an image. Is it possible to do like this. And if yes, how would the crop() function look like?

$imgURL is always a .jpg image.

$image = file_get_contents($imgURL);
$maxWidth = 100;
$height = 68;

$image = crop($image, $maxWidth, $height);

file_put_contents("media/imgname.jpg", $image);

function crop($image, $maxWidth, $height){
    ...how do I crop the image?
}
+1  A: 

Take a look at the gd library that's usually part of most PHP installs.

Typically, you'd:

  1. import the image using one of the imagecreatefromTYPE functions
  2. use imagecreate($width,$height) to make a blank buffer image
  3. use imagecopy() to transfer the portion you want to the buffer
  4. use one of the imageTYPE functions to write the buffer out to a file.
Amber
+2  A: 

If you have the GD library installed, taked a look at the functions available to you. If you want more explanation and an example, take a look at this blog.

Also, there's plenty of SO posts to help you along.

A: 

Another option you may be able to use is Image Magick if it GD is not installed on your server.

jimiyash