SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Zealot dcgamers's Avatar
    Join Date
    Dec 2005
    Posts
    129
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Php Image Crop Function

    How can I crop an image using php? I can use imagecopy() but the manual is very confusing. I have 4 coordinates, x1 y1 and x2 y2.

    Someone explain to me how to use imagecopy() and where to put the variables because it just doesn't make sense to me. Save me from my insanity.

  2. #2
    SitePoint Evangelist
    Join Date
    Oct 2003
    Posts
    440
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP.net is a great place to learn about functions and their arguments. See this:

    http://www.php.net/imagecopy

  3. #3
    SitePoint Zealot dcgamers's Avatar
    Join Date
    Dec 2005
    Posts
    129
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I know that because I said:
    ...but the manual is very confusing.
    I asked for someone to explain the manual because it doesn't make it clear enough. Can someone do that?

  4. #4
    SitePoint Evangelist
    Join Date
    Oct 2003
    Posts
    440
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you scroll down on the manual you will see a bunch of explanations and implementations. This one should work for you.

    Code:
    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&#233;faut
    $x=isset($_GET['x'])?$_GET['x']:0;    // x est facultatif, 0 par d&#233;faut
    $y=isset($_GET['y'])?$_GET['y']:0;    // y est facultatif, 0 par d&#233;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">


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •