Ardilla
Ardilla

Reputation: 51

Cropping images with Php

I'm using Jquery plugin ImgAreaSelect with PHP 5.3.9 with GD 2.0.34.

Following some examples from the plugin, I added a form that gives X and Y values from where I start selecting the image till the end of the selection.

This is going OK, because I recieve the values correctly, but I cannot crop the image. Followed some examples/tutorials but always failed.

Here is my PHP code:

$x1 = $_POST['x1']; //this one gives me the point where start to crop
$x2 = $_POST['x2']; //the end of X axis
$y1 = $_POST['y1']; //same for Y1 and Y2
$y2 = $_POST['y2'];
$w = $x2 - $x1; //getting the width for the new image
$h = $y2 - $y1; //getting the height for the new image
$src_img = "path/image";
$format = end(explode(".", $src_img)); //taking the image format (jpg, png, gif)
$size = getimagesize($src_img);
switch($format) {
    case "jpg":
        $copy = imagecreatefromjpeg($src_img);
        $new = ImageCreateTrueColor($w, $h);
        imagecopyresampled($new, $copy, 0, 0, $x1, $y1, $w, $h, $size[0], $size[1]);
        header('Content-type: image/jpeg');
        imagejpeg($new);
    break;
}

I would like to know if there's something wrong (most probably).

Thanks for all and taking your time to help.

Upvotes: 0

Views: 8997

Answers (4)

debasish
debasish

Reputation: 745

//resize and crop image by center
function resize_crop_image($max_width, $max_height, $source_file, $dst_dir, $quality = 80){
    $imgsize = getimagesize($source_file);
    $width = $imgsize[0];
    $height = $imgsize[1];
    $mime = $imgsize['mime'];

    switch($mime){
        case 'image/gif':
            $image_create = "imagecreatefromgif";
            $image = "imagegif";
            break;

        case 'image/png':
            $image_create = "imagecreatefrompng";
            $image = "imagepng";
            $quality = 7;
            break;

        case 'image/jpeg':
            $image_create = "imagecreatefromjpeg";
            $image = "imagejpeg";
            $quality = 80;
            break;

        default:
            return false;
            break;
    }

    $dst_img = imagecreatetruecolor($max_width, $max_height);
    $src_img = $image_create($source_file);

    $width_new = $height * $max_width / $max_height;
    $height_new = $width * $max_height / $max_width;
    //if the new width is greater than the actual width of the image, then the height is too large and the rest cut off, or vice versa
    if($width_new > $width){
        //cut point by height
        $h_point = (($height - $height_new) / 2);
        //copy image
        imagecopyresampled($dst_img, $src_img, 0, 0, 0, $h_point, $max_width, $max_height, $width, $height_new);
    }else{
        //cut point by width
        $w_point = (($width - $width_new) / 2);
        imagecopyresampled($dst_img, $src_img, 0, 0, $w_point, 0, $max_width, 
Resize and crop image from center with PHP


$max_height, $width_new, $height);
    }

    $image($dst_img, $dst_dir, $quality);

    if($dst_img)imagedestroy($dst_img);
    if($src_img)imagedestroy($src_img);
}
//usage example
resize_crop_image(100, 100, "test.jpg", "test.jpg");

Upvotes: 0

dognose
dognose

Reputation: 20889

imagecopyresampled($new, $copy, $x1, $y1, 0, 0, $w, $h, $size[0], $size[1]);

http://php.net/manual/en/function.imagecopyresampled.php

In other words, imagecopyresampled() will take a rectangular area from src_image of width src_w and height src_h at position (src_x,src_y) and place it in a rectangular area of dst_image of width dst_w and height dst_h at position (dst_x,dst_y).

in other words, you need to change it to:

imagecopyresampled($new, $copy,0 ,0 ,$x1, $y1, $w, $h, $w, $h);

Upvotes: 2

bystwn22
bystwn22

Reputation: 1794

Anyway you can try this code also

<?php
  $x1 = $_POST['x1']; //this one gives me the point where start to crop
  $x2 = $_POST['x2']; //the end of X axis
  $y1 = $_POST['y1']; //same for Y1 and Y2
  $y2 = $_POST['y2'];
  $w  = ( $x2 - $x1 ); //getting the width for the new image
  $h  = ( $y2 - $y1 ); //getting the height for the new image

  $src  = "path_to_file";
  $info = getimagesize( $src );

  switch( $info[2] ) {
    case IMAGETYPE_JPEG:
      $copy = imagecreatefromjpeg( $src );
      $new  = imagecreatetruecolor( $w, $h );
      imagecopyresampled( $new, $copy, 0, 0, $x1, $y1, $info[0], $info[1], $w, $h );
      header( 'Content-type: image/jpeg' );
      imagejpeg( $new );
    break;
    default:
    break;
  }
?>

Upvotes: 0

Ardilla
Ardilla

Reputation: 51

thanks to all, @Deepanshu gave me a link wich I saw a piece of code, but somehow strange: bool imagecopyresampled ( resource $dst_image , resource $src_image , int $dst_x , int $dst_y , int $src_x , int $src_y , int $dst_w , int $dst_h , int $src_w , int $src_h )

the last $src_w and $src_h I had to put the new width and the new height, instead the width and height of the original image.

so, the final code working properly is:

imagecopyresampled($new, $copy, 0, 0, $x1, $y1, $w, $h, $w, $h);

Upvotes: -1

Related Questions