Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I cut an image from the bottom using PHP?

Tags:

php

image

gd

I want to take out the text in the bottom of an image. How can I cut it from bottom ...say 10 pixels to cut from bottom.

I want do this in PHP. I have lots of images that have text in the bottom.

Is there a way to do it?

like image 697
nazir Avatar asked Feb 24 '10 13:02

nazir


2 Answers

Here you go.

To change the name of the image, change $in_filename (currently 'source.jpg'). You can use URLs in there as well, although obviously that will perform worse.

Change the $new_height variable to set how much of the bottom you want cropped.

Play around with $offset_x, $offset_y, $new_width and $new_height, and you'll figure it out.

Please let me know that it works. :)

Hope it helps!

<?php

$in_filename = 'source.jpg';

list($width, $height) = getimagesize($in_filename);

$offset_x = 0;
$offset_y = 0;

$new_height = $height - 15;
$new_width = $width;

$image = imagecreatefromjpeg($in_filename);
$new_image = imagecreatetruecolor($new_width, $new_height);
imagecopy($new_image, $image, 0, 0, $offset_x, $offset_y, $width, $height);

header('Content-Type: image/jpeg');
imagejpeg($new_image);

?>
like image 65
Teekin Avatar answered Oct 26 '22 20:10

Teekin


You may use the GD Image Library to manipulate images in PHP. The function you're looking for is imagecopy(), which copies part of an image onto another. Here's an example from PHP.net that does roughly what you describe:

<?php

$width = 50;
$height = 50;

$source_x = 0;
$source_y = 0;

// Create images
$source = imagecreatefromjpeg('source.jpg');
$new = imagecreatetruecolor($width, $height);

// Copy
imagecopy($source, $new, 0, 0, $source_x, $source_y, $width, $height);

// Output image
header('Content-Type: image/jpeg');
imagejpeg($new);

?>

To crop the source image, change the $source_x and $source_y variables to your liking.

like image 45
Johannes Gorset Avatar answered Oct 26 '22 20:10

Johannes Gorset