Cropping Images

[php] $url = $_POST[‘image’];

function get_content($URL){
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $URL);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}

  $imagesmall = get_content($url);
  $imagelarge = get_content($url);

$path_parts = pathinfo($url); 

$extension = ($path_parts['basename']); 

    file_put_contents('../uploads/small_'.$extension, $imagesmall);
    file_put_contents('../uploads/large_'.$extension, $imagelarge);[/php]

This is my current code and I would like to crop the $imagesmall and $imagelarge before putting the contents to the server. Can anyone help me? I’m a bit new to this.

Here’s a well known routine created by Tato Ulmanen to crop images.

[php]$image = imagecreatefromjpeg($_GET[‘src’]);
$filename = ‘images/cropped_whatever.jpg’;

$thumb_width = 200;
$thumb_height = 150;

$width = imagesx($image);
$height = imagesy($image);

$original_aspect = $width / $height;
$thumb_aspect = $thumb_width / $thumb_height;

if ( $original_aspect >= $thumb_aspect )
{
// If image is wider than thumbnail (in aspect ratio sense)
$new_height = $thumb_height;
$new_width = $width / ($height / $thumb_height);
}
else
{
// If the thumbnail is wider than the image
$new_width = $thumb_width;
$new_height = $height / ($width / $thumb_width);
}

$thumb = imagecreatetruecolor( $thumb_width, $thumb_height );

// Resize and crop
imagecopyresampled($thumb,
$image,
0 - ($new_width - $thumb_width) / 2, // Center the image horizontally
0 - ($new_height - $thumb_height) / 2, // Center the image vertically
0, 0,
$new_width, $new_height,
$width, $height);
imagejpeg($thumb, $filename, 80);[/php]

Sponsor our Newsletter | Privacy Policy | Terms of Service