1

I have the below image which I'd like to crop, so all the black areas are removed and the thumb returned. The thumb must be 130px (width) by 155px (height).

thumb

How can I crop it using PHP's GD library? (Imagick library isn't an option).

If there is something I can improve in my question, please let me know.

EDIT

I've used imagecopyresized() function as suggested by @Martijn, with the following code

imagecopyresized(
    imagecreatetruecolor(130,155) , 
    imagecreatefromjpeg($src_image) , 
    0, 
    0, 
    0, 
    0, 
    130,  
    155,  
    260 , 
    310   
)

What i'm getting is this result

thumb2

What am I doing wrong?

W.D.
  • 1,033
  • 1
  • 7
  • 12
  • To be clear, you want something to: a) trim the black, and b) thumb it? Or is the black an error inyour cropping? – Martijn Apr 19 '14 at 11:52
  • The first option - trim the black and return an image with the dimensions 130x155 – W.D. Apr 19 '14 at 11:54
  • About your edit: It seems like your selection of the image with the black is too small. You sure its 260x310? If so, just play around with 1 variable at a time, see what it does. It'll give you a better understanding of this function :) – Martijn Apr 19 '14 at 12:24
  • Yes, it is. the dimensions are 260x310. I'm sorry, but I'm not English. What do you mean by placing around with 1 variable? – W.D. Apr 19 '14 at 12:26
  • a typo, should be `play around`, fiddle with it, see what happens if you change it – Martijn Apr 19 '14 at 12:27
  • You mean, changing 260 or 310 (the actual with or height)? – W.D. Apr 19 '14 at 12:28
  • Yes. It seems like your selection is too small, now you have to find out why – Martijn Apr 19 '14 at 12:30
  • But have you noticed that the cropped image isn't even present on the first one? I mean totally wrong place is cropped. – W.D. Apr 19 '14 at 12:35

1 Answers1

3

This can be hard to do for a library, as the black may differ in size, and the part of the image you may want is not always the same.

I suggest jCrop (yes, the site is very minimal), which allows you to select a part of the image. If it can be done by hand, this is a very easy method. I use this in my company CMS', our customers never need explanation on how it works, very natural :)


If that is not an option, you can try imagecopyresampled():

imagecopyresampled(
    $dst_image , // the thumb you want to place it on
    $src_image , // the image to crop it from
    0, // place left of thumb
    0, // place top of thumb
    0, // start from left of input image
    0, // start from top of input image
    130,  // destination width
    155,  // destination height
    $src_w , // the width of the image without the black
    $src_h   // the height of the image without the black
)

If you dont have a fixed size for the image on the black canvas, you could write a function to find the offset. You can do this by taking the first line of pixels, and find the first black pixel, and check if there are (lets say) 10min black pixels after that.
This can be sensative, you can increase the scouting area to test if the black is also in the lines below.

Martijn
  • 15,791
  • 4
  • 36
  • 68
  • What about imagecopyresampled() http://www.php.net/manual/en/function.imagecopyresampled.php? Do they differ much? – W.D. Apr 19 '14 at 12:01
  • OK. I'm trying imagecopyresized() and will let you know if it succeeds. – W.D. Apr 19 '14 at 12:04
  • Yes, and that was the one I actually wanted. In VERY simple terms, the copyResized scales like paint does, not that pretty, but fast. The resampled works more like photoshop, prettier result – Martijn Apr 19 '14 at 12:04
  • Just one thing to clarify, $dst_image should have 130x155 dimensions and be created by imagecreatetrucolor(w,h)? – W.D. Apr 19 '14 at 12:08
  • Jup. The jCrop also has some example how to do that. Even if you're not gonna use it, it'll be a push in the right direction – Martijn Apr 19 '14 at 12:13